Class MultipartParser

java.lang.Object
com.jsgalactic.axiom.http.MultipartParser
All Implemented Interfaces:
AutoCloseable

public final class MultipartParser extends Object implements AutoCloseable
Event-based parser for multipart/* request bodies read from a BodyReader. The parser drives the reader itself: parse() blocks until the closing boundary or a failure and calls MultipartParser.Listener.part(com.jsgalactic.axiom.http.MultipartParser.Part) once for each complete part, in wire order. It never buffers the whole body. Part content is held in memory up to a small threshold and then spilled to a temporary file; every file is deleted by close(), including after a parse failure or when the handler stops reading early. Part values are valid only while the parser is open.

All limits are explicit in MultipartParser.Limits and are checked while bytes arrive: the number of parts, bytes per part, bytes of each part's header section and total body bytes, including boundaries and preamble. An exceeded limit or malformed framing throws MultipartException, which a handler can let propagate as a 400. The caller owns the source reader and should close it separately.

  • Constructor Details

    • MultipartParser

      public MultipartParser(BodyReader source, String contentType, MultipartParser.Limits limits, MultipartParser.Listener listener)
      Creates a parser that uses the system temporary directory for spilled parts.
      Parameters:
      source - reader of the body; owned by the caller
      contentType - request or part Content-Type containing the boundary parameter
      limits - explicit bounds
      listener - receives completed parts
      Throws:
      MultipartException - if the media type or boundary is missing or malformed
    • MultipartParser

      public MultipartParser(BodyReader source, String contentType, MultipartParser.Limits limits, MultipartParser.Listener listener, Path directory)
      Creates a parser that spills parts into the given directory, so an application or a test can choose and observe the temporary storage.
      Parameters:
      source - reader of the body; owned by the caller
      contentType - request Content-Type containing the boundary parameter
      limits - explicit bounds
      listener - receives completed parts
      directory - existing directory for temporary files, or null for the system directory
      Throws:
      MultipartException - if the media type or boundary is missing or malformed
      IllegalArgumentException - if the directory does not exist or is not writable
  • Method Details

    • parse

      public void parse() throws IOException
      Reads the body to its closing boundary, delivering each complete part to the listener. Calling it twice, or calling it after close(), fails.
      Throws:
      IOException - if the source fails, the listener fails or a limit is exceeded
      MultipartException - if the framing is malformed
      IllegalStateException - if the parser was used already
    • close

      public void close()
      Deletes all temporary part files. Idempotent and safe to call on a failed parse or from a handler that stopped reading early. The source reader is not closed.
      Specified by:
      close in interface AutoCloseable