Interface DownloadHandler
- All Superinterfaces:
ElementRequestHandler
,Serializable
- All Known Implementing Classes:
AbstractDownloadHandler
,ClassDownloadHandler
,FileDownloadHandler
,InputStreamDownloadHandler
,ServletResourceDownloadHandler
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
This interface can be implemented in two ways:
- By creating a lambda expression that implements the
handleDownloadRequest(DownloadEvent)
method - By creating a child or anonymous class that implements this interface
The interface provides several factory methods for common download scenarios:
forFile(File)
- for downloading files from the serverforClassResource(Class, String)
- for downloading class resourcesforServletResource(String)
- for downloading servlet resourcesfromInputStream(InputStreamDownloadCallback)
- for downloading from input streams
DownloadHandler.forFile(new File("/path/to/file.txt"));All factory methods have overloads that allow adding a download progress listener and set a custom file name and URL postfix:
DownloadHandler.forClassResource(MyView.class, "attachment-XYZ.txt", "attachment.txt", new TransferProgressListener() { @Override public void onComplete(TransferContext context, long transferredBytes) { // update UI on complete } });
If you need to write directly to an OutputStream, you can use a lambda
expression with event.getOutputStream()
to access the output stream:
DownloadHandler handler = event -> { try (OutputStream out = event.getOutputStream()) { // Write your data to the output stream out.write(yourData); } };
- Since:
- 24.8
-
Method Summary
Modifier and TypeMethodDescriptionstatic ClassDownloadHandler
forClassResource
(Class<?> clazz, String resourceName) Generate a download handler for class resource.static ClassDownloadHandler
forClassResource
(Class<?> clazz, String resourceName, TransferProgressListener listener) Generate a download handler for class resource with the given progress listener.static ClassDownloadHandler
forClassResource
(Class<?> clazz, String resourceName, String fileNameOverride) Generate a download handler for class resource with the given download name.static ClassDownloadHandler
forClassResource
(Class<?> clazz, String resourceName, String fileNameOverride, TransferProgressListener listener) Generate a download handler for class resource with the given download name and progress listener.static FileDownloadHandler
Get a download handler for serving givenFile
.static FileDownloadHandler
forFile
(File file, TransferProgressListener listener) Get a download handler for serving givenFile
with the given progress listener.static FileDownloadHandler
Get a download handler for serving givenFile
with the given download file name.static FileDownloadHandler
forFile
(File file, String fileNameOverride, TransferProgressListener listener) Get a download handler for serving givenFile
with the given download file name and progress listener.forServletResource
(String path) Generate a download handler for a servlet resource.forServletResource
(String path, TransferProgressListener listener) Generate a download handler for a servlet resource with the given progress listener.forServletResource
(String path, String fileNameOverride) Generate a download handler for a servlet resource with the given download file name.forServletResource
(String path, String fileNameOverride, TransferProgressListener listener) Generate a download handler for a servlet resource with the given download fileNameOverride and progress listener.static InputStreamDownloadHandler
fromInputStream
(InputStreamDownloadCallback callback) Generate a function for downloading from a generated InputStream.static InputStreamDownloadHandler
fromInputStream
(InputStreamDownloadCallback callback, TransferProgressListener listener) Generate a function for downloading from a generated InputStream with the given progress listener.void
Method that is called when the client wants to download from the url stored for this specific handler registration.default void
handleRequest
(VaadinRequest request, VaadinResponse response, VaadinSession session, Element owner) Request handler callback for handing client-server or server-client data transfer scoped to a specific (owner) element.Methods inherited from interface com.vaadin.flow.server.streams.ElementRequestHandler
getDisabledUpdateMode, getUrlPostfix, isAllowInert
-
Method Details
-
handleDownloadRequest
Method that is called when the client wants to download from the url stored for this specific handler registration.- Parameters:
event
- download event containing the necessary data for writing the response- Throws:
IOException
- if an IO error occurred during download
-
handleRequest
default void handleRequest(VaadinRequest request, VaadinResponse response, VaadinSession session, Element owner) throws IOException Description copied from interface:ElementRequestHandler
Request handler callback for handing client-server or server-client data transfer scoped to a specific (owner) element. Note: when handling requests via this API, you need to take care of typical stream handling issues, e.g. exceptions yourself. However, you do not need to close the stream yourself, Flow will handle that for you when needed.- Specified by:
handleRequest
in interfaceElementRequestHandler
- Parameters:
request
- VaadinRequest request to handleresponse
- VaadinResponse response to handlesession
- VaadinSession current VaadinSessionowner
- Element owner element- Throws:
IOException
- if an IO error occurred during data transfer
-
forFile
Get a download handler for serving givenFile
.The downloaded file name is resolved as
file.getName()
.- Parameters:
file
- file to server for download- Returns:
- DownloadHandler implementation for download a file
-
forFile
Get a download handler for serving givenFile
with the given download file name.- Parameters:
file
- file to server for downloadfileNameOverride
- download file name that overridesfile.getName()
and also used as a download request URL postfix- Returns:
- DownloadHandler implementation for download a file
-
forFile
static FileDownloadHandler forFile(File file, String fileNameOverride, TransferProgressListener listener) Get a download handler for serving givenFile
with the given download file name and progress listener.- Parameters:
file
- file to server for downloadfileNameOverride
- download file name that overridesfile.getName()
and also used as a download request URL postfixlistener
- listener for transfer progress events- Returns:
- DownloadHandler implementation for download a file
-
forFile
Get a download handler for serving givenFile
with the given progress listener.The downloaded file name is resolved as
file.getName()
.- Parameters:
file
- file to server for downloadlistener
- listener for transfer progress events- Returns:
- DownloadHandler implementation for download a file
-
forClassResource
Generate a download handler for class resource.For instance for the file
resources/com/example/ui/MyData.json
and classcom.example.ui.MyData
the definition would beforClassResource(MyData.class, "MyData.json")
.The downloaded file name is resolved as
resourceName
.- Parameters:
clazz
- class for resource moduleresourceName
- name of class resource- Returns:
- DownloadHandler implementation for download a class resource
-
forClassResource
static ClassDownloadHandler forClassResource(Class<?> clazz, String resourceName, String fileNameOverride) Generate a download handler for class resource with the given download name.For instance for the file
resources/com/example/ui/MyData.json
and classcom.example.ui.MyData
the definition would beforClassResource(MyData.class, "MyData.json", "Data.json")
- Parameters:
clazz
- class for resource moduleresourceName
- name of class resourcefileNameOverride
- download file name that overridesresourceName
and also used as a download request URL postfix- Returns:
- DownloadHandler implementation for download a class resource
-
forClassResource
static ClassDownloadHandler forClassResource(Class<?> clazz, String resourceName, String fileNameOverride, TransferProgressListener listener) Generate a download handler for class resource with the given download name and progress listener.For instance for the file
resources/com/example/ui/MyData.json
and classcom.example.ui.MyData
the definition would beforClassResource(MyData.class, "MyData.json", "Data.json")
- Parameters:
clazz
- class for resource moduleresourceName
- name of class resourcefileNameOverride
- download file name that overridesresourceName
and also used as a download request URL postfixlistener
- listener for transfer progress events- Returns:
- DownloadHandler implementation for download a class resource
-
forClassResource
static ClassDownloadHandler forClassResource(Class<?> clazz, String resourceName, TransferProgressListener listener) Generate a download handler for class resource with the given progress listener.For instance for the file
resources/com/example/ui/MyData.json
and classcom.example.ui.MyData
the definition would beforClassResource(MyData.class, "MyData.json", "Data.json")
.The downloaded file name is resolved as
resourceName
.- Parameters:
clazz
- class for resource moduleresourceName
- name of class resourcelistener
- listener for transfer progress events- Returns:
- DownloadHandler implementation for download a class resource
-
forServletResource
Generate a download handler for a servlet resource.For instance for the file
webapp/WEB-INF/servlet.json
the path would be/WEB-INF/servlet.json
.The downloaded file name is resolved as the last segment in
path
.- Parameters:
path
- the servlet path to the file- Returns:
- DownloadHandler implementation for downloading a servlet resource
-
forServletResource
Generate a download handler for a servlet resource with the given download file name.For instance for the file
webapp/WEB-INF/servlet.json
the path would be/WEB-INF/servlet.json
File name override is appended to the download url as the logical name of the target file.
- Parameters:
path
- the servlet path to the filefileNameOverride
- download file name that overrides the name taken frompath
and also used as a download request URL postfix- Returns:
- DownloadHandler implementation for downloading a servlet resource
-
forServletResource
static ServletResourceDownloadHandler forServletResource(String path, String fileNameOverride, TransferProgressListener listener) Generate a download handler for a servlet resource with the given download fileNameOverride and progress listener.For instance for the file
webapp/WEB-INF/servlet.json
the path would be/WEB-INF/servlet.json
File name override is appended to the download url as the logical name of the target file.
- Parameters:
path
- the servlet path to the filefileNameOverride
- download file name that overrides the name taken frompath
and also used as a download request URL postfixlistener
- listener for transfer progress events- Returns:
- DownloadHandler implementation for downloading a servlet resource
-
forServletResource
static ServletResourceDownloadHandler forServletResource(String path, TransferProgressListener listener) Generate a download handler for a servlet resource with the given progress listener.For instance for the file
webapp/WEB-INF/servlet.json
the path would be/WEB-INF/servlet.json
.- Parameters:
path
- the servlet path to the filelistener
- listener for transfer progress events- Returns:
- DownloadHandler implementation for downloading a servlet resource
-
fromInputStream
Generate a function for downloading from a generated InputStream.DownloadResponse
instances can be created using various factory methods or with new operator.- Parameters:
callback
- a function that will be called on download- Returns:
- DownloadHandler implementation for download from an input stream
-
fromInputStream
static InputStreamDownloadHandler fromInputStream(InputStreamDownloadCallback callback, TransferProgressListener listener) Generate a function for downloading from a generated InputStream with the given progress listener.DownloadResponse
instances can be created using various factory methods or with new operator.- Parameters:
callback
- a function that will be called on downloadlistener
- listener for transfer progress events- Returns:
- DownloadHandler implementation for download from an input stream
-