public class CommonsHttpInvokerRequestExecutor extends AbstractHttpInvokerRequestExecutor
HttpInvokerRequestExecutor
implementation that uses
Jakarta Commons HttpClient
to execute POST requests. Requires Commons HttpClient 3.0 or higher.
Allows to use a pre-configured org.apache.commons.httpclient.HttpClient
instance, potentially with authentication, HTTP connection pooling, etc.
Also designed for easy subclassing, providing specific template methods.
SimpleHttpInvokerRequestExecutor
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_READ_TIMEOUT_MILLISECONDS
Default timeout value if no HttpClient is explicitly provided.
|
private HttpClient |
httpClient |
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 |
---|
CommonsHttpInvokerRequestExecutor()
Create a new CommonsHttpInvokerRequestExecutor with a default
HttpClient that uses a default MultiThreadedHttpConnectionManager.
|
CommonsHttpInvokerRequestExecutor(HttpClient httpClient)
Create a new CommonsHttpInvokerRequestExecutor with the given
HttpClient instance.
|
Modifier and Type | Method and Description |
---|---|
protected PostMethod |
createPostMethod(HttpInvokerClientConfiguration config)
Create a PostMethod for the given configuration.
|
protected RemoteInvocationResult |
doExecuteRequest(HttpInvokerClientConfiguration config,
java.io.ByteArrayOutputStream baos)
Execute the given request through Commons HttpClient.
|
protected void |
executePostMethod(HttpInvokerClientConfiguration config,
HttpClient httpClient,
PostMethod postMethod)
Execute the given PostMethod instance.
|
HttpClient |
getHttpClient()
Return the HttpClient instance that this request executor uses.
|
protected java.io.InputStream |
getResponseBody(HttpInvokerClientConfiguration config,
PostMethod postMethod)
Extract the response body from the given executed remote invocation request.
|
protected boolean |
isGzipResponse(PostMethod postMethod)
Determine whether the given response indicates a GZIP response.
|
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,
PostMethod postMethod,
java.io.ByteArrayOutputStream baos)
Set the given serialized remote invocation as request body.
|
protected void |
validateResponse(HttpInvokerClientConfiguration config,
PostMethod postMethod)
Validate the given response as contained in the PostMethod 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_READ_TIMEOUT_MILLISECONDS
private HttpClient httpClient
public CommonsHttpInvokerRequestExecutor()
DEFAULT_READ_TIMEOUT_MILLISECONDS
.org.apache.commons.httpclient.HttpClient
,
org.apache.commons.httpclient.MultiThreadedHttpConnectionManager
public CommonsHttpInvokerRequestExecutor(HttpClient httpClient)
httpClient
- the HttpClient instance to use for this request executorpublic void setHttpClient(HttpClient httpClient)
public HttpClient getHttpClient()
public void setConnectTimeout(int timeout)
timeout
- the timeout value in millisecondsorg.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout(int)
public void setReadTimeout(int timeout)
timeout
- the timeout value in millisecondsorg.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int)
,
DEFAULT_READ_TIMEOUT_MILLISECONDS
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 deserializationcreatePostMethod(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration)
,
setRequestBody(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, PostMethod, java.io.ByteArrayOutputStream)
,
executePostMethod(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, HttpClient, PostMethod)
,
validateResponse(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, PostMethod)
,
getResponseBody(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, PostMethod)
protected PostMethod createPostMethod(HttpInvokerClientConfiguration config) throws java.io.IOException
The default implementation creates a standard PostMethod 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 void setRequestBody(HttpInvokerClientConfiguration config, PostMethod postMethod, java.io.ByteArrayOutputStream baos) throws java.io.IOException
The default implementation simply sets the serialized invocation as the PostMethod'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 servicepostMethod
- the PostMethod to set the request body onbaos
- the ByteArrayOutputStream that contains the serialized
RemoteInvocation objectjava.io.IOException
- if thrown by I/O methodsorg.apache.commons.httpclient.methods.PostMethod#setRequestBody(java.io.InputStream)
,
org.apache.commons.httpclient.methods.PostMethod#setRequestEntity
,
org.apache.commons.httpclient.methods.InputStreamRequestEntity
protected void executePostMethod(HttpInvokerClientConfiguration config, HttpClient httpClient, PostMethod postMethod) throws java.io.IOException
config
- the HTTP invoker configuration that specifies the target servicehttpClient
- the HttpClient to execute onpostMethod
- the PostMethod to executejava.io.IOException
- if thrown by I/O methodsorg.apache.commons.httpclient.HttpClient#executeMethod(org.apache.commons.httpclient.HttpMethod)
protected void validateResponse(HttpInvokerClientConfiguration config, PostMethod postMethod) 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 servicepostMethod
- the executed PostMethod to validatejava.io.IOException
- if validation failedorg.apache.commons.httpclient.methods.PostMethod#getStatusCode()
,
org.apache.commons.httpclient.HttpException
protected java.io.InputStream getResponseBody(HttpInvokerClientConfiguration config, PostMethod postMethod) throws java.io.IOException
The default implementation simply fetches the PostMethod'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 servicepostMethod
- the PostMethod to read the response body fromjava.io.IOException
- if thrown by I/O methodsisGzipResponse(PostMethod)
,
GZIPInputStream
,
org.apache.commons.httpclient.methods.PostMethod#getResponseBodyAsStream()
,
org.apache.commons.httpclient.methods.PostMethod#getResponseHeader(String)
protected boolean isGzipResponse(PostMethod postMethod)
The default implementation checks whether the HTTP "Content-Encoding" header contains "gzip" (in any casing).
postMethod
- the PostMethod to check