public class HttpOp extends Object
For HTTP GET, the application supplies a URL, the accept header string, and a list of handlers to deal with different content type responses.
For HTTP POST, the application supplies a URL, content, the accept header string, and a list of handlers to deal with different content type responses, or no response is expected.
For HTTP PUT, the application supplies a URL, content, the accept header string
Modifier and Type | Class and Description |
---|---|
static class |
HttpOp.CaptureInput
TypedInputStream from an HTTP response.
|
static class |
HttpOp.CaptureString
Capture response as a string (UTF-8 assumed)
|
Modifier and Type | Field and Description |
---|---|
static String |
ARQ_USER_AGENT
Constant for the default User-Agent header that ARQ will use
|
Constructor and Description |
---|
HttpOp() |
Modifier and Type | Method and Description |
---|---|
static void |
applyAuthentication(org.apache.http.impl.client.AbstractHttpClient client,
String target,
org.apache.http.protocol.HttpContext context,
HttpAuthenticator authenticator)
Applies authentication to the given client as appropriate
|
static void |
applyUserAgent(org.apache.http.HttpMessage message)
Applies the configured User-Agent string to the HTTP request
|
static org.apache.http.client.HttpClient |
createCachingHttpClient()
Create an HttpClient that performs connection pooling.
|
static void |
execHttpDelete(String url)
Executes a HTTP DELETE operation
|
static void |
execHttpDelete(String url,
HttpResponseHandler handler)
Executes a HTTP DELETE operation
|
static void |
execHttpDelete(String url,
HttpResponseHandler handler,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP DELETE operation
|
static TypedInputStream |
execHttpGet(String url)
Executes a HTTP GET and return a TypedInputStream.
|
static TypedInputStream |
execHttpGet(String url,
String acceptHeader)
Executes a HTTP GET and return a TypedInputStream.
|
static TypedInputStream |
execHttpGet(String url,
String acceptHeader,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP GET and returns a TypedInputStream
|
static void |
execHttpGet(String url,
String acceptHeader,
HttpResponseHandler handler)
Executes a HTTP Get request, handling the response with given handler.
|
static void |
execHttpGet(String url,
String acceptHeader,
HttpResponseHandler handler,
HttpAuthenticator authenticator)
Executes a HTTP Get request handling the response with the given handler.
|
static void |
execHttpGet(String url,
String acceptHeader,
HttpResponseHandler handler,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP Get request handling the response with one of the given
handlers
|
static String |
execHttpGetString(String url)
Convenience operation to execute a GET with no content negtotiation and
return the response as a string.
|
static String |
execHttpGetString(String url,
String acceptHeader)
Convenience operation to execute a GET and return the response as a
string
|
static void |
execHttpHead(String url)
Executes a HTTP HEAD operation
|
static void |
execHttpHead(String url,
String acceptString,
HttpResponseHandler handler)
Executes a HTTP HEAD operation
|
static void |
execHttpHead(String url,
String acceptString,
HttpResponseHandler handler,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP HEAD operation
|
static void |
execHttpPost(String url,
org.apache.http.HttpEntity entity)
Executes a HTTP POST of the given entity
|
static void |
execHttpPost(String url,
org.apache.http.HttpEntity entity,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
POST with response body.
|
static void |
execHttpPost(String url,
org.apache.http.HttpEntity entity,
String acceptString,
HttpResponseHandler handler)
Executes a HTTP Post
|
static void |
execHttpPost(String url,
org.apache.http.HttpEntity entity,
String acceptHeader,
HttpResponseHandler handler,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
POST with response body.
|
static void |
execHttpPost(String url,
String contentType,
InputStream input,
long length)
Executes a HTTP POST with a request body from an input stream without
response body with no response handling
|
static void |
execHttpPost(String url,
String contentType,
InputStream input,
long length,
String acceptType,
HttpResponseHandler handler)
Executes a HTTP POST with request body from an input stream and response
handling.
|
static void |
execHttpPost(String url,
String contentType,
InputStream input,
long length,
String acceptType,
HttpResponseHandler handler,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP POST with request body from an input stream and response
handling.
|
static void |
execHttpPost(String url,
String contentType,
String content)
Executes a HTTP POST with the given contentype/string as the request body
and throws away success responses, failure responses will throw an error.
|
static void |
execHttpPost(String url,
String contentType,
String content,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP POST with a string as the request body and response
handling
|
static void |
execHttpPost(String url,
String contentType,
String content,
String acceptType,
HttpResponseHandler handler,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP POST with a string as the request body and response
handling
|
static void |
execHttpPostForm(String url,
Params params)
Executes a HTTP POST.
|
static void |
execHttpPostForm(String url,
Params params,
String acceptString,
HttpResponseHandler handler)
Executes a HTTP POST form operation
|
static void |
execHttpPostForm(String url,
Params params,
String acceptHeader,
HttpResponseHandler handler,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP POST form operation
|
static TypedInputStream |
execHttpPostFormStream(String url,
Params params,
String acceptHeader)
Executes a HTTP POST and returns a TypedInputStream, The TypedInputStream
must be closed.
|
static TypedInputStream |
execHttpPostFormStream(String url,
Params params,
String acceptHeader,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP POST Form and returns a TypedInputStream
|
static void |
execHttpPut(String url,
org.apache.http.HttpEntity entity)
Executes a HTTP PUT operation
|
static void |
execHttpPut(String url,
org.apache.http.HttpEntity entity,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP PUT operation
|
static void |
execHttpPut(String url,
String contentType,
InputStream input,
long length)
Executes a HTTP PUT operation
|
static void |
execHttpPut(String url,
String contentType,
InputStream input,
long length,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP PUT operation
|
static void |
execHttpPut(String url,
String contentType,
String content)
Executes a HTTP PUT operation
|
static void |
execHttpPut(String url,
String contentType,
String content,
org.apache.http.client.HttpClient httpClient,
org.apache.http.protocol.HttpContext httpContext,
HttpAuthenticator authenticator)
Executes a HTTP PUT operation
|
static HttpAuthenticator |
getDefaultAuthenticator()
Gets the default authenticator used for authenticate requests if no
specific authenticator is provided.
|
static org.apache.http.client.HttpClient |
getDefaultHttpClient()
Return the current default
HttpClient . |
static boolean |
getUseDefaultClientWithAuthentication()
Gets whether the configured default HttpClient will be used in
conjunction with authentication.
|
static String |
getUserAgent()
Gets the User-Agent string that ARQ is applying to all HTTP requests
|
static String |
readPayload(org.apache.http.HttpEntity entity) |
static void |
setDefaultAuthenticator(HttpAuthenticator authenticator)
Sets the default authenticator used for authenticate requests if no
specific authenticator is provided.
|
static void |
setDefaultHttpClient(org.apache.http.client.HttpClient httpClient)
Performance can be improved by using a shared HttpClient that uses
connection pooling.
|
static void |
setUseDefaultClientWithAuthentication(boolean useWithAuth)
Sets whether the configured default HttpClient will be used in
conjunction with authentication.
|
static void |
setUserAgent(String userAgent)
Sets the User-Agent string that ARQ will apply to all HTTP requests
|
public static final String ARQ_USER_AGENT
public static HttpAuthenticator getDefaultAuthenticator()
public static void setDefaultAuthenticator(HttpAuthenticator authenticator)
authenticator
- Authenticatorpublic static org.apache.http.client.HttpClient getDefaultHttpClient()
HttpClient
. This may be null, meaning
a new HttpClient
is created each time, if none is provided
in the HttpOp function call.public static void setDefaultHttpClient(org.apache.http.client.HttpClient httpClient)
Performance can be improved by using a shared HttpClient that uses connection pooling. However, pool management is complicated and can lead to starvation (the system locks-up, especially on Java6; it's JVM sensitive).
The default HttpClient is not used if an HttpAuthenticator is provided since this can potentially leak authentication credentials to be leaked between requests to different services. However in some cases it may be valuable to share the client regardless in which case you can also set the
Set to "null" to create a new HttpClient for each call (default behaviour, more reliable, but slower when many HTTP operation are attempted).
See the Apache Http Client documentation for more details.
httpClient
- HTTP Clientpublic static boolean getUseDefaultClientWithAuthentication()
This defaults to false because this can potentially leak authentication credentials between requests to different services. However in some cases it may be valuable to share the client regardless e.g. when you have a specially configured HttpClient for your environment.
public static void setUseDefaultClientWithAuthentication(boolean useWithAuth)
This defaults to false because this can potentially leak authentication credentials between requests to different services. However in some cases it may be valuable to share the client regardless e.g. when you have a specially configured HttpClient for your environment.
useWithAuth
- True if the configured default client should be used with
authentication, false if it should notpublic static org.apache.http.client.HttpClient createCachingHttpClient()
setDefaultHttpClient(org.apache.http.client.HttpClient)
or provided in the HttpOp calls.public static String getUserAgent()
public static void setUserAgent(String userAgent)
userAgent
- User-Agent stringpublic static void execHttpGet(String url, String acceptHeader, HttpResponseHandler handler)
HTTP responses 400 and 500 become exceptions.
url
- URLacceptHeader
- Accept Headerhandler
- Response Handlerpublic static void execHttpGet(String url, String acceptHeader, HttpResponseHandler handler, HttpAuthenticator authenticator)
HTTP responses 400 and 500 become exceptions.
url
- URLacceptHeader
- Accept Headerhandler
- Response Handlerauthenticator
- HTTP Authenticatorpublic static void execHttpGet(String url, String acceptHeader, HttpResponseHandler handler, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
The acceptHeader string is any legal value for HTTP Accept: field.
The handlers are the set of content types (without charset), used to dispatch the response body for handling.
HTTP responses 400 and 500 become exceptions.
url
- URLacceptHeader
- Accept Headerhandler
- Response handler called to process the responsehttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static TypedInputStream execHttpGet(String url)
The acceptHeader string is any legal value for HTTP Accept: field.
url
- URLpublic static TypedInputStream execHttpGet(String url, String acceptHeader)
The acceptHeader string is any legal value for HTTP Accept: field.
url
- URLacceptHeader
- Accept Headerpublic static TypedInputStream execHttpGet(String url, String acceptHeader, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
A 404 will result in a null stream being returned, any other error code results in an exception.
url
- URLacceptHeader
- Accept HeaderhttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static String execHttpGetString(String url)
url
- URLpublic static String execHttpGetString(String url, String acceptHeader)
url
- URLacceptHeader
- Accept header.public static void execHttpPost(String url, String contentType, String content)
url
- URLcontentType
- Content Type to POSTcontent
- Content to POSTpublic static void execHttpPost(String url, String contentType, String content, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
url
- URLcontentType
- Content Type to POSTcontent
- Content to POSThttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static void execHttpPost(String url, String contentType, String content, String acceptType, HttpResponseHandler handler, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
url
- URLcontentType
- Content Type to POSTcontent
- Content to POSTacceptType
- Accept Typehandler
- Response handler called to process the responsehttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static void execHttpPost(String url, String contentType, InputStream input, long length)
url
- URLcontentType
- Content Type to POSTinput
- Input Stream to POST fromlength
- Amount of content to POSTpublic static void execHttpPost(String url, String contentType, InputStream input, long length, String acceptType, HttpResponseHandler handler)
The input stream is assumed to be UTF-8.
url
- URLcontentType
- Content Type to POSTinput
- Input Stream to POST content fromlength
- Length of content to POSTacceptType
- Accept Typehandler
- Response handler called to process the responsepublic static void execHttpPost(String url, String contentType, InputStream input, long length, String acceptType, HttpResponseHandler handler, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
The input stream is assumed to be UTF-8.
url
- URLcontentType
- Content Type to POSTinput
- Input Stream to POST content fromlength
- Length of content to POSTacceptType
- Accept Typehandler
- Response handler called to process the responsehttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static void execHttpPost(String url, org.apache.http.HttpEntity entity)
url
- URLentity
- Entity to POSTpublic static void execHttpPost(String url, org.apache.http.HttpEntity entity, String acceptString, HttpResponseHandler handler)
url
- URLentity
- Entity to POSTacceptString
- Accept Headerhandler
- Response Handlerpublic static void execHttpPost(String url, org.apache.http.HttpEntity entity, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
The content for the POST body comes from the HttpEntity.
Additional headers e.g. for authentication can be injected through an
HttpContext
url
- URLentity
- Entity to POSThttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static void execHttpPost(String url, org.apache.http.HttpEntity entity, String acceptHeader, HttpResponseHandler handler, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
The content for the POST body comes from the HttpEntity.
Additional headers e.g. for authentication can be injected through an
HttpContext
url
- URLentity
- Entity to POSTacceptHeader
- Accept Headerhandler
- Response handler called to process the responsehttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static TypedInputStream execHttpPostFormStream(String url, Params params, String acceptHeader)
url
- URLparams
- Parameters to POSTacceptHeader
- public static void execHttpPostForm(String url, Params params)
url
- URLparams
- Parameters to POSTpublic static TypedInputStream execHttpPostFormStream(String url, Params params, String acceptHeader, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
The acceptHeader string is any legal value for HTTP Accept: field.
A 404 will result in a null stream being returned, any other error code results in an exception.
url
- URLacceptHeader
- Accept Headerparams
- Parameters to POSThttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static void execHttpPostForm(String url, Params params, String acceptString, HttpResponseHandler handler)
url
- URLparams
- Form parameters to POSTacceptString
- Accept Headerhandler
- Response handler called to process the responsepublic static void execHttpPostForm(String url, Params params, String acceptHeader, HttpResponseHandler handler, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
url
- URLparams
- Form parameters to POSTacceptHeader
- Accept Headerhandler
- Response handler called to process the responsehttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static void execHttpPut(String url, String contentType, String content)
url
- URLcontentType
- Content Type for the PUTcontent
- Content for the PUTpublic static void execHttpPut(String url, String contentType, String content, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
url
- URLcontentType
- Content Type for the PUTcontent
- Content for the PUThttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static void execHttpPut(String url, String contentType, InputStream input, long length)
url
- URLcontentType
- Content Type for the PUTinput
- Input Stream to read PUT content fromlength
- Amount of content to PUTpublic static void execHttpPut(String url, String contentType, InputStream input, long length, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
url
- URLcontentType
- Content Type for the PUTinput
- Input Stream to read PUT content fromlength
- Amount of content to PUThttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static void execHttpPut(String url, org.apache.http.HttpEntity entity)
url
- URLentity
- HTTP Entity to PUTpublic static void execHttpPut(String url, org.apache.http.HttpEntity entity, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
url
- URLentity
- HTTP Entity to PUThttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static void execHttpHead(String url)
url
- URLpublic static void execHttpHead(String url, String acceptString, HttpResponseHandler handler)
url
- URLacceptString
- Accept Headerhandler
- Response Handlerpublic static void execHttpHead(String url, String acceptString, HttpResponseHandler handler, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
url
- URLacceptString
- Accept Headerhandler
- Response HandlerhttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static void execHttpDelete(String url)
url
- URLpublic static void execHttpDelete(String url, HttpResponseHandler handler)
url
- URLhandler
- Response Handlerpublic static void execHttpDelete(String url, HttpResponseHandler handler, org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext, HttpAuthenticator authenticator)
url
- URLhandler
- Response HandlerhttpClient
- HTTP ClienthttpContext
- HTTP Contextauthenticator
- HTTP Authenticatorpublic static String readPayload(org.apache.http.HttpEntity entity) throws IOException
IOException
public static void applyUserAgent(org.apache.http.HttpMessage message)
message
- HTTP requestpublic static void applyAuthentication(org.apache.http.impl.client.AbstractHttpClient client, String target, org.apache.http.protocol.HttpContext context, HttpAuthenticator authenticator)
If a null authenticator is provided this method tries to use the
registered default authenticator which may be set via the
setDefaultAuthenticator(HttpAuthenticator)
method.
client
- HTTP Clienttarget
- Target URIcontext
- HTTP Contextauthenticator
- HTTP AuthenticatorLicenced under the Apache License, Version 2.0