public class

CommonsHttpInvokerRequestExecutor

extends AbstractHttpInvokerRequestExecutor
java.lang.Object
   ↳ org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor
     ↳ org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor

Class Overview

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.

Summary

[Expand]
Inherited Constants
From class org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor
[Expand]
Inherited Fields
From class org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor
Public Constructors
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.
Public Methods
HttpClient getHttpClient()
Return the HttpClient instance that this request executor uses.
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 Methods
PostMethod createPostMethod(HttpInvokerClientConfiguration config)
Create a PostMethod for the given configuration.
RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)
Execute the given request through Commons HttpClient.
void executePostMethod(HttpInvokerClientConfiguration config, HttpClient httpClient, PostMethod postMethod)
Execute the given PostMethod instance.
InputStream getResponseBody(HttpInvokerClientConfiguration config, PostMethod postMethod)
Extract the response body from the given executed remote invocation request.
boolean isGzipResponse(PostMethod postMethod)
Determine whether the given response indicates a GZIP response.
void setRequestBody(HttpInvokerClientConfiguration config, PostMethod postMethod, ByteArrayOutputStream baos)
Set the given serialized remote invocation as request body.
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.
[Expand]
Inherited Methods
From class org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor
From class java.lang.Object
From interface org.springframework.beans.factory.BeanClassLoaderAware
From interface org.springframework.remoting.httpinvoker.HttpInvokerRequestExecutor

Public Constructors

public CommonsHttpInvokerRequestExecutor ()

Create a new CommonsHttpInvokerRequestExecutor with a default HttpClient that uses a default MultiThreadedHttpConnectionManager. Sets the socket read timeout to #DEFAULT_READ_TIMEOUT_MILLISECONDS.

See Also
  • org.apache.commons.httpclient.HttpClient
  • org.apache.commons.httpclient.MultiThreadedHttpConnectionManager

public CommonsHttpInvokerRequestExecutor (HttpClient httpClient)

Create a new CommonsHttpInvokerRequestExecutor with the given HttpClient instance. The socket read timeout of the provided HttpClient will not be changed.

Parameters
httpClient the HttpClient instance to use for this request executor

Public Methods

public HttpClient getHttpClient ()

Return the HttpClient instance that this request executor uses.

public void setHttpClient (HttpClient httpClient)

Set the HttpClient instance to use for this request executor.

public void setReadTimeout (int timeout)

Set the socket read timeout for the underlying HttpClient. A value of 0 means never timeout.

Parameters
timeout the timeout value in milliseconds
See Also
  • org.apache.commons.httpclient.params.HttpConnectionManagerParams#setSoTimeout(int)
  • #DEFAULT_READ_TIMEOUT_MILLISECONDS

Protected Methods

protected PostMethod createPostMethod (HttpInvokerClientConfiguration config)

Create a PostMethod for the given configuration.

The default implementation creates a standard PostMethod with "application/x-java-serialized-object" as "Content-Type" header.

Parameters
config the HTTP invoker configuration that specifies the target service
Returns
  • the PostMethod instance
Throws
IOException if thrown by I/O methods

protected RemoteInvocationResult doExecuteRequest (HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)

Execute the given request through Commons HttpClient.

This method implements the basic processing workflow: The actual work happens in this class's template methods.

Parameters
config the HTTP invoker configuration that specifies the target service
baos the ByteArrayOutputStream that contains the serialized RemoteInvocation object
Returns
  • the RemoteInvocationResult object

protected void executePostMethod (HttpInvokerClientConfiguration config, HttpClient httpClient, PostMethod postMethod)

Execute the given PostMethod instance.

Parameters
config the HTTP invoker configuration that specifies the target service
httpClient the HttpClient to execute on
postMethod the PostMethod to execute
Throws
IOException if thrown by I/O methods
See Also
  • org.apache.commons.httpclient.HttpClient#executeMethod(org.apache.commons.httpclient.HttpMethod)

protected InputStream getResponseBody (HttpInvokerClientConfiguration config, PostMethod postMethod)

Extract the response body from the given executed remote invocation request.

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.

Parameters
config the HTTP invoker configuration that specifies the target service
postMethod the PostMethod to read the response body from
Returns
  • an InputStream for the response body
Throws
IOException if thrown by I/O methods
See Also
  • isGzipResponse(PostMethod)
  • java.util.zip.GZIPInputStream
  • org.apache.commons.httpclient.methods.PostMethod#getResponseBodyAsStream()
  • org.apache.commons.httpclient.methods.PostMethod#getResponseHeader(String)

protected boolean isGzipResponse (PostMethod postMethod)

Determine whether the given response indicates a GZIP response.

Default implementation checks whether the HTTP "Content-Encoding" header contains "gzip" (in any casing).

Parameters
postMethod the PostMethod to check
Returns
  • whether the given response indicates a GZIP response

protected void setRequestBody (HttpInvokerClientConfiguration config, PostMethod postMethod, ByteArrayOutputStream baos)

Set the given serialized remote invocation as request body.

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 potentially set appropriate HTTP request headers.

Parameters
config the HTTP invoker configuration that specifies the target service
postMethod the PostMethod to set the request body on
baos the ByteArrayOutputStream that contains the serialized RemoteInvocation object
Throws
IOException if thrown by I/O methods
See Also
  • org.apache.commons.httpclient.methods.PostMethod#setRequestBody(java.io.InputStream)
  • org.apache.commons.httpclient.methods.PostMethod#setRequestEntity
  • org.apache.commons.httpclient.methods.InputStreamRequestEntity

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.

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 service
postMethod the executed PostMethod to validate
Throws
IOException if validation failed
See Also
  • org.apache.commons.httpclient.methods.PostMethod#getStatusCode()
  • org.apache.commons.httpclient.HttpException