Package com.jsgalactic.axiom.http
Class MultipartParser
java.lang.Object
com.jsgalactic.axiom.http.MultipartParser
- All Implemented Interfaces:
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordBounds applied while a multipart body is read.static interfaceReceives parts as they are completed by the parser.static final recordOne complete part: its headers, disposition parameters and bounded content. -
Constructor Summary
ConstructorsConstructorDescriptionMultipartParser(BodyReader source, String contentType, MultipartParser.Limits limits, MultipartParser.Listener listener) Creates a parser that uses the system temporary directory for spilled parts.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. -
Method Summary
-
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 callercontentType- request or part Content-Type containing the boundary parameterlimits- explicit boundslistener- 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 callercontentType- request Content-Type containing the boundary parameterlimits- explicit boundslistener- receives completed partsdirectory- existing directory for temporary files, or null for the system directory- Throws:
MultipartException- if the media type or boundary is missing or malformedIllegalArgumentException- if the directory does not exist or is not writable
-
-
Method Details
-
parse
Reads the body to its closing boundary, delivering each complete part to the listener. Calling it twice, or calling it afterclose(), fails.- Throws:
IOException- if the source fails, the listener fails or a limit is exceededMultipartException- if the framing is malformedIllegalStateException- 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:
closein interfaceAutoCloseable
-