Class ClassDownloadHandler

All Implemented Interfaces:
DownloadHandler, ElementRequestHandler, Serializable

public class ClassDownloadHandler extends AbstractDownloadHandler<ClassDownloadHandler>
Download handler for serving a class resource.

For instance for the file resources/com/example/ui/MyData.json and class com.example.ui.MyData the definition would be forClassResource(MyData.class, "MyData.json")

Byte range requests, which media players use to seek and browsers use to resume a download, are not answered by default. A resource is read as a stream from its start up to each range, and a resource in a packaged jar is usually compressed, so each seek inflates all the bytes before it again, which adds up for large media that a player fetches in many small ranges. Call enableRangeRequests() to answer them anyway, or use DownloadHandler.forFile(java.io.File), which answers ranges by seeking in the file.

Since:
24.8
See Also:
  • Constructor Details

    • ClassDownloadHandler

      public ClassDownloadHandler(Class<?> clazz, String resourceName)
      Create a class resource download handler with the resource name as the url postfix (file name).

      The downloaded file name and download URL postfix will be set to resourceName. If you want to use a different file name, use ClassDownloadHandler(Class, String, String) instead.

      Parameters:
      clazz - class to use for getting resource
      resourceName - resource to get
    • ClassDownloadHandler

      public ClassDownloadHandler(Class<?> clazz, String resourceName, String fileName)
      Create a class resource download handler with the given file name as the url postfix.

      The downloaded file name and download URL postfix will be set to fileName.

      Parameters:
      clazz - class to use for getting resource
      resourceName - resource to get
      fileName - download file name that overrides resourceName and also used as a download request URL postfix
  • Method Details

    • handleDownloadRequest

      public void handleDownloadRequest(DownloadEvent downloadEvent) throws IOException
      Description copied from interface: DownloadHandler
      Method that is called when the client wants to download from the url stored for this specific handler registration.

      Once this method has been called, the download is no longer bound to the owner component's lifecycle and is not interrupted by detaching the owner or by closing the browser tab, see the class-level documentation.

      Parameters:
      downloadEvent - download event containing the necessary data for writing the response
      Throws:
      IOException - if an IO error occurred during download
    • enableRangeRequests

      public ClassDownloadHandler enableRangeRequests()
      Enables answering byte range requests, which media players use to seek and browsers use to resume a download. Safari does not play audio or video without them.

      Each range is read by skipping the resource up to its start. For a file on disk that is a seek, but an entry of a packaged jar is usually compressed and has to be inflated from its start for every range, which is costly for large media that a player fetches in many small ranges.

      Returns:
      this instance for method chaining
    • isRangeRequestsEnabled

      public boolean isRangeRequestsEnabled()
      Returns whether this handler answers byte range requests, which media players use to seek and browsers use to resume a download. Safari does not play audio or video from a handler that does not answer them.

      A handler that does not answer them sends the whole content for every request. A handler that does may still send the whole content for a particular response, for example when the length of its content is not known. The default implementation returns false.

      In development mode, a built-in handler logs a warning the first time that a handler of its class serves the whole content for a byte range request, for example because ranges are not enabled or the length of the content is unknown.

      Range requests are answered after enableRangeRequests().

      Returns:
      true if the handler is configured to answer byte range requests
    • getUrlPostfix

      public String getUrlPostfix()
      Description copied from interface: ElementRequestHandler
      Optional URL postfix allows appending an application-controlled string, e.g. the logical name of the target file, to the end of the otherwise random-looking download URL. If defined, requests that would otherwise be routable are still rejected if the postfix is missing or invalid. Postfix changes the last segment in the resource url.
      Returns:
      String optional URL postfix, or null for "".