public class SimpleHttpInvokerRequestExecutor extends AbstractHttpInvokerRequestExecutor
Designed for easy subclassing, customizing specific template methods. However, consider CommonsHttpInvokerRequestExecutor for more sophisticated needs: The J2SE HttpURLConnection is rather limited in its capabilities.
CommonsHttpInvokerRequestExecutor
,
HttpURLConnection
Modifier and Type | Field and Description |
---|---|
private int |
connectTimeout |
private int |
readTimeout |
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 |
---|
SimpleHttpInvokerRequestExecutor() |
Modifier and Type | Method and Description |
---|---|
protected RemoteInvocationResult |
doExecuteRequest(HttpInvokerClientConfiguration config,
java.io.ByteArrayOutputStream baos)
Execute the given request through a standard J2SE HttpURLConnection.
|
protected boolean |
isGzipResponse(java.net.HttpURLConnection con)
Determine whether the given response is a GZIP response.
|
protected java.net.HttpURLConnection |
openConnection(HttpInvokerClientConfiguration config)
Open an HttpURLConnection for the given remote invocation request.
|
protected void |
prepareConnection(java.net.HttpURLConnection connection,
int contentLength)
Prepare the given HTTP connection.
|
protected java.io.InputStream |
readResponseBody(HttpInvokerClientConfiguration config,
java.net.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,
java.net.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,
java.net.HttpURLConnection con,
java.io.ByteArrayOutputStream baos)
Set the given serialized remote invocation as request body.
|
createObjectInputStream, decorateInputStream, decorateOutputStream, doReadRemoteInvocationResult, doWriteRemoteInvocation, executeRequest, getBeanClassLoader, getByteArrayOutputStream, getContentType, isAcceptGzipEncoding, readRemoteInvocationResult, setAcceptGzipEncoding, setBeanClassLoader, setContentType, writeRemoteInvocation
public void setConnectTimeout(int connectTimeout)
Default is the system's default timeout.
URLConnection.setConnectTimeout(int)
public void setReadTimeout(int readTimeout)
Default is the system's default timeout.
URLConnection.setReadTimeout(int)
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 deserializationopenConnection(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)
protected java.net.HttpURLConnection openConnection(HttpInvokerClientConfiguration config) throws java.io.IOException
config
- the HTTP invoker configuration that specifies the
target servicejava.io.IOException
- if thrown by I/O methodsURL.openConnection()
protected void prepareConnection(java.net.HttpURLConnection connection, int contentLength) throws java.io.IOException
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.
connection
- the HTTP connection to preparecontentLength
- the length of the content to sendjava.io.IOException
- if thrown by HttpURLConnection methodsHttpURLConnection.setRequestMethod(java.lang.String)
,
URLConnection.setRequestProperty(java.lang.String, java.lang.String)
protected void writeRequestBody(HttpInvokerClientConfiguration config, java.net.HttpURLConnection con, java.io.ByteArrayOutputStream baos) throws java.io.IOException
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.
config
- the HTTP invoker configuration that specifies the target servicecon
- the HttpURLConnection to write the request body tobaos
- the ByteArrayOutputStream that contains the serialized
RemoteInvocation objectjava.io.IOException
- if thrown by I/O methodsURLConnection.getOutputStream()
,
URLConnection.setRequestProperty(java.lang.String, java.lang.String)
protected void validateResponse(HttpInvokerClientConfiguration config, java.net.HttpURLConnection con) 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 servicecon
- the HttpURLConnection to validatejava.io.IOException
- if validation failedHttpURLConnection.getResponseCode()
protected java.io.InputStream readResponseBody(HttpInvokerClientConfiguration config, java.net.HttpURLConnection con) throws java.io.IOException
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.
config
- the HTTP invoker configuration that specifies the target servicecon
- the HttpURLConnection to read the response body fromjava.io.IOException
- if thrown by I/O methodsisGzipResponse(java.net.HttpURLConnection)
,
GZIPInputStream
,
URLConnection.getInputStream()
,
HttpURLConnection.getHeaderField(int)
,
HttpURLConnection.getHeaderFieldKey(int)
protected boolean isGzipResponse(java.net.HttpURLConnection con)
Default implementation checks whether the HTTP "Content-Encoding" header contains "gzip" (in any casing).
con
- the HttpURLConnection to check