Class AbstractMultiPartRequest

java.lang.Object
org.apache.struts2.dispatcher.multipart.AbstractMultiPartRequest
All Implemented Interfaces:
MultiPartRequest
Direct Known Subclasses:
JakartaMultiPartRequest, JakartaStreamMultiPartRequest

public abstract class AbstractMultiPartRequest extends Object implements MultiPartRequest
Abstract class with some helper methods, it should be used when starting development of another implementation of MultiPartRequest
  • Field Details

    • STRUTS_MESSAGES_UPLOAD_ERROR_PARAMETER_TOO_LONG_KEY

      protected static final String STRUTS_MESSAGES_UPLOAD_ERROR_PARAMETER_TOO_LONG_KEY
      See Also:
    • BUFFER_SIZE

      public static final int BUFFER_SIZE
      Defines the internal buffer size used during streaming operations.
      See Also:
    • errors

      protected List<LocalizedMessage> errors
      Internal list of raised errors to be passed to the Struts2 framework.
    • maxSize

      protected Long maxSize
      Specifies the maximum size of the entire request.
    • maxSizeOfFiles

      protected Long maxSizeOfFiles
      Specifies the maximum size of all the uploaded files.
    • maxFiles

      protected Long maxFiles
      Specifies the maximum number of files in one request.
    • maxStringLength

      protected Long maxStringLength
      Specifies the maximum length of a string parameter in a multipart request.
    • maxFileSize

      protected Long maxFileSize
      Specifies the maximum size per a file in the request.
    • bufferSize

      protected int bufferSize
      Specifies the buffer size to use during streaming.
    • defaultEncoding

      protected String defaultEncoding
      Defines default encoding to encode data from request used if not provided with request
    • uploadedFiles

      protected Map<String,List<UploadedFile>> uploadedFiles
      Map between file fields and file data.
    • parameters

      protected Map<String,List<String>> parameters
      Map between non-file fields and values.
  • Constructor Details

    • AbstractMultiPartRequest

      public AbstractMultiPartRequest()
  • Method Details

    • setBufferSize

      public void setBufferSize(String bufferSize)
      Parameters:
      bufferSize - Sets the buffer size to be used.
    • setDefaultEncoding

      public void setDefaultEncoding(String enc)
    • setMaxSize

      public void setMaxSize(String maxSize)
      Parameters:
      maxSize - Injects the Struts multipart request maximum size.
    • setMaxSizeOfFiles

      public void setMaxSizeOfFiles(String maxSizeOfFiles)
      Parameters:
      maxSizeOfFiles - Injects the Struts maximum size of all uploaded files.
    • setMaxFiles

      public void setMaxFiles(String maxFiles)
      Parameters:
      maxFiles - Injects the Struts maximum size of an individual file uploaded.
    • setMaxFileSize

      public void setMaxFileSize(String maxFileSize)
      Parameters:
      maxFileSize - Injects the Struts maximum number of files, which can be uploaded.
    • setMaxStringLength

      public void setMaxStringLength(String maxStringLength)
      Parameters:
      maxStringLength - Injects the Struts maximum size of single form field.
    • processUpload

      protected abstract void processUpload(jakarta.servlet.http.HttpServletRequest request, String saveDir) throws IOException
      Process the request extract file upload data
      Parameters:
      request - current HttpServletRequest
      saveDir - a temporary directory to store files
      Throws:
      IOException
    • readCharsetEncoding

      protected Charset readCharsetEncoding(jakarta.servlet.http.HttpServletRequest request)
      Parameters:
      request - multipart request
      Returns:
      character encoding from request or defaultEncoding
    • createJakartaFileUpload

      protected abstract org.apache.commons.fileupload2.jakarta.servlet6.JakartaServletDiskFileUpload createJakartaFileUpload(Charset charset, Path saveDir)
      Creates an instance of JakartaServletDiskFileUpload used by the parser to extract uploaded files
      Parameters:
      charset - used charset from incoming request
      saveDir - a temporary folder to store uploaded files (not always needed)
    • prepareServletFileUpload

      protected org.apache.commons.fileupload2.jakarta.servlet6.JakartaServletDiskFileUpload prepareServletFileUpload(Charset charset, Path saveDir)
    • exceedsMaxStringLength

      protected boolean exceedsMaxStringLength(String fieldName, String fieldValue)
    • parse

      public void parse(jakarta.servlet.http.HttpServletRequest request, String saveDir) throws IOException
      Processes the upload.
      Specified by:
      parse in interface MultiPartRequest
      Parameters:
      request - the servlet request
      saveDir - location of the save dir
      Throws:
      IOException
    • buildErrorMessage

      protected LocalizedMessage buildErrorMessage(Class<? extends Throwable> exceptionClass, String defaultMessage, Object[] args)
      Build error message.
      Parameters:
      exceptionClass - a class of the exception
      defaultMessage - a default message to use
      args - arguments
      Returns:
      error message
    • getCanonicalName

      protected String getCanonicalName(String originalFileName)
      Parameters:
      originalFileName - file name
      Returns:
      the canonical name based on the supplied filename
    • sanitizeNewlines

      protected String sanitizeNewlines(String before)
    • getErrors

      public List<LocalizedMessage> getErrors()
      Description copied from interface: MultiPartRequest
      Returns a list of error messages that may have occurred while processing the request. If there are no errors, an empty list is returned. If the underlying implementation (ie: cos, jakarta, etc) cannot support providing these errors, an empty list is also returned. This list of errors is reported back to the MultiPartRequestWrapper's errors field.
      Specified by:
      getErrors in interface MultiPartRequest
      Returns:
      a list of Strings that represent various errors during parsing
    • getFileParameterNames

      public Enumeration<String> getFileParameterNames()
      Description copied from interface: MultiPartRequest
      Returns an enumeration of the parameter names for uploaded files
      Specified by:
      getFileParameterNames in interface MultiPartRequest
      Returns:
      an enumeration of the parameter names for uploaded files
    • getContentType

      public String[] getContentType(String fieldName)
      Description copied from interface: MultiPartRequest
      Returns the content type(s) of the file(s) associated with the specified field name (as supplied by the client browser), or null if no files are associated with the given field name.
      Specified by:
      getContentType in interface MultiPartRequest
      Parameters:
      fieldName - input field name
      Returns:
      an array of content encoding for the specified input field name or null if no content type was specified.
    • getFile

      public UploadedFile[] getFile(String fieldName)
      Description copied from interface: MultiPartRequest
      Returns a UploadedFile object for the filename specified or null if no files are associated with the given field name.
      Specified by:
      getFile in interface MultiPartRequest
      Parameters:
      fieldName - input field name
      Returns:
      a UploadedFile[] object for files associated with the specified input field name
    • getFileNames

      public String[] getFileNames(String fieldName)
      Description copied from interface: MultiPartRequest
      Returns a String[] of file names for files associated with the specified input field name
      Specified by:
      getFileNames in interface MultiPartRequest
      Parameters:
      fieldName - input field name
      Returns:
      a String[] of file names for files associated with the specified input field name
    • getFilesystemName

      public String[] getFilesystemName(String fieldName)
      Description copied from interface: MultiPartRequest
      Returns the file system name(s) of files associated with the given field name or null if no files are associated with the given field name.
      Specified by:
      getFilesystemName in interface MultiPartRequest
      Parameters:
      fieldName - input field name
      Returns:
      the file system name(s) of files associated with the given field name
    • getParameter

      public String getParameter(String name)
      Description copied from interface: MultiPartRequest
      Returns the specified request parameter.
      Specified by:
      getParameter in interface MultiPartRequest
      Parameters:
      name - the name of the parameter to get
      Returns:
      the parameter or null if it was not found.
    • getParameterNames

      public Enumeration<String> getParameterNames()
      Description copied from interface: MultiPartRequest
      Returns an enumeration of String parameter names.
      Specified by:
      getParameterNames in interface MultiPartRequest
      Returns:
      an enumeration of String parameter names.
    • getParameterValues

      public String[] getParameterValues(String name)
      Description copied from interface: MultiPartRequest
      Returns a list of all parameter values associated with a parameter name. If there is only one parameter value per name the resulting array will be of length 1. If the parameter doesn't exist, null should be returned instead of empty array.
      Specified by:
      getParameterValues in interface MultiPartRequest
      Parameters:
      name - the name of the parameter.
      Returns:
      an array of all values associated with the parameter name.
    • cleanUp

      public void cleanUp()
      Description copied from interface: MultiPartRequest
      Cleans up all uploaded file, should be called at the end of request
      Specified by:
      cleanUp in interface MultiPartRequest