Class AbstractDownloadHandler<R extends AbstractDownloadHandler>

java.lang.Object
com.vaadin.flow.server.streams.TransferProgressAwareHandler<DownloadEvent,R>
com.vaadin.flow.server.streams.AbstractDownloadHandler<R>
Type Parameters:
R - the type of the subclass implementing this abstract class
All Implemented Interfaces:
DownloadHandler, ElementRequestHandler, Serializable
Direct Known Subclasses:
ClassDownloadHandler, FileDownloadHandler, InputStreamDownloadHandler, ServletResourceDownloadHandler

public abstract class AbstractDownloadHandler<R extends AbstractDownloadHandler> extends TransferProgressAwareHandler<DownloadEvent,R> implements DownloadHandler
Abstract class for common methods used in pre-made download handlers.
Since:
24.8
See Also:
  • Constructor Details

    • AbstractDownloadHandler

      public AbstractDownloadHandler()
  • Method Details

    • getTransferContext

      protected TransferContext getTransferContext(DownloadEvent transferEvent)
      Description copied from class: TransferProgressAwareHandler
      This method is used to get the transfer context from the transfer events (e.g. DownloadEvent).
      Specified by:
      getTransferContext in class TransferProgressAwareHandler<DownloadEvent,R extends AbstractDownloadHandler>
      Parameters:
      transferEvent - the transfer event
      Returns:
      the transfer context
    • getContentType

      protected String getContentType(String fileName, VaadinResponse response)
      Gets the MIME type that the servlet container maps to the given file name, or application/octet-stream when it maps none.

      Unlike getContentType(), this ignores the type set with withContentType(String). Override it to change how the type is derived from the file name; to serve the content with the configured type when there is one, call resolveContentType(String, VaadinResponse) instead.

      Parameters:
      fileName - the name of the file to look up the type for
      response - the response, which gives access to the servlet container
      Returns:
      the MIME type for the file name, not null
    • withContentType

      public R withContentType(String contentType)
      Sets the MIME type that the content is served with, such as text/vtt, instead of the one that the servlet container maps to the file name, or application/octet-stream when it maps none.

      A content type that a DownloadResponse gives for a particular download still takes precedence over this one.

      Parameters:
      contentType - the MIME type of the content, not null or blank
      Returns:
      this instance for method chaining
      Throws:
      IllegalArgumentException - if the content type is blank
    • getContentType

      public String getContentType()
      Gets the MIME type set with withContentType(String).

      Unlike getContentType(String, VaadinResponse), this does not look up a type for the file name. Use resolveContentType(String, VaadinResponse) for the type that the content is served with.

      Returns:
      the MIME type that the content is served with, or null if it is taken from the file name
    • resolveContentType

      protected final String resolveContentType(String fileName, VaadinResponse response)
      Resolves the MIME type that the content is served with: the one set with withContentType(String), or else the one that getContentType(String, VaadinResponse) gives for the file name. The built-in handlers serve their content with this type, and a subclass that sets the content type of its own response should do the same.
      Parameters:
      fileName - the name of the file to look up the type for when none is set
      response - the response, which gives access to the servlet container
      Returns:
      the MIME type that the content is served with, not null
    • inline

      public R inline()
      Sets this download content to be displayed inside the Web page, or as the Web page, e.g. as an image or inside an iframe.

      Implementations of this class should ensure that the 'Content-Disposition' attribute is 'inline', if this method is called.

      Returns:
      this instance for method chaining
    • isInline

      public boolean isInline()
      Returns if the download content to be displayed inside the Web page or downloaded as a file.
      Returns:
      true if the content is to be displayed inline, false if it is to be downloaded as a file