Class HttpComponentsHttpInvokerRequestExecutor
- java.lang.Object
- org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor
- org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor
- All Implemented Interfaces:
Aware
,BeanClassLoaderAware
,HttpInvokerRequestExecutor
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.
- Since:
- 3.1
- Author:
- Juergen Hoeller, Stephane Nicoll
- See Also:
SimpleHttpInvokerRequestExecutor
Field Summary
Fields inherited from class org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor
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 Summary
Constructors Constructor Description HttpComponentsHttpInvokerRequestExecutor()
Create a new instance of the HttpComponentsHttpInvokerRequestExecutor with a defaultHttpClient
that uses a defaultorg.apache.http.impl.conn.PoolingClientConnectionManager
.HttpComponentsHttpInvokerRequestExecutor(HttpClient httpClient)
Create a new instance of the HttpComponentsClientHttpRequestFactory with the givenHttpClient
instance.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected HttpPost
createHttpPost(HttpInvokerClientConfiguration config)
Create a HttpPost for the given configuration.protected RequestConfig
createRequestConfig(HttpInvokerClientConfiguration config)
Create aRequestConfig
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 theHttpClient
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 theHttpClient
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.Methods inherited from class org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor
createObjectInputStream, decorateInputStream, decorateOutputStream, doReadRemoteInvocationResult, doWriteRemoteInvocation, executeRequest, getBeanClassLoader, getByteArrayOutputStream, getContentType, isAcceptGzipEncoding, readRemoteInvocationResult, setAcceptGzipEncoding, setBeanClassLoader, setContentType, writeRemoteInvocation
Constructor Detail
HttpComponentsHttpInvokerRequestExecutor
public HttpComponentsHttpInvokerRequestExecutor()
Create a new instance of the HttpComponentsHttpInvokerRequestExecutor with a defaultHttpClient
that uses a defaultorg.apache.http.impl.conn.PoolingClientConnectionManager
.
HttpComponentsHttpInvokerRequestExecutor
public HttpComponentsHttpInvokerRequestExecutor(HttpClient httpClient)
Create a new instance of the HttpComponentsClientHttpRequestFactory with the givenHttpClient
instance.- Parameters:
httpClient
- the HttpClient instance to use for this request executor
Method Detail
setHttpClient
public void setHttpClient(HttpClient httpClient)
Set theHttpClient
instance to use for this request executor.
getHttpClient
public HttpClient getHttpClient()
Return theHttpClient
instance that this request executor uses.
setConnectTimeout
public void setConnectTimeout(int timeout)
Set the connection timeout for the underlying HttpClient. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfig
instance on a customHttpClient
.- Parameters:
timeout
- the timeout value in milliseconds- See Also:
RequestConfig.getConnectTimeout()
setConnectionRequestTimeout
public void setConnectionRequestTimeout(int connectionRequestTimeout)
Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying HttpClient. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfig
instance on a customHttpClient
.- Parameters:
connectionRequestTimeout
- the timeout value to request a connection in milliseconds- See Also:
RequestConfig.getConnectionRequestTimeout()
setReadTimeout
public void setReadTimeout(int timeout)
Set the socket read timeout for the underlying HttpClient. A timeout value of 0 specifies an infinite timeout.Additional properties can be configured by specifying a
RequestConfig
instance on a customHttpClient
.- Parameters:
timeout
- the timeout value in milliseconds- See Also:
DEFAULT_READ_TIMEOUT_MILLISECONDS
,RequestConfig.getSocketTimeout()
doExecuteRequest
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws IOException, ClassNotFoundException
Execute the given request through the HttpClient.This method implements the basic processing workflow: The actual work happens in this class's template methods.
- Specified by:
doExecuteRequest
in classAbstractHttpInvokerRequestExecutor
- Parameters:
config
- the HTTP invoker configuration that specifies the target servicebaos
- the ByteArrayOutputStream that contains the serialized RemoteInvocation object- Returns:
- the RemoteInvocationResult object
- Throws:
IOException
- if thrown by I/O operationsClassNotFoundException
- if thrown during deserialization- See Also:
createHttpPost(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)
createHttpPost
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException
Create a HttpPost for the given configuration.The default implementation creates a standard HttpPost with "application/x-java-serialized-object" as "Content-Type" header.
- Parameters:
config
- the HTTP invoker configuration that specifies the target service- Returns:
- the HttpPost instance
- Throws:
IOException
- if thrown by I/O methods
createRequestConfig
protected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config)
Create aRequestConfig
for the given configuration. Can returnnull
to indicate that no custom request config should be set and the defaults of theHttpClient
should be used.The default implementation tries to merge the defaults of the client with the local customizations of the instance, if any.
- Parameters:
config
- the HTTP invoker configuration that specifies the target service- Returns:
- the RequestConfig to use
setRequestBody
protected void setRequestBody(HttpInvokerClientConfiguration config, HttpPost httpPost, ByteArrayOutputStream baos) throws IOException
Set the given serialized remote invocation as request body.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.
- Parameters:
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 object- Throws:
IOException
- if thrown by I/O methods
executeHttpPost
protected org.apache.http.HttpResponse executeHttpPost(HttpInvokerClientConfiguration config, HttpClient httpClient, HttpPost httpPost) throws IOException
Execute the given HttpPost instance.- Parameters:
config
- the HTTP invoker configuration that specifies the target servicehttpClient
- the HttpClient to execute onhttpPost
- the HttpPost to execute- Returns:
- the resulting HttpResponse
- Throws:
IOException
- if thrown by I/O methods
validateResponse
protected void validateResponse(HttpInvokerClientConfiguration config, org.apache.http.HttpResponse response) throws IOException
Validate the given response as contained in the HttpPost object, throwing an exception if it does not correspond to a successful HTTP response.Default implementation rejects any HTTP status code beyond 2xx, to avoid parsing the response body and trying to deserialize from a corrupted stream.
- Parameters:
config
- the HTTP invoker configuration that specifies the target serviceresponse
- the resulting HttpResponse to validate- Throws:
IOException
- if validation failed
getResponseBody
protected InputStream getResponseBody(HttpInvokerClientConfiguration config, org.apache.http.HttpResponse httpResponse) throws IOException
Extract the response body from the given executed remote invocation request.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.
- Parameters:
config
- the HTTP invoker configuration that specifies the target servicehttpResponse
- the resulting HttpResponse to read the response body from- Returns:
- an InputStream for the response body
- Throws:
IOException
- if thrown by I/O methods- See Also:
isGzipResponse(org.apache.http.HttpResponse)
,GZIPInputStream
isGzipResponse
protected boolean isGzipResponse(org.apache.http.HttpResponse httpResponse)
Determine whether the given response indicates a GZIP response.The default implementation checks whether the HTTP "Content-Encoding" header contains "gzip" (in any casing).
- Parameters:
httpResponse
- the resulting HttpResponse to check- Returns:
- whether the given response indicates a GZIP response