public class AsyncInputStreamHelper extends Object
@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 and Description |
---|
AsyncInputStreamHelper(javax.ws.rs.container.AsyncResponse asyncResponse,
InputStream inputStream)
Constructor using received response containing an InputStream to forward
|
AsyncInputStreamHelper(javax.ws.rs.container.AsyncResponse asyncResponse,
javax.ws.rs.core.Response receivedResponse)
Constructor using native InputStream and size to forward
|
Modifier and Type | Method and 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 the
writeErrorResponse(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 the
writeErrorResponse(Response) . |
void |
writeAsyncResponse(javax.ws.rs.core.Response.ResponseBuilder responseBuilder,
javax.ws.rs.core.Response.Status status)
Once constructed, call this to finalize your operation.
Note that receivedResponse if any is closed for you there.
|
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.
|
public AsyncInputStreamHelper(javax.ws.rs.container.AsyncResponse asyncResponse, InputStream inputStream)
asyncResponse
- the AsyncReponse from the Resource APIinputStream
- the native InputStream to send (not from a Client response)public AsyncInputStreamHelper(javax.ws.rs.container.AsyncResponse asyncResponse, javax.ws.rs.core.Response receivedResponse)
asyncResponse
- the AsyncReponse from the Resource APIreceivedResponse
- Received Response containing the InputStream to forward as ispublic void writeErrorResponse(javax.ws.rs.core.Response errorResponse)
errorResponse
- the fully prepared ErrorResponsepublic void writeResponse(javax.ws.rs.core.Response.ResponseBuilder responseBuilder)
responseBuilder
- the ResponseBuilder initialize with your own parameters and statuspublic static void asyncResponseResume(javax.ws.rs.container.AsyncResponse asyncResponse, javax.ws.rs.core.Response response)
writeErrorResponse(Response)
.asyncResponse
- response
- the fully prepared ErrorResponsepublic static void asyncResponseResume(javax.ws.rs.container.AsyncResponse asyncResponse, javax.ws.rs.core.Response response, InputStream stream)
writeErrorResponse(Response)
.asyncResponse
- response
- the fully prepared ErrorResponsestream
- an inputStream to close anywaypublic void writeAsyncResponse(javax.ws.rs.core.Response.ResponseBuilder responseBuilder, javax.ws.rs.core.Response.Status status)
responseBuilder
- the ResponseBuilder initialize with your own parameters and statusstatus
- the status of the responseCopyright © 2018 Vitam. All rights reserved.