public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvokerRequestExecutor
HttpInvokerRequestExecutor
implementation that uses
Apache HttpComponents HttpClient
to execute POST requests.
Allows to use a pre-configured 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
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
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. |
Modifier and Type | Method and Description |
---|---|
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,
ByteArrayOutputStream baos)
Execute the given request through the HttpClient.
|
protected org.apache.http.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 InputStream |
getResponseBody(HttpInvokerClientConfiguration config,
org.apache.http.HttpResponse httpResponse)
Extract the response body from the given executed remote invocation request.
|
protected boolean |
isGzipResponse(org.apache.http.HttpResponse httpResponse)
Determine whether the given response indicates a GZIP response.
|
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,
ByteArrayOutputStream baos)
Set the given serialized remote invocation as request body.
|
protected void |
validateResponse(HttpInvokerClientConfiguration config,
org.apache.http.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
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 executorpublic 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()
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws IOException, 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 objectIOException
- if thrown by I/O operationsClassNotFoundException
- if thrown during deserializationcreateHttpPost(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration)
,
setRequestBody(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, org.apache.http.client.methods.HttpPost, java.io.ByteArrayOutputStream)
,
executeHttpPost(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, org.apache.http.client.HttpClient, org.apache.http.client.methods.HttpPost)
,
validateResponse(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, org.apache.http.HttpResponse)
,
getResponseBody(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, org.apache.http.HttpResponse)
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws 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 serviceIOException
- 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 serviceprotected void setRequestBody(HttpInvokerClientConfiguration config, HttpPost httpPost, ByteArrayOutputStream baos) throws 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 objectIOException
- if thrown by I/O methodsprotected org.apache.http.HttpResponse executeHttpPost(HttpInvokerClientConfiguration config, HttpClient httpClient, HttpPost httpPost) throws IOException
config
- the HTTP invoker configuration that specifies the target servicehttpClient
- the HttpClient to execute onhttpPost
- the HttpPost to executeIOException
- if thrown by I/O methodsprotected void validateResponse(HttpInvokerClientConfiguration config, org.apache.http.HttpResponse response) throws 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 validateIOException
- if validation failedprotected InputStream getResponseBody(HttpInvokerClientConfiguration config, org.apache.http.HttpResponse httpResponse) throws 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 fromIOException
- if thrown by I/O methodsisGzipResponse(org.apache.http.HttpResponse)
,
GZIPInputStream
protected boolean isGzipResponse(org.apache.http.HttpResponse httpResponse)
The default implementation checks whether the HTTP "Content-Encoding" header contains "gzip" (in any casing).
httpResponse
- the resulting HttpResponse to check