Package com.blackrook.small.multipart
Class MultipartParser
java.lang.Object
com.blackrook.small.multipart.MultipartParser
- Direct Known Subclasses:
MultipartFormDataParser
Abstract Multipart parser.
- Author:
- Matthew Tropiano
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final StringContent-Disposition.protected static final StringContent-Disposition.protected final StringNewline.protected final byte[]Newline as bytes.protected static final StringContent type boundary piece.protected static final StringContent type charset piece. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAdds a part to the multipart parser.protected FilegenerateTempFile(String filename, File outputDir) Creates a temporary file for read part data.static booleanisMultipart(javax.servlet.http.HttpServletRequest request) Checks if the request is a multipart form.iterator()voidParses the request content.protected voidparseContentType(String line, Part part) protected abstract voidparseData(javax.servlet.ServletInputStream inStream, File outputDir, String startBoundary, String endBoundary, byte[] startBoundaryBytes) Parses the servlet content and generates parts.protected voidparseDisposition(String line, Part part) protected intscanDataUntilBoundary(InputStream in, OutputStream out, byte[] boundaryBytes) Scans an input stream into an output stream until it hits a part boundary.protected StringscanLine(InputStream in) Scans and returns the next line.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
NEWLINE
Newline.- See Also:
-
NEWLINE_BYTES
protected final byte[] NEWLINE_BYTESNewline as bytes. -
PIECE_BOUNDARY
Content type boundary piece.- See Also:
-
PIECE_CHARSET
Content type charset piece.- See Also:
-
HEADER_DISPOSITION
Content-Disposition.- See Also:
-
HEADER_TYPE
Content-Disposition.- See Also:
-
-
Constructor Details
-
MultipartParser
public MultipartParser()Creates a new multipart parser.
-
-
Method Details
-
isMultipart
public static boolean isMultipart(javax.servlet.http.HttpServletRequest request) Checks if the request is a multipart form.- Parameters:
request- the servlet request.- Returns:
- true if a request is a multipart request, false otherwise.
-
parse
public void parse(javax.servlet.http.HttpServletRequest request, File outputDir) throws MultipartParserException, IOException Parses the request content.- Parameters:
request- the servlet request to parse.outputDir- the temporary directory for read files.- Throws:
MultipartParserException- if a parsing error occurs in parsing the content.IOException- if a read error occurs.
-
parseData
protected abstract void parseData(javax.servlet.ServletInputStream inStream, File outputDir, String startBoundary, String endBoundary, byte[] startBoundaryBytes) throws MultipartParserException, UnsupportedEncodingException, IOException Parses the servlet content and generates parts.- Parameters:
inStream- the input servlet stream.outputDir- the output directorystartBoundary- the boundary string for each part.endBoundary- the last boundary string.startBoundaryBytes- the boundary string for each part as bytes in the correct encoding.- Throws:
MultipartParserException- if something is malformed in the request body.UnsupportedEncodingException- if the part encoding is not supported.IOException- if a read error occurs.
-
parseDisposition
- Throws:
MultipartParserException
-
parseContentType
-
scanLine
Scans and returns the next line.- Parameters:
in- the servlet input stream (for request content).- Returns:
- the read string.
- Throws:
IOException- if the stream could not be read.
-
scanDataUntilBoundary
protected int scanDataUntilBoundary(InputStream in, OutputStream out, byte[] boundaryBytes) throws IOException Scans an input stream into an output stream until it hits a part boundary.- Parameters:
in- the input stream.out- the output stream.boundaryBytes- the boundary as bytes.- Returns:
- the amount of bytes read.
- Throws:
IOException- if the input stream could not be read or the output stream could not be written to.
-
generateTempFile
Creates a temporary file for read part data.- Parameters:
filename- the Part's file name (extension is pulled from this).outputDir- the temporary output directory.- Returns:
- the file created.
- Throws:
IOException- if the canonical path could not be read.
-
addPart
Adds a part to the multipart parser.- Parameters:
part- the part to add.
-
getPartList
- Returns:
- all of the parts parsed by this parser.
-
iterator
-