public class SimpleClientHttpRequestFactory extends java.lang.Object implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory
ClientHttpRequestFactory
implementation that uses standard JDK facilities.HttpURLConnection
,
HttpComponentsClientHttpRequestFactory
Constructor and Description |
---|
SimpleClientHttpRequestFactory() |
Modifier and Type | Method and Description |
---|---|
AsyncClientHttpRequest |
createAsyncRequest(java.net.URI uri,
HttpMethod httpMethod)
Create a new asynchronous
AsyncClientHttpRequest for the specified URI
and HTTP method. |
ClientHttpRequest |
createRequest(java.net.URI uri,
HttpMethod httpMethod)
Create a new
ClientHttpRequest for the specified URI and HTTP method. |
protected java.net.HttpURLConnection |
openConnection(java.net.URL url,
java.net.Proxy proxy)
Opens and returns a connection to the given URL.
|
protected void |
prepareConnection(java.net.HttpURLConnection connection,
java.lang.String httpMethod)
Template method for preparing the given
HttpURLConnection . |
void |
setBufferRequestBody(boolean bufferRequestBody)
Indicate whether this request factory should buffer the
request body internally.
|
void |
setChunkSize(int chunkSize)
Set the number of bytes to write in each chunk when not buffering request
bodies locally.
|
void |
setConnectTimeout(int connectTimeout)
Set the underlying URLConnection's connect timeout (in milliseconds).
|
void |
setOutputStreaming(boolean outputStreaming)
Set if the underlying URLConnection can be set to 'output streaming' mode.
|
void |
setProxy(java.net.Proxy proxy)
Set the
Proxy to use for this request factory. |
void |
setReadTimeout(int readTimeout)
Set the underlying URLConnection's read timeout (in milliseconds).
|
void |
setTaskExecutor(AsyncListenableTaskExecutor taskExecutor)
Set the task executor for this request factory.
|
public void setProxy(java.net.Proxy proxy)
Proxy
to use for this request factory.public void setBufferRequestBody(boolean bufferRequestBody)
Default is true
. When sending large amounts of data via POST or PUT,
it is recommended to change this property to false
, so as not to run
out of memory. This will result in a ClientHttpRequest
that either
streams directly to the underlying HttpURLConnection
(if the
Content-Length
is known in advance), or that will use "Chunked transfer encoding"
(if the Content-Length
is not known in advance).
setChunkSize(int)
,
HttpURLConnection.setFixedLengthStreamingMode(int)
public void setChunkSize(int chunkSize)
Note that this parameter is only used when
bufferRequestBody
is set to false
,
and the Content-Length
is not known in advance.
setBufferRequestBody(boolean)
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)
public void setOutputStreaming(boolean outputStreaming)
true
.
When output streaming is enabled, authentication and redirection cannot be handled automatically.
If output streaming is disabled, the HttpURLConnection.setFixedLengthStreamingMode(int)
and
HttpURLConnection.setChunkedStreamingMode(int)
methods of the underlying connection will never
be called.
outputStreaming
- if output streaming is enabledpublic void setTaskExecutor(AsyncListenableTaskExecutor taskExecutor)
taskExecutor
- the task executorpublic ClientHttpRequest createRequest(java.net.URI uri, HttpMethod httpMethod) throws java.io.IOException
ClientHttpRequestFactory
ClientHttpRequest
for the specified URI and HTTP method.
The returned request can be written to, and then executed by calling
ClientHttpRequest.execute()
.
createRequest
in interface ClientHttpRequestFactory
uri
- the URI to create a request forhttpMethod
- the HTTP method to executejava.io.IOException
- in case of I/O errorspublic AsyncClientHttpRequest createAsyncRequest(java.net.URI uri, HttpMethod httpMethod) throws java.io.IOException
AsyncClientHttpRequest
for the specified URI
and HTTP method.
The returned request can be written to, and then executed by calling
AsyncClientHttpRequest.executeAsync()
.
Setting the taskExecutor
property is required before calling this method.
createAsyncRequest
in interface AsyncClientHttpRequestFactory
uri
- the URI to create a request forhttpMethod
- the HTTP method to executejava.io.IOException
- in case of I/O errorsprotected java.net.HttpURLConnection openConnection(java.net.URL url, @Nullable java.net.Proxy proxy) throws java.io.IOException
The default implementation uses the given proxy - if any - to open a connection.
url
- the URL to open a connection toproxy
- the proxy to use, may be null
java.io.IOException
- in case of I/O errorsprotected void prepareConnection(java.net.HttpURLConnection connection, java.lang.String httpMethod) throws java.io.IOException
HttpURLConnection
.
The default implementation prepares the connection for input and output, and sets the HTTP method.
connection
- the connection to preparehttpMethod
- the HTTP request method (GET
, POST
, etc.)java.io.IOException
- in case of I/O errors