Class ApacheHttpClient4Executor
- java.lang.Object
-
- org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor
-
- All Implemented Interfaces:
ClientExecutor
public class ApacheHttpClient4Executor extends java.lang.Object implements ClientExecutor
- Version:
- $Revision: 1 $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ApacheHttpClient4Executor.FileExposingFileEntity
We useFileEntity
as theHttpEntity
implementation when the request OutputStream has been saved to a File on disk (because it was too large to fit into memory seeRestCFHttpClientExecutor#writeRequestBodyToOutputStream(ClientRequest)
); however, we have to delete the File supporting theFileEntity
, otherwise the disk will soon run out of space - remember that there can be very huge files, in GB range, processed on a regular basis - and FileEntity exposes its content File as a protected field.private static class
ApacheHttpClient4Executor.MemoryUnit
Enumeration to represent memory units.(package private) static class
ApacheHttpClient4Executor.ResponseStream
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
BYTE_MEMORY_UNIT
protected boolean
closed
protected boolean
createdHttpClient
private int
fileUploadInMemoryThresholdLimit
For uploading File's over JAX-RS framework, this property, together withfileUploadMemoryUnit
, defines the maximum File size allowed in memory.private ApacheHttpClient4Executor.MemoryUnit
fileUploadMemoryUnit
The unit forfileUploadInMemoryThresholdLimit
.private java.io.File
fileUploadTempFileDir
Temp directory to write output request stream to.static java.lang.String
GIGABYTE_MEMORY_UNIT
protected org.apache.http.client.HttpClient
httpClient
protected org.apache.http.protocol.HttpContext
httpContext
static java.lang.String
KILOBYTE_MEMORY_UNIT
static java.lang.String
MEGABYTE_MEMORY_UNIT
private static java.lang.String
processId
Used to build temp file prefix.protected int
responseBufferSize
-
Constructor Summary
Constructors Constructor Description ApacheHttpClient4Executor()
ApacheHttpClient4Executor(org.apache.http.client.HttpClient httpClient)
ApacheHttpClient4Executor(org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.apache.http.HttpEntity
buildEntity(ClientRequest request)
Build the HttpEntity to be sent to the Service as part of (POST) request.private static void
checkClientExceptionMapper()
protected void
cleanUpAfterExecute(org.apache.http.client.methods.HttpRequestBase httpMethod)
If passed httpMethod is of type HttpPost then obtain its entity.void
close()
void
commitHeaders(ClientRequest request, org.apache.http.client.methods.HttpRequestBase httpMethod)
protected java.io.InputStream
createBufferedStream(java.io.InputStream is)
private org.apache.http.client.methods.HttpRequestBase
createHttpMethod(java.lang.String url, java.lang.String restVerb)
ClientRequest
createRequest(java.lang.String uriTemplate)
ClientRequest
createRequest(javax.ws.rs.core.UriBuilder uriBuilder)
ClientResponse
execute(ClientRequest request)
static CaseInsensitiveMap<java.lang.String>
extractHeaders(org.apache.http.HttpResponse response)
void
finalize()
org.apache.http.client.HttpClient
getHttpClient()
org.apache.http.protocol.HttpContext
getHttpContext()
private int
getMemoryUnitMultiplier()
int
getResponseBufferSize()
Response stream is wrapped in a BufferedInputStream.protected java.lang.String
getTempfilePrefix()
Use context information, which will include node name, to avoid conflicts in case of multiple VMS using same temp directory location.private void
handleFileNotDeletedError(java.io.File tempRequestFile, java.lang.Exception ex)
Log that the file did not get deleted but prevent the request from failing by eating the exception.boolean
isClosed()
void
loadHttpMethod(ClientRequest request, org.apache.http.client.methods.HttpRequestBase httpMethod)
void
setFileUploadInMemoryThresholdLimit(int pInMemoryThresholdLimit)
Setter forfileUploadInMemoryThresholdLimit
void
setFileUploadMemoryUnit(java.lang.String pMemoryUnit)
Setter forfileUploadMemoryUnit
void
setFileUploadTempFileDir(java.io.File pTempFileDir)
Setter forfileUploadTempFileDir
(package private) void
setHttpClient(org.apache.http.client.HttpClient pHttpClient)
Setter for theHttpClient
to which this class delegates the actual HTTP call.void
setHttpContext(org.apache.http.protocol.HttpContext httpContext)
void
setResponseBufferSize(int responseBufferSize)
Response stream is wrapped in a BufferedInputStream.private org.apache.commons.io.output.DeferredFileOutputStream
writeRequestBodyToOutputStream(ClientRequest request)
Creates the request OutputStream, to be sent to the end Service invoked, as a DeferredFileOutputStream.
-
-
-
Field Detail
-
BYTE_MEMORY_UNIT
public static final java.lang.String BYTE_MEMORY_UNIT
- See Also:
- Constant Field Values
-
KILOBYTE_MEMORY_UNIT
public static final java.lang.String KILOBYTE_MEMORY_UNIT
- See Also:
- Constant Field Values
-
MEGABYTE_MEMORY_UNIT
public static final java.lang.String MEGABYTE_MEMORY_UNIT
- See Also:
- Constant Field Values
-
GIGABYTE_MEMORY_UNIT
public static final java.lang.String GIGABYTE_MEMORY_UNIT
- See Also:
- Constant Field Values
-
processId
private static java.lang.String processId
Used to build temp file prefix.
-
httpClient
protected org.apache.http.client.HttpClient httpClient
-
createdHttpClient
protected boolean createdHttpClient
-
httpContext
protected org.apache.http.protocol.HttpContext httpContext
-
closed
protected boolean closed
-
fileUploadInMemoryThresholdLimit
private int fileUploadInMemoryThresholdLimit
For uploading File's over JAX-RS framework, this property, together withfileUploadMemoryUnit
, defines the maximum File size allowed in memory. If fileSize exceeds this size, it will be stored tofileUploadTempFileDir
.
Defaults to 1 MB
-
fileUploadMemoryUnit
private ApacheHttpClient4Executor.MemoryUnit fileUploadMemoryUnit
- See Also:
ApacheHttpClient4Executor.MemoryUnit
-
fileUploadTempFileDir
private java.io.File fileUploadTempFileDir
Temp directory to write output request stream to. Any file to be uploaded has to be written out to the output request stream to be sent to the service and when the File is too huge the output request stream is written out to the disk rather than to memory.
Defaults to JVM temp directory.
-
responseBufferSize
protected int responseBufferSize
-
-
Constructor Detail
-
ApacheHttpClient4Executor
public ApacheHttpClient4Executor()
-
ApacheHttpClient4Executor
public ApacheHttpClient4Executor(org.apache.http.client.HttpClient httpClient)
-
ApacheHttpClient4Executor
public ApacheHttpClient4Executor(org.apache.http.client.HttpClient httpClient, org.apache.http.protocol.HttpContext httpContext)
-
-
Method Detail
-
checkClientExceptionMapper
private static void checkClientExceptionMapper()
-
getResponseBufferSize
public int getResponseBufferSize()
Response stream is wrapped in a BufferedInputStream. Default is 8192. Value of 0 will not wrap it. Value of -1 will use a SelfExpandingBufferedInputStream- Returns:
-
setResponseBufferSize
public void setResponseBufferSize(int responseBufferSize)
Response stream is wrapped in a BufferedInputStream. Default is 8192. Value of 0 will not wrap it. Value of -1 will use a SelfExpandingBufferedInputStream- Parameters:
responseBufferSize
-
-
getHttpClient
public org.apache.http.client.HttpClient getHttpClient()
-
getHttpContext
public org.apache.http.protocol.HttpContext getHttpContext()
-
setHttpContext
public void setHttpContext(org.apache.http.protocol.HttpContext httpContext)
-
extractHeaders
public static CaseInsensitiveMap<java.lang.String> extractHeaders(org.apache.http.HttpResponse response)
-
createRequest
public ClientRequest createRequest(java.lang.String uriTemplate)
- Specified by:
createRequest
in interfaceClientExecutor
-
createRequest
public ClientRequest createRequest(javax.ws.rs.core.UriBuilder uriBuilder)
- Specified by:
createRequest
in interfaceClientExecutor
-
createBufferedStream
protected java.io.InputStream createBufferedStream(java.io.InputStream is)
-
execute
public ClientResponse execute(ClientRequest request) throws java.lang.Exception
- Specified by:
execute
in interfaceClientExecutor
- Throws:
java.lang.Exception
-
cleanUpAfterExecute
protected void cleanUpAfterExecute(org.apache.http.client.methods.HttpRequestBase httpMethod)
If passed httpMethod is of type HttpPost then obtain its entity. If the entity has an enclosing File then delete it by invoking this method after the request has completed. The entity will have an enclosing File only if it was too huge to fit into memory.- Parameters:
httpMethod
- - the httpMethod to clean up.- See Also:
writeRequestBodyToOutputStream(ClientRequest)
-
createHttpMethod
private org.apache.http.client.methods.HttpRequestBase createHttpMethod(java.lang.String url, java.lang.String restVerb)
-
loadHttpMethod
public void loadHttpMethod(ClientRequest request, org.apache.http.client.methods.HttpRequestBase httpMethod) throws java.lang.Exception
- Throws:
java.lang.Exception
-
buildEntity
protected org.apache.http.HttpEntity buildEntity(ClientRequest request) throws java.io.IOException
Build the HttpEntity to be sent to the Service as part of (POST) request. Creates a off-memoryApacheHttpClient4Executor.FileExposingFileEntity
or a regular in-memoryByteArrayEntity
depending on if the request OutputStream fit into memory when built by callingwriteRequestBodyToOutputStream(ClientRequest)
.- Parameters:
request
- -- Returns:
- - the built HttpEntity
- Throws:
java.io.IOException
- -
-
writeRequestBodyToOutputStream
private org.apache.commons.io.output.DeferredFileOutputStream writeRequestBodyToOutputStream(ClientRequest request) throws java.io.IOException
Creates the request OutputStream, to be sent to the end Service invoked, as a DeferredFileOutputStream.- Parameters:
request
- -- Returns:
- - DeferredFileOutputStream with the ClientRequest written out per HTTP specification.
- Throws:
java.io.IOException
- -
-
getMemoryUnitMultiplier
private int getMemoryUnitMultiplier()
- Returns:
- - the constant to multiply
fileUploadInMemoryThresholdLimit
with based onfileUploadMemoryUnit
enumeration value.
-
getTempfilePrefix
protected java.lang.String getTempfilePrefix()
Use context information, which will include node name, to avoid conflicts in case of multiple VMS using same temp directory location.- Returns:
- -
-
handleFileNotDeletedError
private void handleFileNotDeletedError(java.io.File tempRequestFile, java.lang.Exception ex)
Log that the file did not get deleted but prevent the request from failing by eating the exception. Register the file to be deleted on exit, so it will get deleted eventually.- Parameters:
tempRequestFile
- -ex
- - a null may be passed in which case this param gets ignored.
-
setHttpClient
void setHttpClient(org.apache.http.client.HttpClient pHttpClient)
Setter for theHttpClient
to which this class delegates the actual HTTP call. Note that this class acts as the adapter between RestEasy and Apache HTTP Component library.- Parameters:
pHttpClient
- -
-
setFileUploadInMemoryThresholdLimit
public void setFileUploadInMemoryThresholdLimit(int pInMemoryThresholdLimit)
Setter forfileUploadInMemoryThresholdLimit
- Parameters:
pInMemoryThresholdLimit
- - the inMemoryThresholdLimitMB to set
-
setFileUploadTempFileDir
public void setFileUploadTempFileDir(java.io.File pTempFileDir)
Setter forfileUploadTempFileDir
- Parameters:
pTempFileDir
- the tempFileDir to set
-
setFileUploadMemoryUnit
public void setFileUploadMemoryUnit(java.lang.String pMemoryUnit)
Setter forfileUploadMemoryUnit
- Parameters:
pMemoryUnit
- the memoryUnit to set
-
commitHeaders
public void commitHeaders(ClientRequest request, org.apache.http.client.methods.HttpRequestBase httpMethod)
-
close
public void close()
- Specified by:
close
in interfaceClientExecutor
-
isClosed
public boolean isClosed()
-
finalize
public void finalize() throws java.lang.Throwable
- Overrides:
finalize
in classjava.lang.Object
- Throws:
java.lang.Throwable
-
-