Package com.vaadin.flow.server.streams
Class DownloadResponse
java.lang.Object
com.vaadin.flow.server.streams.DownloadResponse
- All Implemented Interfaces:
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.
- Since:
- 24.8
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDownloadResponse
(InputStream inputStream, String fileName, String contentType, long contentLength) Create a download response with content stream and content data. -
Method Summary
Modifier and TypeMethodDescriptionstatic DownloadResponse
error
(int statusCode) Generate an error response for download.static DownloadResponse
Generate an error response for download with message.static DownloadResponse
error
(HttpStatusCode statusCode) Generate an error response for download.static DownloadResponse
error
(HttpStatusCode statusCode, String message) Generate an error response for download with message.long
Get the defined size for the content or-1
if unknown.Get the content type.int
getError()
Get the set error code.Get error message if set for error response.Get the defined file name.Get the InputStream to read the content data from.boolean
hasError()
Check if response has an error code.void
setError
(int error) Set http error code.void
Set http error code and error message.void
setError
(HttpStatusCode error) Set http error code.void
setError
(HttpStatusCode error, String errorMessage) Set http error code and error message.
-
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 notnull
, otherwise the header is not setcontentType
- content type or a value determined from fileName ifnull
contentLength
- byte size of a stream or-1
if unknown
-
-
Method Details
-
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
Get the defined file name.- Returns:
- file name
-
getContentType
Get the content type.For a
null
value the type should be gotten fromVaadinService.getMimeType(fileName)
or be set to default valueapplication/octet-stream
- Returns:
- content type
-
getContentLength
public long getContentLength()Get the defined size for the content or-1
if unknown.- Returns:
- content size
-
error
Generate an error response for download.- Parameters:
statusCode
- error status code- Returns:
- DownloadResponse for request
-
error
Generate an error response for download with message.- Parameters:
statusCode
- error status codemessage
- error message for details on what went wrong- Returns:
- DownloadResponse for request
-
error
Generate an error response for download.- Parameters:
statusCode
- error status code- Returns:
- DownloadResponse for request
-
error
Generate an error response for download with message.- Parameters:
statusCode
- error status codemessage
- error message for details on what went wrong- Returns:
- DownloadResponse for request
-
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
Set http error code and error message.- Parameters:
error
- error codeerrorMessage
- error message
-
setError
Set http error code.- Parameters:
error
- error code
-
setError
Set http error code and error message.- Parameters:
error
- error codeerrorMessage
- error message
-
getError
public int getError()Get the set error code.- Returns:
- error code or -1 if not set
-
getErrorMessage
Get error message if set for error response.- Returns:
- error message or null if not set
-