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
      Since:
      25.4
    • 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
      Since:
      25.4
    • 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
      Since:
      25.4
    • inline

      @Deprecated(since="25.4", forRemoval=true) public R inline()
      Deprecated, for removal: This API element is subject to removal in a future version.
      whether content is shown or saved depends on where the handler is used, not on the handler, and this changes the handler for every place that uses it. The components that show content, such as Image and IFrame, and Download.onClick with a new tab already serve it inline. For an Anchor, use setDownload(false) or AttachmentType.INLINE. Elsewhere, register the handler with DownloadDisposition.INLINE on its ElementStreamResource instead. Once this method is removed, an Anchor without such a choice serves a built-in handler as a download.
      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.

      The built-in handlers then serve the content inline unless the place where the handler is used asks for an attachment with a DownloadDisposition, for example Download.onClick with DownloadTarget.SAVE_AS_FILE. Other implementations should serve the content inline when this method is called, for example with DownloadEvent.inline(String); that call always sends inline, also where an attachment is asked for.

      Returns:
      this instance for method chaining
    • isInline

      @Deprecated(since="25.4", forRemoval=true) public boolean isInline()
      Deprecated, for removal: This API element is subject to removal in a future version.
      inline() is deprecated, since the place where the handler is used decides how the content is served. A custom handler names its content with DownloadEvent.fileName(String), and the place of use serves it inline or as an attachment with a DownloadDisposition on the registered resource.
      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