Class AbstractUploadHandler<R extends AbstractUploadHandler>

java.lang.Object
com.vaadin.flow.server.streams.TransferProgressAwareHandler<UploadEvent,R>
com.vaadin.flow.server.streams.AbstractUploadHandler<R>
Type Parameters:
R - type of the subclass implementing this class, for method chaining
All Implemented Interfaces:
ElementRequestHandler, UploadHandler, Serializable
Direct Known Subclasses:
AbstractFileUploadHandler, InMemoryUploadHandler

public abstract class AbstractUploadHandler<R extends AbstractUploadHandler> extends TransferProgressAwareHandler<UploadEvent,R> implements UploadHandler
Base class for the pre-made upload handlers, adding support for synchronous UploadValidators on top of the transfer progress handling.

Validators are held here, rather than on TransferProgressAwareHandler, so that they apply only to uploads and never to downloads.

See Also:
  • Constructor Details

    • AbstractUploadHandler

      public AbstractUploadHandler()
  • Method Details

    • withValidator

      public R withValidator(UploadValidator validator)
      Adds a validator that can reject the upload while it is being received.

      The validator's phase methods are invoked synchronously on the request thread; calling UploadEvent.reject(String) from any of them aborts the upload and prevents the success callback from being invoked. Validators are invoked in the order they are added; the first rejection stops further reading.

      Parameters:
      validator - the validator to add, not null
      Returns:
      this instance for method chaining
    • validateMetadata

      public R validateMetadata(UploadMetadataCallback callback)
      Adds a validator that inspects only the upload metadata, before any data is read.
      Parameters:
      callback - the metadata validation callback, not null
      Returns:
      this instance for method chaining
      See Also:
    • validateHeader

      public R validateHeader(int maxBytes, UploadHeaderCallback callback)
      Adds a validator that inspects the first maxBytes bytes of the upload, for example a file-signature ("magic byte") or MIME-sniffing check. The validator is invoked once with the assembled leading bytes and can reject the upload after only those bytes have been read.
      Parameters:
      maxBytes - the number of leading bytes to inspect, must be positive
      callback - the header validation callback, not null
      Returns:
      this instance for method chaining
      See Also:
    • validateComplete

      public R validateComplete(UploadCompleteCallback callback)
      Adds a validator that inspects the fully received upload, before it is delivered, for example an antivirus scan.
      Parameters:
      callback - the complete validation callback, not null
      Returns:
      this instance for method chaining
      See Also:
    • runMetadataValidators

      protected void runMetadataValidators(UploadEvent event) throws IOException
      Runs the metadata phase of the registered validators, before any data is read. Stops at the first rejection.
      Parameters:
      event - the upload being handled
      Throws:
      IOException - if a validator fails
    • applyHeaderValidators

      protected InputStream applyHeaderValidators(UploadEvent event, InputStream in) throws IOException
      Runs the header phase of the registered validators: reads the leading bytes (up to the largest requested UploadValidator.headerSize()), gives each header validator a read-only view of its first headerSize() bytes, and returns a stream that replays those bytes followed by the rest of in. Returns in unchanged when no header validation is requested or once the upload is rejected. Stops at the first rejection.
      Parameters:
      event - the upload being handled
      in - the upload input stream
      Returns:
      the stream to transfer, with the consumed header spliced back in
      Throws:
      IOException - if reading the header or a validator fails
    • runCompleteValidators

      protected void runCompleteValidators(UploadEvent event, UploadContent content) throws IOException
      Runs the complete phase of the registered validators against the fully received content. Stops at the first rejection.
      Parameters:
      event - the upload being handled
      content - handle to the received content
      Throws:
      IOException - if a validator fails
    • hasValidators

      protected boolean hasValidators()
      Whether any validators are registered. The handlers use this to skip the complete-validation phase (and building an UploadContent) entirely when there is nothing to validate.
      Returns:
      true if at least one validator is registered
    • getTransferContext

      protected TransferContext getTransferContext(UploadEvent event)
      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<UploadEvent,R extends AbstractUploadHandler>
      Parameters:
      event - the transfer event
      Returns:
      the transfer context