Class AsyncInputStreamHelper
java.lang.Object
fr.gouv.vitam.common.server.application.AsyncInputStreamHelper
Async Response for InputStream Helper
Usage:
Direct download, where no Http Client is called but a direct InputStream generated
@Path(DOWNLOAD + HttpMethod.GET)
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.WILDCARD)
public void downloadDirectGet(@Suspended final AsyncResponse asyncResponse) {
VitamThreadPoolExecutor.getInstance().execute(new Runnable() {
@Override
public void run() {
File file = new File(...)
FileInputStream inputStream = new FileInputStream(file);
new AsyncInputStreamHelper(asyncResponse, inputStream)
.writeResponse(Response.ok());
}
});
}
Indirect download, where one Http Client is called to get an InputStream to forward
@Path(DOWNLOAD_INDIRECT + HttpMethod.GET)
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.WILDCARD)
public void downloadIndirectGet(@Suspended final AsyncResponse asyncResponse) throws VitamClientInternalException {
VitamThreadPoolExecutor.getInstance().execute(new Runnable() {
@Override
public void run() {
String method = HttpMethod.GET;
Response response = null;
try (final BenchmarkClientRest client =
BenchmarkClientFactory.getInstance().getClient()) {
response = client.performRequest(method, BenchmarkResourceProduceInputStream.DOWNLOAD + method,
null, MediaType.APPLICATION_OCTET_STREAM_TYPE);
buildReponse(asyncResponse, response); // Using AsyncInputStreamHelper
} catch (VitamClientInternalException e) {
AsyncInputStreamHelper.asyncResponseResume(asyncResponse,
Response.status(Status.INTERNAL_SERVER_ERROR).build());
}
}
});
}
-
Constructor Summary
ConstructorDescriptionAsyncInputStreamHelper
(javax.ws.rs.container.AsyncResponse asyncResponse, InputStream inputStream) Constructor using received response containing an InputStream to forwardAsyncInputStreamHelper
(javax.ws.rs.container.AsyncResponse asyncResponse, javax.ws.rs.core.Response receivedResponse) Constructor using native InputStream and size to forward -
Method Summary
Modifier and TypeMethodDescriptionstatic void
asyncResponseResume
(javax.ws.rs.container.AsyncResponse asyncResponse, javax.ws.rs.core.Response response) Call this to finalize your operation in case of Error message while no remote client operation is done. Note you must not call this method if you have already a received Response but thewriteErrorResponse(Response)
.static void
asyncResponseResume
(javax.ws.rs.container.AsyncResponse asyncResponse, javax.ws.rs.core.Response response, InputStream stream) Call this to finalize your operation in case of Error message while no remote client operation is done. Note you must not call this method if you have already a received Response but thewriteErrorResponse(Response)
.void
writeErrorResponse
(javax.ws.rs.core.Response errorResponse) Once constructed, call this to finalize your operation in case of Error message. Note that receivedResponse if any is fully read and closed for you there.void
writeResponse
(javax.ws.rs.core.Response.ResponseBuilder responseBuilder) Once constructed, call this to finalize your operation. Note that receivedResponse if any is closed for you there.
-
Constructor Details
-
AsyncInputStreamHelper
public AsyncInputStreamHelper(javax.ws.rs.container.AsyncResponse asyncResponse, InputStream inputStream) Constructor using received response containing an InputStream to forward- Parameters:
asyncResponse
- the AsyncReponse from the Resource APIinputStream
- the native InputStream to send (not from a Client response)
-
AsyncInputStreamHelper
public AsyncInputStreamHelper(javax.ws.rs.container.AsyncResponse asyncResponse, javax.ws.rs.core.Response receivedResponse) Constructor using native InputStream and size to forward- Parameters:
asyncResponse
- the AsyncReponse from the Resource APIreceivedResponse
- Received Response containing the InputStream to forward as is
-
-
Method Details
-
writeErrorResponse
public void writeErrorResponse(javax.ws.rs.core.Response errorResponse) Once constructed, call this to finalize your operation in case of Error message. Note that receivedResponse if any is fully read and closed for you there.- Parameters:
errorResponse
- the fully prepared ErrorResponse
-
writeResponse
public void writeResponse(javax.ws.rs.core.Response.ResponseBuilder responseBuilder) Once constructed, call this to finalize your operation. Note that receivedResponse if any is closed for you there.- Parameters:
responseBuilder
- the ResponseBuilder initialize with your own parameters and status
-
asyncResponseResume
public static void asyncResponseResume(javax.ws.rs.container.AsyncResponse asyncResponse, javax.ws.rs.core.Response response) Call this to finalize your operation in case of Error message while no remote client operation is done. Note you must not call this method if you have already a received Response but thewriteErrorResponse(Response)
.- Parameters:
asyncResponse
-response
- the fully prepared ErrorResponse
-
asyncResponseResume
public static void asyncResponseResume(javax.ws.rs.container.AsyncResponse asyncResponse, javax.ws.rs.core.Response response, InputStream stream) Call this to finalize your operation in case of Error message while no remote client operation is done. Note you must not call this method if you have already a received Response but thewriteErrorResponse(Response)
.- Parameters:
asyncResponse
-response
- the fully prepared ErrorResponsestream
- an inputStream to close anyway
-