Class AsyncInputStreamHelper
- java.lang.Object
-
- fr.gouv.vitam.common.server.application.AsyncInputStreamHelper
-
public class AsyncInputStreamHelper extends java.lang.Object
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()); } }); }
@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
Constructors Constructor Description AsyncInputStreamHelper(javax.ws.rs.container.AsyncResponse asyncResponse, java.io.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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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)
.static void
asyncResponseResume(javax.ws.rs.container.AsyncResponse asyncResponse, javax.ws.rs.core.Response response, java.io.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 Detail
-
AsyncInputStreamHelper
public AsyncInputStreamHelper(javax.ws.rs.container.AsyncResponse asyncResponse, java.io.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 Detail
-
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, java.io.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
-
-