Class FileDownloader

  • All Implemented Interfaces:
    MethodEventSource, ClientConnector, Extension, Connector, Serializable

    public class FileDownloader
    extends AbstractExtension
    Extension that starts a download when the extended component is clicked. This is used to overcome two challenges:
    • Resource should be bound to a component to allow it to be garbage collected when there are no longer any ways of reaching the resource.
    • Download should be started directly when the user clicks e.g. a Button without going through a server-side click listener to avoid triggering security warnings in some browsers.

    Please note that the download will be started in an iframe, which means that care should be taken to avoid serving content types that might make the browser attempt to show the content using a plugin instead of downloading it. Connector resources (e.g. FileResource and ClassResource) will automatically be served using a Content-Type: application/octet-stream header unless setOverrideContentType(boolean) has been set to false while files served in other ways, (e.g. ExternalResource or ThemeResource) will not automatically get this treatment.

    Since:
    7.0.0
    Author:
    Vaadin Ltd
    See Also:
    Serialized Form
    • Constructor Detail

      • FileDownloader

        public FileDownloader​(Resource resource)
        Creates a new file downloader for the given resource. To use the downloader, you should also AbstractExtension.extend(AbstractClientConnector) the component.
        Parameters:
        resource - the resource to download when the user clicks the extended component.
    • Method Detail

      • getFileDownloadResource

        public Resource getFileDownloadResource()
        Gets the resource set for download.
        Returns:
        the resource that will be downloaded if clicking the extended component
      • setFileDownloadResource

        public void setFileDownloadResource​(Resource resource)
        Sets the resource that is downloaded when the extended component is clicked.
        Parameters:
        resource - the resource to download
      • setOverrideContentType

        public void setOverrideContentType​(boolean overrideContentType)
        Sets whether the content type of served resources should be overriden to application/octet-stream to reduce the risk of a browser plugin choosing to display the resource instead of downloading it. This is by default set to true.

        Please note that this only affects Connector resources (e.g. FileResource and ClassResource) but not other resource types (e.g. ExternalResource or ThemeResource).

        Parameters:
        overrideContentType - true to override the content type if possible; false to use the original content type.
      • isOverrideContentType

        public boolean isOverrideContentType()
        Checks whether the content type should be overridden.
        Returns:
        true if the content type will be overridden when possible; false if the original content type will be used.
        See Also:
        setOverrideContentType(boolean)
      • handleConnectorRequest

        public boolean handleConnectorRequest​(VaadinRequest request,
                                              VaadinResponse response,
                                              String path)
                                       throws IOException
        Description copied from interface: ClientConnector
        Handle a request directed to this connector. This can be used by connectors to dynamically generate a response and it is also used internally when serving ConnectorResources.

        Requests to /APP/connector/[ui id]/[connector id]/ are routed to this method with the remaining part of the requested path available in the path parameter.

        NOTE that the session is not locked when this method is called. It is the responsibility of the connector to ensure that the session is locked while handling state or other session related data. For best performance the session should be unlocked before writing a large response to the client.

        Specified by:
        handleConnectorRequest in interface ClientConnector
        Overrides:
        handleConnectorRequest in class AbstractClientConnector
        Parameters:
        request - the request that should be handled
        response - the response object to which the response should be written
        path - the requested relative path
        Returns:
        true if the request has been handled, false if no response has been written.
        Throws:
        IOException - if there is a problem generating a response.