public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvokerRequestExecutor
HttpInvokerRequestExecutor
implementation that uses
Apache HttpComponents HttpClient
to execute POST requests.
Allows to use a pre-configured org.apache.http.client.HttpClient
instance, potentially with authentication, HTTP connection pooling, etc.
Also designed for easy subclassing, providing specific template methods.
As of Spring 4.1, this request executor requires Apache HttpComponents 4.3 or higher.
SimpleHttpInvokerRequestExecutor
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_MAX_CONNECTIONS_PER_ROUTE |
private static int |
DEFAULT_MAX_TOTAL_CONNECTIONS |
private static int |
DEFAULT_READ_TIMEOUT_MILLISECONDS |
private HttpClient |
httpClient |
private RequestConfig |
requestConfig |
CONTENT_TYPE_SERIALIZED_OBJECT, ENCODING_GZIP, HTTP_HEADER_ACCEPT_ENCODING, HTTP_HEADER_ACCEPT_LANGUAGE, HTTP_HEADER_CONTENT_ENCODING, HTTP_HEADER_CONTENT_LENGTH, HTTP_HEADER_CONTENT_TYPE, HTTP_METHOD_POST, logger
Modifier | Constructor and Description |
---|---|
|
HttpComponentsHttpInvokerRequestExecutor()
Create a new instance of the HttpComponentsHttpInvokerRequestExecutor with a default
HttpClient that uses a default org.apache.http.impl.conn.PoolingClientConnectionManager . |
|
HttpComponentsHttpInvokerRequestExecutor(HttpClient httpClient)
Create a new instance of the HttpComponentsClientHttpRequestFactory
with the given
HttpClient instance. |
private |
HttpComponentsHttpInvokerRequestExecutor(HttpClient httpClient,
RequestConfig requestConfig) |
Modifier and Type | Method and Description |
---|---|
private RequestConfig.Builder |
cloneRequestConfig() |
private static HttpClient |
createDefaultHttpClient() |
protected HttpPost |
createHttpPost(HttpInvokerClientConfiguration config)
Create a HttpPost for the given configuration.
|
protected RequestConfig |
createRequestConfig(HttpInvokerClientConfiguration config)
Create a
RequestConfig for the given configuration. |
protected RemoteInvocationResult |
doExecuteRequest(HttpInvokerClientConfiguration config,
java.io.ByteArrayOutputStream baos)
Execute the given request through the HttpClient.
|
protected HttpResponse |
executeHttpPost(HttpInvokerClientConfiguration config,
HttpClient httpClient,
HttpPost httpPost)
Execute the given HttpPost instance.
|
HttpClient |
getHttpClient()
Return the
HttpClient instance that this request executor uses. |
protected java.io.InputStream |
getResponseBody(HttpInvokerClientConfiguration config,
HttpResponse httpResponse)
Extract the response body from the given executed remote invocation request.
|
protected boolean |
isGzipResponse(HttpResponse httpResponse)
Determine whether the given response indicates a GZIP response.
|
private RequestConfig |
mergeRequestConfig(RequestConfig defaultRequestConfig) |
void |
setConnectionRequestTimeout(int connectionRequestTimeout)
Set the timeout in milliseconds used when requesting a connection from the connection
manager using the underlying HttpClient.
|
void |
setConnectTimeout(int timeout)
Set the connection timeout for the underlying HttpClient.
|
void |
setHttpClient(HttpClient httpClient)
Set the
HttpClient instance to use for this request executor. |
void |
setReadTimeout(int timeout)
Set the socket read timeout for the underlying HttpClient.
|
protected void |
setRequestBody(HttpInvokerClientConfiguration config,
HttpPost httpPost,
java.io.ByteArrayOutputStream baos)
Set the given serialized remote invocation as request body.
|
protected void |
validateResponse(HttpInvokerClientConfiguration config,
HttpResponse response)
Validate the given response as contained in the HttpPost object,
throwing an exception if it does not correspond to a successful HTTP response.
|
createObjectInputStream, decorateInputStream, decorateOutputStream, doReadRemoteInvocationResult, doWriteRemoteInvocation, executeRequest, getBeanClassLoader, getByteArrayOutputStream, getContentType, isAcceptGzipEncoding, readRemoteInvocationResult, setAcceptGzipEncoding, setBeanClassLoader, setContentType, writeRemoteInvocation
private static final int DEFAULT_MAX_TOTAL_CONNECTIONS
private static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE
private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS
private HttpClient httpClient
private RequestConfig requestConfig
public HttpComponentsHttpInvokerRequestExecutor()
HttpClient
that uses a default org.apache.http.impl.conn.PoolingClientConnectionManager
.public HttpComponentsHttpInvokerRequestExecutor(HttpClient httpClient)
HttpClient
instance.httpClient
- the HttpClient instance to use for this request executorprivate HttpComponentsHttpInvokerRequestExecutor(HttpClient httpClient, RequestConfig requestConfig)
private static HttpClient createDefaultHttpClient()
public void setHttpClient(HttpClient httpClient)
HttpClient
instance to use for this request executor.public HttpClient getHttpClient()
HttpClient
instance that this request executor uses.public void setConnectTimeout(int timeout)
Additional properties can be configured by specifying a
RequestConfig
instance on a custom HttpClient
.
timeout
- the timeout value in millisecondsRequestConfig#getConnectTimeout()
public void setConnectionRequestTimeout(int connectionRequestTimeout)
Additional properties can be configured by specifying a
RequestConfig
instance on a custom HttpClient
.
connectionRequestTimeout
- the timeout value to request a connection in millisecondsRequestConfig#getConnectionRequestTimeout()
public void setReadTimeout(int timeout)
Additional properties can be configured by specifying a
RequestConfig
instance on a custom HttpClient
.
timeout
- the timeout value in millisecondsDEFAULT_READ_TIMEOUT_MILLISECONDS
,
RequestConfig#getSocketTimeout()
private RequestConfig.Builder cloneRequestConfig()
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, java.io.ByteArrayOutputStream baos) throws java.io.IOException, java.lang.ClassNotFoundException
This method implements the basic processing workflow: The actual work happens in this class's template methods.
doExecuteRequest
in class AbstractHttpInvokerRequestExecutor
config
- the HTTP invoker configuration that specifies the
target servicebaos
- the ByteArrayOutputStream that contains the serialized
RemoteInvocation objectjava.io.IOException
- if thrown by I/O operationsjava.lang.ClassNotFoundException
- if thrown during deserializationcreateHttpPost(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration)
,
setRequestBody(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, HttpPost, java.io.ByteArrayOutputStream)
,
executeHttpPost(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, HttpClient, HttpPost)
,
validateResponse(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, HttpResponse)
,
getResponseBody(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, HttpResponse)
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws java.io.IOException
The default implementation creates a standard HttpPost with "application/x-java-serialized-object" as "Content-Type" header.
config
- the HTTP invoker configuration that specifies the
target servicejava.io.IOException
- if thrown by I/O methodsprotected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config)
RequestConfig
for the given configuration. Can return null
to indicate that no custom request config should be set and the defaults of the
HttpClient
should be used.
The default implementation tries to merge the defaults of the client with the local customizations of the instance, if any.
config
- the HTTP invoker configuration that specifies the
target serviceprivate RequestConfig mergeRequestConfig(RequestConfig defaultRequestConfig)
protected void setRequestBody(HttpInvokerClientConfiguration config, HttpPost httpPost, java.io.ByteArrayOutputStream baos) throws java.io.IOException
The default implementation simply sets the serialized invocation as the HttpPost's request body. This can be overridden, for example, to write a specific encoding and to potentially set appropriate HTTP request headers.
config
- the HTTP invoker configuration that specifies the target servicehttpPost
- the HttpPost to set the request body onbaos
- the ByteArrayOutputStream that contains the serialized
RemoteInvocation objectjava.io.IOException
- if thrown by I/O methodsprotected HttpResponse executeHttpPost(HttpInvokerClientConfiguration config, HttpClient httpClient, HttpPost httpPost) throws java.io.IOException
config
- the HTTP invoker configuration that specifies the target servicehttpClient
- the HttpClient to execute onhttpPost
- the HttpPost to executejava.io.IOException
- if thrown by I/O methodsprotected void validateResponse(HttpInvokerClientConfiguration config, HttpResponse response) throws java.io.IOException
Default implementation rejects any HTTP status code beyond 2xx, to avoid parsing the response body and trying to deserialize from a corrupted stream.
config
- the HTTP invoker configuration that specifies the target serviceresponse
- the resulting HttpResponse to validatejava.io.IOException
- if validation failedprotected java.io.InputStream getResponseBody(HttpInvokerClientConfiguration config, HttpResponse httpResponse) throws java.io.IOException
The default implementation simply fetches the HttpPost's response body stream. If the response is recognized as GZIP response, the InputStream will get wrapped in a GZIPInputStream.
config
- the HTTP invoker configuration that specifies the target servicehttpResponse
- the resulting HttpResponse to read the response body fromjava.io.IOException
- if thrown by I/O methodsisGzipResponse(HttpResponse)
,
GZIPInputStream
protected boolean isGzipResponse(HttpResponse httpResponse)
The default implementation checks whether the HTTP "Content-Encoding" header contains "gzip" (in any casing).
httpResponse
- the resulting HttpResponse to check