Class DownloadResponse

java.lang.Object
com.vaadin.flow.server.streams.DownloadResponse
All Implemented Interfaces:
Serializable

public class DownloadResponse extends Object implements Serializable
Data class containing required information for sending the given input stream to the client.

The given input stream will be read at a later time and will be automatically closed by the caller.

An error response, created with one of the error(...) methods, only sets the HTTP status code of the response. The response body stays empty: the error message and the exception are not sent to the browser. Instead, they are passed to the handler's transfer listeners: the error message is the message of the IOException given to TransferProgressListener.onError(TransferContext, java.io.IOException), and TransferContext.exception() returns the given exception, or else that IOException. To show the error to the user, add a listener, for example with TransferProgressAwareHandler.whenComplete(com.vaadin.flow.function.SerializableBiConsumer) and update the UI from there.

Since:
24.8
See Also:
  • Constructor Details

    • DownloadResponse

      public DownloadResponse(InputStream inputStream, String fileName, String contentType, long contentLength)
      Create a download response with content stream and content data.
      Parameters:
      inputStream - data stream for data to send to client, stream will be closed automatically after use by the caller.
      fileName - name of the file to be downloaded, configured by setting the Content-Disposition header to 'attachment' if the value is not null, otherwise the header is not set
      contentType - content type, or null for the one set with AbstractDownloadHandler.withContentType(String) on the handler, or else a value determined from fileName
      contentLength - byte size of a stream or -1 if unknown
  • Method Details

    • getInputStream

      public InputStream getInputStream()
      Get the InputStream to read the content data from.

      InputStream needs to be closed by the called after reading is over.

      Returns:
      content InputStream
    • getFileName

      public String getFileName()
      Get the defined file name.
      Returns:
      file name
    • getContentType

      public String getContentType()
      Get the content type.

      For a null value the type should be the one set with AbstractDownloadHandler.withContentType(String) on the handler, or else be gotten from VaadinService.getMimeType(fileName) or be set to default value application/octet-stream

      Returns:
      content type
    • getContentLength

      public long getContentLength()
      Get the defined size for the content or -1 if unknown.
      Returns:
      content size
    • error

      public static DownloadResponse error(int statusCode)
      Generate an error response for download.
      Parameters:
      statusCode - error status code
      Returns:
      DownloadResponse for request
    • error

      public static DownloadResponse error(int statusCode, Exception exception)
      Generate an error response for download.
      Parameters:
      statusCode - error status code
      exception - exception that caused the error
      Returns:
      DownloadResponse for request
      Since:
      24.9
    • error

      public static DownloadResponse error(int statusCode, String message)
      Generate an error response for download with message.
      Parameters:
      statusCode - error status code
      message - error message for details on what went wrong, given to the transfer listeners but not written to the response
      Returns:
      DownloadResponse for request
    • error

      public static DownloadResponse error(int statusCode, String message, Exception exception)
      Generate an error response for download with message.
      Parameters:
      statusCode - error status code
      message - error message for details on what went wrong, given to the transfer listeners but not written to the response
      exception - exception that caused the error
      Returns:
      DownloadResponse for request
      Since:
      24.9
    • error

      public static DownloadResponse error(HttpStatusCode statusCode)
      Generate an error response for download.
      Parameters:
      statusCode - error status code
      Returns:
      DownloadResponse for request
    • error

      public static DownloadResponse error(HttpStatusCode statusCode, Exception exception)
      Generate an error response for download.
      Parameters:
      statusCode - error status code
      exception - exception that caused the error
      Returns:
      DownloadResponse for request
      Since:
      24.9
    • error

      public static DownloadResponse error(HttpStatusCode statusCode, String message)
      Generate an error response for download with message.
      Parameters:
      statusCode - error status code
      message - error message for details on what went wrong, given to the transfer listeners but not written to the response
      Returns:
      DownloadResponse for request
    • error

      public static DownloadResponse error(HttpStatusCode statusCode, String message, Exception exception)
      Generate an error response for download with message.
      Parameters:
      statusCode - error status code
      message - error message for details on what went wrong, given to the transfer listeners but not written to the response
      exception - exception that caused the error
      Returns:
      DownloadResponse for request
      Since:
      24.9
    • hasError

      public boolean hasError()
      Check if response has an error code.
      Returns:
      true is error code has been set
    • setError

      public void setError(int error)
      Set http error code.
      Parameters:
      error - error code
    • setError

      public void setError(int error, Exception exception)
      Set http error code.
      Parameters:
      error - error code
      exception - exception that caused the error
      Since:
      24.9
    • setError

      public void setError(int error, String errorMessage)
      Set http error code and error message.
      Parameters:
      error - error code
      errorMessage - error message, given to the transfer listeners but not written to the response
    • setError

      public void setError(int error, String errorMessage, Exception exception)
      Set http error code and error message.
      Parameters:
      error - error code
      errorMessage - error message, given to the transfer listeners but not written to the response
      exception - exception that caused the error
      Since:
      24.9
    • setError

      public void setError(HttpStatusCode error)
      Set http error code.
      Parameters:
      error - error code
    • setError

      public void setError(HttpStatusCode error, Exception exception)
      Set http error code.
      Parameters:
      error - error code
      exception - exception that caused the error
      Since:
      24.9
    • setError

      public void setError(HttpStatusCode error, String errorMessage)
      Set http error code and error message.
      Parameters:
      error - error code
      errorMessage - error message, given to the transfer listeners but not written to the response
    • setError

      public void setError(HttpStatusCode error, String errorMessage, Exception exception)
      Set http error code and error message.
      Parameters:
      error - error code
      errorMessage - error message, given to the transfer listeners but not written to the response
      exception - exception that caused the error
      Since:
      24.9
    • getError

      public int getError()
      Get the set error code.
      Returns:
      error code or -1 if not set
    • getErrorMessage

      public String getErrorMessage()
      Get error message if set for error response.

      The message is not written to the response, see the class description.

      Returns:
      error message or null if not set
    • getException

      public Exception getException()
      Get the exception that occurred during the download process, if any.
      Returns:
      the exception or null if no exception occurred
      Since:
      24.9
      See Also: