Package com.vaadin.flow.server.streams
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.vaadin.flow.server.streams.UploadHandler
UploadHandler.MixedUploadResponse, UploadHandler.RejectedFilesResponse -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected InputStreamapplyHeaderValidators(UploadEvent event, InputStream in) Runs the header phase of the registered validators: reads the leading bytes (up to the largest requestedUploadValidator.headerSize()), gives each header validator a read-only view of its firstheaderSize()bytes, and returns a stream that replays those bytes followed by the rest ofin.protected TransferContextgetTransferContext(UploadEvent event) This method is used to get the transfer context from the transfer events (e.g.protected booleanWhether any validators are registered.protected voidrunCompleteValidators(UploadEvent event, UploadContent content) Runs the complete phase of the registered validators against the fully received content.protected voidrunMetadataValidators(UploadEvent event) Runs the metadata phase of the registered validators, before any data is read.validateComplete(UploadCompleteCallback callback) Adds a validator that inspects the fully received upload, before it is delivered, for example an antivirus scan.validateHeader(int maxBytes, UploadHeaderCallback callback) Adds a validator that inspects the firstmaxBytesbytes of the upload, for example a file-signature ("magic byte") or MIME-sniffing check.validateMetadata(UploadMetadataCallback callback) Adds a validator that inspects only the upload metadata, before any data is read.withValidator(UploadValidator validator) Adds a validator that can reject the upload while it is being received.Methods inherited from class com.vaadin.flow.server.streams.TransferProgressAwareHandler
addTransferProgressListener, getListeners, notifyError, onProgress, onProgress, onProgress, onProgress, setTransferUI, uiAccess, whenComplete, whenComplete, whenStart, whenStartMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.server.streams.ElementRequestHandler
getDisabledUpdateMode, getUrlPostfix, isAllowInertMethods inherited from interface com.vaadin.flow.server.streams.UploadHandler
getFileCountMax, getFileSizeMax, getRequestSizeMax, handleRequest, handleUploadRequest, responseHandled
-
Constructor Details
-
AbstractUploadHandler
public AbstractUploadHandler()
-
-
Method Details
-
withValidator
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, notnull- Returns:
- this instance for method chaining
-
validateMetadata
Adds a validator that inspects only the upload metadata, before any data is read.- Parameters:
callback- the metadata validation callback, notnull- Returns:
- this instance for method chaining
- See Also:
-
validateHeader
Adds a validator that inspects the firstmaxBytesbytes 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 positivecallback- the header validation callback, notnull- Returns:
- this instance for method chaining
- See Also:
-
validateComplete
Adds a validator that inspects the fully received upload, before it is delivered, for example an antivirus scan.- Parameters:
callback- the complete validation callback, notnull- Returns:
- this instance for method chaining
- See Also:
-
runMetadataValidators
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
Runs the header phase of the registered validators: reads the leading bytes (up to the largest requestedUploadValidator.headerSize()), gives each header validator a read-only view of its firstheaderSize()bytes, and returns a stream that replays those bytes followed by the rest ofin. Returnsinunchanged when no header validation is requested or once the upload is rejected. Stops at the first rejection.- Parameters:
event- the upload being handledin- 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
Runs the complete phase of the registered validators against the fully received content. Stops at the first rejection.- Parameters:
event- the upload being handledcontent- 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 anUploadContent) entirely when there is nothing to validate.- Returns:
trueif at least one validator is registered
-
getTransferContext
Description copied from class:TransferProgressAwareHandlerThis method is used to get the transfer context from the transfer events (e.g.DownloadEvent).- Specified by:
getTransferContextin classTransferProgressAwareHandler<UploadEvent,R extends AbstractUploadHandler> - Parameters:
event- the transfer event- Returns:
- the transfer context
-