Class MultipartRequest


  • public class MultipartRequest
    extends java.lang.Object
    The class MultipartRequest allows servlets to process file uploads. Formally speaking, it supports requests with multipart/form-data content type. This content type is used to submit forms that has the multipart/form-data encoding type, which is used to upload files and is not directly supported by the Servlet Specification.

    The class MultipartRequest takes an HttpServletRequest, parses it extracting any parameters and files and exposes them through an API. Notice that the class MultipartRequest supports regular requests as well so that it is possible to process any request using a single API.

    File parameters are passed as MultipartRequest.File objects, which encapsulates the file's properties and contents. Regular parameters are passed as String objects.

    Notice that the class MultipartRequest supports a simplified version of MIME entity headers, specifically it does not support character escaping, header wrapping, comments nor any extensions. Also, it assumes that parameters are sent using the western (iso-8859-1) character set. Finally, it does not support multipart/mixed parts, which are used to send multiple files as a single parameter, and it assumes that the request is well formed, so no error checking is performed.

    Since:
    MicroStrategy Web 7.3.1 or earlier
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  MultipartRequest.File
      The class MultipartRequest.File encapsulates uploaded files.
    • Constructor Summary

      Constructors 
      Constructor Description
      MultipartRequest()
      Creates a new, empty MultipartRequest with a default expiration date.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void copyParameters​(ContainerStringCollection params)
      Copies all parameters from a request to this MultipartRequest.
      static MultipartRequest get​(int ID)
      Gets a MultipartRequest object given its ID.
      long getExpiration()
      Gets the expiration of this MultipartRequest object.
      java.lang.String getFile()
      Gets the file being uploaded.
      MultipartRequest.File getFileParameter​(java.lang.String name)
      Convenient method that returns the value of a MultipartRequest.File parameter.
      ContainerStringCollection getFormAndQueryStrings()
      Returns reqest parameter strings collection
      int getID()
      Gets the ID of this MultipartRequest.
      java.lang.Object getParameter​(java.lang.String name)
      Returns the value of a given parameter, or null if the parameter doesn't exist.
      java.util.Iterator getParameterNames()
      Returns an Iterator that iterates over the names of the parameters contained in this MultipartRequest.
      java.lang.Object[] getParameterValues​(java.lang.String name)
      Returns an array of objects containing all of the values the given request parameter has, or null if the parameter does not exist.
      int getProcessed()
      Gets the number of bytes of the request's body already processed.
      java.lang.String getStringParameter​(java.lang.String name)
      Convenient method that returns the value of a String parameter.
      int getTotal()
      Gets the total number of bytes of the request.
      void parseRequest​(java.lang.String contentType, java.io.InputStream requestStream)
      Parses a request, populating this MultipartRequest.
      void release()
      Releases this MultipartRequest object and all of its parameters.
      void setExpiration​(long expiration)
      Sets the expiration date for this MultipartRequest.
      void setTotal​(int total)
      Sets the total number of bytes of the request.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MultipartRequest

        public MultipartRequest()
        Creates a new, empty MultipartRequest with a default expiration date.
    • Method Detail

      • copyParameters

        public void copyParameters​(ContainerStringCollection params)
        Copies all parameters from a request to this MultipartRequest.
        Parameters:
        params - request parameters
      • parseRequest

        public void parseRequest​(java.lang.String contentType,
                                 java.io.InputStream requestStream)
                          throws java.io.IOException
        Parses a request, populating this MultipartRequest. If the content type of the request is multipart/form-data, parses it, extracting any parameters and files, populating this object.
        Parameters:
        contentType - content type
        requestStream - an input stream representing request to parse
        Throws:
        java.io.IOException - if an error occurs while reading the request, writing to temporary files, or if the pushback buffer is too small
      • setTotal

        public void setTotal​(int total)
        Sets the total number of bytes of the request.
        Parameters:
        total - total number of bytes
      • get

        public static MultipartRequest get​(int ID)
        Gets a MultipartRequest object given its ID.
        Parameters:
        ID - object ID
        Returns:
        the object with the given ID or null if it doesn't exist or if it was released
      • getExpiration

        public long getExpiration()
        Gets the expiration of this MultipartRequest object.
        Returns:
        the expiration of this object
      • getFile

        public java.lang.String getFile()
        Gets the file being uploaded.
        Returns:
        the name of the file being uploaded or an empty string if there is no file being uploaded
      • getFileParameter

        public MultipartRequest.File getFileParameter​(java.lang.String name)
        Convenient method that returns the value of a MultipartRequest.File parameter. If the parameter has multiple values, returns just the first one. Use the method getParameterValues(String) to get all values.
        Parameters:
        name - name of the desired parameter
        Returns:
        the value of the given parameter, casted to a MultipartRequest.File, or null if the parameter does not exist
      • getID

        public int getID()
        Gets the ID of this MultipartRequest. Each object is guaranteed to have a unique ID.
        Returns:
        the ID of this object
      • getParameter

        public java.lang.Object getParameter​(java.lang.String name)
        Returns the value of a given parameter, or null if the parameter doesn't exist. The value of the parameter is a String or a MultipartRequest.File object. If the parameter has multiple values, returns just the first one. Use the method getParameterValues(String) to get all values.
        Parameters:
        name - name of the desired parameter
        Returns:
        the value of the given parameter
      • getParameterNames

        public java.util.Iterator getParameterNames()
        Returns an Iterator that iterates over the names of the parameters contained in this MultipartRequest. The names of the parameters are String objects.
        Returns:
        the names of the parameters, as an Iterator
      • getParameterValues

        public java.lang.Object[] getParameterValues​(java.lang.String name)
        Returns an array of objects containing all of the values the given request parameter has, or null if the parameter does not exist. The values of the parameters are String or MultipartRequest.File objects.
        Parameters:
        name - name of the parameter desired
        Returns:
        the values of the requested parameter
      • getProcessed

        public int getProcessed()
        Gets the number of bytes of the request's body already processed. This method can be called by another thread.
        Returns:
        number of bytes already processed
      • getStringParameter

        public java.lang.String getStringParameter​(java.lang.String name)
        Convenient method that returns the value of a String parameter. If the parameter has multiple values, returns just the first one. Use the method getParameterValues(String) to get all values.
        Parameters:
        name - name of the desired parameter
        Returns:
        the value of the given parameter, casted to a String, or null if the parameter does not exist
      • getTotal

        public int getTotal()
        Gets the total number of bytes of the request.
        Returns:
        the total number of bytes
      • release

        public void release()
                     throws java.io.IOException
        Releases this MultipartRequest object and all of its parameters. This method should be called when this object is not needed anymore. If this object is not explicitly released before its expiration, it will be automatically released when it expires.
        Throws:
        java.io.IOException - if an error occurs while releasing the temporary files
      • setExpiration

        public void setExpiration​(long expiration)
        Sets the expiration date for this MultipartRequest. The expiration date is specified using the same base time as the one used by the method System.currentTimeMillis().
        Parameters:
        expiration - expiration date
      • getFormAndQueryStrings

        public ContainerStringCollection getFormAndQueryStrings()
        Returns reqest parameter strings collection
        Returns:
        reqest parameter strings collection