Class MultipartParser

java.lang.Object
com.blackrook.small.multipart.MultipartParser
All Implemented Interfaces:
Iterable<Part>
Direct Known Subclasses:
MultipartFormDataParser

public abstract class MultipartParser extends Object implements Iterable<Part>
Abstract Multipart parser.
Author:
Matthew Tropiano
  • Field Details

  • 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 directory
      startBoundary - 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

      protected void parseDisposition(String line, Part part) throws MultipartParserException
      Throws:
      MultipartParserException
    • parseContentType

      protected void parseContentType(String line, Part part)
    • scanLine

      protected String scanLine(InputStream in) throws IOException
      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

      protected File generateTempFile(String filename, File outputDir) throws IOException
      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

      protected void addPart(Part part)
      Adds a part to the multipart parser.
      Parameters:
      part - the part to add.
    • getPartList

      public List<Part> getPartList()
      Returns:
      all of the parts parsed by this parser.
    • iterator

      public Iterator<Part> iterator()
      Specified by:
      iterator in interface Iterable<Part>