Class MultiPartFormData

All Implemented Interfaces:
CompletionStage<MultiPartFormData.Parts>, Future<MultiPartFormData.Parts>

public class MultiPartFormData extends CompletableFuture<MultiPartFormData.Parts>

A CompletableFuture that is completed when a multipart/form-data content has been parsed asynchronously from a Content.Source via parse(Content.Source) or from one or more Content.Chunks via parse(Content.Chunk).

Once the parsing of the multipart/form-data content completes successfully, objects of this class are completed with a MultiPartFormData.Parts object.

Objects of this class may be configured to save multipart files in a configurable directory, and configure the max size of such files, etc.

Typical usage:


 // Some headers that include Content-Type.
 HttpFields headers = ...;
 String boundary = MultiPart.extractBoundary(headers.get(HttpHeader.CONTENT_TYPE));

 // Some multipart/form-data content.
 Content.Source content = ...;

 // Create and configure MultiPartFormData.
 MultiPartFormData formData = new MultiPartFormData(boundary);
 // Where to store the files.
 formData.setFilesDirectory(Path.of("/tmp"));
 // Max 1 MiB files.
 formData.setMaxFileSize(1024 * 1024);

 // Parse the content.
 formData.parse(content)
     // When complete, use the parts.
     .thenAccept(parts -> ...);
 
See Also:
  • Constructor Details

    • MultiPartFormData

      public MultiPartFormData(String boundary)
  • Method Details

    • getBoundary

      public String getBoundary()
      Returns:
      the boundary string
    • parse

      public MultiPartFormData parse(org.eclipse.jetty.io.Content.Source content)

      Parses the given multipart/form-data content.

      Returns this MultiPartFormData object, so that it can be used in the typical "fluent" style of CompletableFuture.

      Parameters:
      content - the multipart/form-data content to parse
      Returns:
      this MultiPartFormData object
    • parse

      public void parse(org.eclipse.jetty.io.Content.Chunk chunk)

      Parses the given chunk containing multipart/form-data bytes.

      One or more chunks may be passed to this method, until the parsing of the multipart/form-data content completes.

      Parameters:
      chunk - the Content.Chunk to parse.
    • getDefaultCharset

      public Charset getDefaultCharset()

      Returns the default charset as specified by RFC 7578, section 4.6, that is the charset specified by the part named _charset_.

      If that part is not present, returns null.

      Returns:
      the default charset specified by the _charset_ part, or null if that part is not present
    • getPartHeadersMaxLength

      public int getPartHeadersMaxLength()
      Returns:
      the max length of a MultiPart.Part headers, in bytes, or -1 for unlimited length
    • setPartHeadersMaxLength

      public void setPartHeadersMaxLength(int partHeadersMaxLength)
      Parameters:
      partHeadersMaxLength - the max length of a MultiPart.Part headers, in bytes, or -1 for unlimited length
    • isUseFilesForPartsWithoutFileName

      public boolean isUseFilesForPartsWithoutFileName()
      Returns:
      whether parts without fileName may be stored as files
    • setUseFilesForPartsWithoutFileName

      public void setUseFilesForPartsWithoutFileName(boolean useFilesForPartsWithoutFileName)
      Parameters:
      useFilesForPartsWithoutFileName - whether parts without fileName may be stored as files
    • getFilesDirectory

      public Path getFilesDirectory()
      Returns:
      the directory where files are saved
    • setFilesDirectory

      public void setFilesDirectory(Path filesDirectory)

      Sets the directory where the files uploaded in the parts will be saved.

      Parameters:
      filesDirectory - the directory where files are saved
    • getMaxFileSize

      public long getMaxFileSize()
      Returns:
      the maximum file size in bytes, or -1 for unlimited file size
    • setMaxFileSize

      public void setMaxFileSize(long maxFileSize)
      Parameters:
      maxFileSize - the maximum file size in bytes, or -1 for unlimited file size
    • getMaxMemoryFileSize

      public long getMaxMemoryFileSize()
      Returns:
      the maximum memory file size in bytes, or -1 for unlimited memory file size
    • setMaxMemoryFileSize

      public void setMaxMemoryFileSize(long maxMemoryFileSize)

      Sets the maximum memory file size in bytes, after which files will be saved in the directory specified by setFilesDirectory(Path).

      Use value 0 to always save the files in the directory.

      Use value -1 to never save the files in the directory.

      Parameters:
      maxMemoryFileSize - the maximum memory file size in bytes, or -1 for unlimited memory file size
    • getMaxLength

      public long getMaxLength()
      Returns:
      the maximum length in bytes of the whole multipart content, or -1 for unlimited length
    • setMaxLength

      public void setMaxLength(long maxLength)
      Parameters:
      maxLength - the maximum length in bytes of the whole multipart content, or -1 for unlimited length
    • completeExceptionally

      public boolean completeExceptionally(Throwable failure)
      Overrides:
      completeExceptionally in class CompletableFuture<MultiPartFormData.Parts>