SimpleHttpInvokerRequestExecutor (Spring Framework 3.2.1.RELEASE API)
Date: 2013-01-24

org.springframework.remoting.httpinvoker

## Class SimpleHttpInvokerRequestExecutor

- java.lang.Object
- - org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor
  - - org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor

- All Implemented Interfaces:
    Aware, BeanClassLoaderAware, HttpInvokerRequestExecutor

      public class SimpleHttpInvokerRequestExecutor
      extends AbstractHttpInvokerRequestExecutor

  HttpInvokerRequestExecutor implementation that uses standard J2SE facilities to execute POST requests, without support for HTTP authentication or advanced configuration options.

  Designed for easy subclassing, customizing specific template methods. However, consider CommonsHttpInvokerRequestExecutor for more sophisticated needs: The J2SE HttpURLConnection is rather limited in its capabilities.

  Since:
    1.1
  Author:
    Juergen Hoeller
  See Also:
    `CommonsHttpInvokerRequestExecutor`, `HttpURLConnection`

- - ### 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 and Description
    `SimpleHttpInvokerRequestExecutor()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected RemoteInvocationResult` | `doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)` Execute the given request through a standard J2SE HttpURLConnection.
    `protected boolean` | `isGzipResponse(HttpURLConnection con)` Determine whether the given response is a GZIP response.
    `protected HttpURLConnection` | `openConnection(HttpInvokerClientConfiguration config)` Open an HttpURLConnection for the given remote invocation request.
    `protected void` | `prepareConnection(HttpURLConnection connection, int contentLength)` Prepare the given HTTP connection.
    `protected InputStream` | `readResponseBody(HttpInvokerClientConfiguration config, HttpURLConnection con)` Extract the response body from the given executed remote invocation request.
    `void` | `setConnectTimeout(int connectTimeout)` Set the underlying URLConnection's connect timeout (in milliseconds).
    `void` | `setReadTimeout(int readTimeout)` Set the underlying URLConnection's read timeout (in milliseconds).
    `protected void` | `validateResponse(HttpInvokerClientConfiguration config, HttpURLConnection con)` Validate the given response as contained in the HttpURLConnection object, throwing an exception if it does not correspond to a successful HTTP response.
    `protected void` | `writeRequestBody(HttpInvokerClientConfiguration config, HttpURLConnection con, ByteArrayOutputStream baos)` Set the given serialized remote invocation as request body.

    - ### Methods inherited from class org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor

      `createObjectInputStream, decorateInputStream, decorateOutputStream, doReadRemoteInvocationResult, doWriteRemoteInvocation, executeRequest, getBeanClassLoader, getByteArrayOutputStream, getContentType, isAcceptGzipEncoding, readRemoteInvocationResult, setAcceptGzipEncoding, setBeanClassLoader, setContentType, writeRemoteInvocation`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SimpleHttpInvokerRequestExecutor

          public SimpleHttpInvokerRequestExecutor()

  - ### Method Detail

    - #### setConnectTimeout

          public void setConnectTimeout(int connectTimeout)

      Set the underlying URLConnection's connect timeout (in milliseconds). A timeout value of 0 specifies an infinite timeout.

      Default is the system's default timeout.

      See Also:
        `URLConnection.setConnectTimeout(int)`

    - #### setReadTimeout

          public void setReadTimeout(int readTimeout)

      Set the underlying URLConnection's read timeout (in milliseconds). A timeout value of 0 specifies an infinite timeout.

      Default is the system's default timeout.

      See Also:
        `URLConnection.setReadTimeout(int)`

    - #### doExecuteRequest

          protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config,
                                                ByteArrayOutputStream baos)
                                                     throws IOException,
                                                            ClassNotFoundException

      Execute the given request through a standard J2SE HttpURLConnection.

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

      Specified by:
        `doExecuteRequest` in class `AbstractHttpInvokerRequestExecutor`
      Parameters:
        `config` - the HTTP invoker configuration that specifies the target service
        `baos` - the ByteArrayOutputStream that contains the serialized RemoteInvocation object
      Returns:
        the RemoteInvocationResult object
      Throws:
        `IOException` - if thrown by I/O operations
        `ClassNotFoundException` - if thrown during deserialization
      See Also:
        `openConnection(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration)`, `prepareConnection(java.net.HttpURLConnection, int)`, `writeRequestBody(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, java.net.HttpURLConnection, java.io.ByteArrayOutputStream)`, `validateResponse(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, java.net.HttpURLConnection)`, `readResponseBody(org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration, java.net.HttpURLConnection)`

    - #### openConnection

          protected HttpURLConnection openConnection(HttpInvokerClientConfiguration config)
                                              throws IOException

      Open an HttpURLConnection for the given remote invocation request.
      Parameters:
        `config` - the HTTP invoker configuration that specifies the target service
      Returns:
        the HttpURLConnection for the given request
      Throws:
        `IOException` - if thrown by I/O methods
      See Also:
        `URL.openConnection()`

    - #### prepareConnection

          protected void prepareConnection(HttpURLConnection connection,
                               int contentLength)
                                    throws IOException

      Prepare the given HTTP connection.

      The default implementation specifies POST as method, "application/x-java-serialized-object" as "Content-Type" header, and the given content length as "Content-Length" header.

      Parameters:
        `connection` - the HTTP connection to prepare
        `contentLength` - the length of the content to send
      Throws:
        `IOException` - if thrown by HttpURLConnection methods
      See Also:
        `HttpURLConnection.setRequestMethod(java.lang.String)`, `URLConnection.setRequestProperty(java.lang.String, java.lang.String)`

    - #### writeRequestBody

          protected void writeRequestBody(HttpInvokerClientConfiguration config,
                              HttpURLConnection con,
                              ByteArrayOutputStream baos)
                                   throws IOException

      Set the given serialized remote invocation as request body.

      The default implementation simply write the serialized invocation to the HttpURLConnection's OutputStream. 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
        `con` - the HttpURLConnection to write the request body to
        `baos` - the ByteArrayOutputStream that contains the serialized RemoteInvocation object
      Throws:
        `IOException` - if thrown by I/O methods
      See Also:
        `URLConnection.getOutputStream()`, `URLConnection.setRequestProperty(java.lang.String, java.lang.String)`

    - #### validateResponse

          protected void validateResponse(HttpInvokerClientConfiguration config,
                              HttpURLConnection con)
                                   throws IOException

      Validate the given response as contained in the HttpURLConnection 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
        `con` - the HttpURLConnection to validate
      Throws:
        `IOException` - if validation failed
      See Also:
        `HttpURLConnection.getResponseCode()`

    - #### readResponseBody

          protected InputStream readResponseBody(HttpInvokerClientConfiguration config,
                                     HttpURLConnection con)
                                          throws IOException

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

      The default implementation simply reads the serialized invocation from the HttpURLConnection's InputStream. 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
        `con` - the HttpURLConnection to read the response body from
      Returns:
        an InputStream for the response body
      Throws:
        `IOException` - if thrown by I/O methods
      See Also:
        `isGzipResponse(java.net.HttpURLConnection)`, `GZIPInputStream`, `URLConnection.getInputStream()`, `HttpURLConnection.getHeaderField(int)`, `HttpURLConnection.getHeaderFieldKey(int)`

    - #### isGzipResponse

          protected boolean isGzipResponse(HttpURLConnection con)

      Determine whether the given response is a GZIP response.

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

      Parameters:
        `con` - the HttpURLConnection to check