Package com.attivio.util
Class HTTPUtils
- java.lang.Object
-
- com.attivio.util.HTTPUtils
-
public class HTTPUtils extends java.lang.ObjectContains utility methods related to HTTP. This class has two categories of methods. The first set of methods provide access to a pooled set of http client connections. The pool uses a centralized connection manager which is configured with the default properties identified inHttpClientTransport. This pool of clients share authorization and the credentials provider (if any) and will automatically configure preemptive authentication. Unless noted, these methods do not require any action on the user to clean up resources.The class also defines a set of single-use client methods, all starting with
singleUse(for examplesingleUseClient(). When using the single-use methods, the caller is responsible for closing the returnedCloseableHttpClientto clean up resources.
-
-
Constructor Summary
Constructors Constructor Description HTTPUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.apache.http.client.methods.CloseableHttpResponseexecute(org.apache.http.client.methods.HttpUriRequest method)Executes the method and returns the response.static byte[]getAsBytes(java.lang.String url)Executes a GET and returns results as byte array.static intgetAsBytes(java.lang.String url, byte[] bytes)Executes a GET and populates results in the provided byte array.static java.io.InputStreamgetAsStream(java.lang.String url)Executes an HTTP get and returns the response back as a stream using the default client.static java.io.InputStreamgetAsStream(java.lang.String url, boolean close)Executes an HTTP get and returns the response back as a stream using the default client.static java.lang.StringgetAsText(java.lang.String url)Executes a GET and returns results as a string in the default encoding.static java.io.InputStreamgetConnectionClosingStream(org.apache.http.client.methods.HttpRequestBase m)Gets a stream from the HTTP method that will be automatically closed when the stream is closed.static org.apache.http.pool.PoolStatsgetConnectionManagerTotalStats()static longgetContentLength(java.lang.String url)Executes a HTTP header request and returns the content lengthstatic org.apache.http.client.CredentialsProvidergetCredentialsProvider()static intgetSizeReturned(java.lang.String url)Executes a GET and returns the number of size in bytes of the result returned.static voidgetToStream(java.lang.String url, java.io.OutputStream out)Reads the full contents of a URL.static booleanisAuthRequired()static longlastModified(java.lang.String url)Executes a HTTP header request and returns the lastModified header as a unix timestampstatic java.lang.Stringpost(InputStreamBuilder in, java.lang.String url)Executes an HTTP request and gets a response back for inspection.static <T> Tpost(java.lang.Class<? extends T> clazz, org.apache.http.entity.AbstractHttpEntity entity, java.lang.String url, boolean close)static java.lang.StringpostAndGetText(java.lang.String request, java.lang.String uri, org.apache.http.entity.ContentType contentType)Executes an HTTP request and gets the text response.static java.lang.StringpostAndGetText(java.lang.String request, java.lang.String uri, org.apache.http.entity.ContentType contentType, java.lang.String encoding)Executes an HTTP request and gets the text response.static <T> Tput(java.lang.Class<? extends T> clazz, org.apache.http.HttpEntity entity, java.lang.String url)static voidresetConnectionManager()static voidsetCredentialsProvider(org.apache.http.client.CredentialsProvider provider)Specifies the credentials provider that gets called for servers returning 401, authentication required, requests.static org.apache.http.impl.client.CloseableHttpClientsingleUseClient()static org.apache.http.impl.client.CloseableHttpClientsingleUseClient(java.lang.String username, java.lang.String password)Returns aHttpClientthat must be closed after use.static org.apache.http.impl.client.CloseableHttpClientsingleUseClient(org.apache.http.auth.AuthScope authScope, org.apache.http.client.CredentialsProvider provider, java.lang.String username, java.lang.String password)Returns aHttpClientthat must be closed after use.static org.apache.http.impl.client.CloseableHttpClientsingleUseClient(org.apache.http.auth.AuthScope authScope, org.apache.http.client.CredentialsProvider provider, java.lang.String username, java.lang.String password, java.lang.String keyStorePath, java.lang.String keyStoreType)Returns aHttpClientthat must be closed after use.static HttpResponseStreamstream(java.lang.String url)Initialize a streaming client connection.
-
-
-
Method Detail
-
resetConnectionManager
public static void resetConnectionManager()
-
getCredentialsProvider
public static org.apache.http.client.CredentialsProvider getCredentialsProvider()
-
setCredentialsProvider
public static void setCredentialsProvider(org.apache.http.client.CredentialsProvider provider)
Specifies the credentials provider that gets called for servers returning 401, authentication required, requests.- Parameters:
provider- credentials provider. null removes authentication from client requests.
-
post
public static <T> T post(java.lang.Class<? extends T> clazz, org.apache.http.entity.AbstractHttpEntity entity, java.lang.String url, boolean close) throws AttivioException- Throws:
AttivioException
-
put
public static <T> T put(java.lang.Class<? extends T> clazz, org.apache.http.HttpEntity entity, java.lang.String url) throws AttivioException- Throws:
AttivioException
-
getAsStream
public static java.io.InputStream getAsStream(java.lang.String url) throws AttivioExceptionExecutes an HTTP get and returns the response back as a stream using the default client. The caller is responsible for closing the input stream after use.- Throws:
AttivioException
-
getAsStream
public static java.io.InputStream getAsStream(java.lang.String url, boolean close) throws AttivioExceptionExecutes an HTTP get and returns the response back as a stream using the default client. The caller is responsible for closing the input stream after use.- Parameters:
url-close- ??- Throws:
AttivioException
-
stream
public static HttpResponseStream stream(java.lang.String url) throws AttivioException
Initialize a streaming client connection. The caller is responsible for closing the input stream after use.- Throws:
AttivioException
-
getConnectionClosingStream
public static java.io.InputStream getConnectionClosingStream(org.apache.http.client.methods.HttpRequestBase m) throws AttivioExceptionGets a stream from the HTTP method that will be automatically closed when the stream is closed.- Throws:
AttivioException
-
getContentLength
public static long getContentLength(java.lang.String url) throws AttivioExceptionExecutes a HTTP header request and returns the content length- Throws:
AttivioException
-
lastModified
public static long lastModified(java.lang.String url) throws AttivioExceptionExecutes a HTTP header request and returns the lastModified header as a unix timestamp- Throws:
AttivioException
-
post
public static java.lang.String post(InputStreamBuilder in, java.lang.String url) throws AttivioException
Executes an HTTP request and gets a response back for inspection.- Parameters:
in- input stream to read from and then write to the http output streamurl- URL to send request to- Returns:
- The response as a String
- Throws:
AttivioException- if an error occurred
-
postAndGetText
public static java.lang.String postAndGetText(java.lang.String request, java.lang.String uri, org.apache.http.entity.ContentType contentType) throws AttivioExceptionExecutes an HTTP request and gets the text response.- Parameters:
request- string to POSTuri- URI to send request tocontentType- the type of request- Returns:
- the string output from the POST. returns the error stream if a HTTP >400 is returned
- Throws:
AttivioException- if an error occurred
-
postAndGetText
public static java.lang.String postAndGetText(java.lang.String request, java.lang.String uri, org.apache.http.entity.ContentType contentType, java.lang.String encoding) throws AttivioExceptionExecutes an HTTP request and gets the text response.- Parameters:
request- string to POSTuri- URI to send request tocontentType- the type of requestencoding-- Returns:
- the string output from the POST. returns the error stream if a HTTP >400 is returned
- Throws:
AttivioException- if an error occurred
-
getToStream
public static void getToStream(java.lang.String url, java.io.OutputStream out) throws AttivioExceptionReads the full contents of a URL.- Parameters:
url- uri to readout- OutputStream to write content to- Throws:
AttivioException- if an error occurred
-
getAsText
public static java.lang.String getAsText(java.lang.String url) throws AttivioExceptionExecutes a GET and returns results as a string in the default encoding. HTTP status codes other than SC_OK will trigger an AttivioException. SC_NOT_FOUND and HttpStatus.SC_NO_CONTENT are mapped toKernelError.NOT_FOUNDin the thrown exception.- Parameters:
url- url to GET- Returns:
- string in the default encoding
- Throws:
AttivioException- if an error occurred.
-
getAsBytes
public static byte[] getAsBytes(java.lang.String url) throws AttivioExceptionExecutes a GET and returns results as byte array.- Parameters:
url- url to GET- Returns:
- byte array
- Throws:
java.io.IOException- if an error occurredAttivioException
-
getAsBytes
public static int getAsBytes(java.lang.String url, byte[] bytes) throws AttivioExceptionExecutes a GET and populates results in the provided byte array. This allows to control the amount of data read- Parameters:
url- url to GETbytes- to populate- Returns:
- byte number of bytes actually read
- Throws:
AttivioException- if an error occurred
-
getSizeReturned
public static int getSizeReturned(java.lang.String url) throws AttivioExceptionExecutes a GET and returns the number of size in bytes of the result returned. The full stream of bytes is consumed to determine the length.- Parameters:
url- url to GET- Returns:
- size in bytes of the return
- Throws:
java.io.IOException- if an error occuredAttivioException
-
execute
public static org.apache.http.client.methods.CloseableHttpResponse execute(org.apache.http.client.methods.HttpUriRequest method) throws AttivioExceptionExecutes the method and returns the response. Compressed responses are accepted. If the response is unauthorized, AttivioException is thrown. Otherwise if the response is not ok an AttivioException with the response text decoded is thrown.- Parameters:
method- method to execute- Returns:
- the response, which must be closed when finished
- Throws:
AttivioException- if unauthorized or the response is not ok.
-
isAuthRequired
public static boolean isAuthRequired()
- Returns:
-
singleUseClient
public static org.apache.http.impl.client.CloseableHttpClient singleUseClient()
-
singleUseClient
public static org.apache.http.impl.client.CloseableHttpClient singleUseClient(java.lang.String username, java.lang.String password)Returns aHttpClientthat must be closed after use. Only a single outbound connection will created and maintained. Pre-emptive authentication not supported.- Parameters:
username- ifnullno authentication is configuredpassword- ifnullno authentication is configured
-
singleUseClient
public static org.apache.http.impl.client.CloseableHttpClient singleUseClient(org.apache.http.auth.AuthScope authScope, org.apache.http.client.CredentialsProvider provider, java.lang.String username, java.lang.String password)Returns aHttpClientthat must be closed after use. Only a single outbound connection will created and maintained. Pre-emptive authentication not supported.- Parameters:
authScope-provider-username- ifnullno authentication is configuredpassword- ifnullno authentication is configured
-
singleUseClient
public static org.apache.http.impl.client.CloseableHttpClient singleUseClient(org.apache.http.auth.AuthScope authScope, org.apache.http.client.CredentialsProvider provider, java.lang.String username, java.lang.String password, java.lang.String keyStorePath, java.lang.String keyStoreType)Returns aHttpClientthat must be closed after use. Only a single outbound connection will created and maintained. Pre-emptive authentication not supported. Note!! If keyStorePath not provided, the returned client will trust any https connection.- Parameters:
authScope-provider-username- ifnullno authentication is configuredpassword- ifnullno authentication is configuredkeyStorePath- ifnullany host is trusted, otherwise an SSL context is set up for the connectionkeyStoreType- ifnulldefaults to jks
-
getConnectionManagerTotalStats
public static org.apache.http.pool.PoolStats getConnectionManagerTotalStats()
-
-