Class 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());
     }
     });
     }
     
     

    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

      Constructors 
      Constructor Description
      AsyncInputStreamHelper​(javax.ws.rs.container.AsyncResponse asyncResponse, java.io.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
    • 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 the writeErrorResponse(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 the writeErrorResponse(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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 API
        inputStream - 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 API
        receivedResponse - 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 the writeErrorResponse(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 the writeErrorResponse(Response).
        Parameters:
        asyncResponse -
        response - the fully prepared ErrorResponse
        stream - an inputStream to close anyway