Search in sources :

Example 1 with DiskFileUpload

use of org.apache.commons.fileupload.DiskFileUpload in project sonarqube by SonarSource.

the class CommonsMultipartRequestHandler method handleRequest.

/**
     * <p> Parses the input stream and partitions the parsed items into a set
     * of form fields and a set of file items. In the process, the parsed
     * items are translated from Commons FileUpload <code>FileItem</code>
     * instances to Struts <code>FormFile</code> instances. </p>
     *
     * @param request The multipart request to be processed.
     * @throws ServletException if an unrecoverable error occurs.
     */
public void handleRequest(HttpServletRequest request) throws ServletException {
    // Get the app config for the current request.
    ModuleConfig ac = (ModuleConfig) request.getAttribute(Globals.MODULE_KEY);
    // Create and configure a DIskFileUpload instance.
    DiskFileUpload upload = new DiskFileUpload();
    // The following line is to support an "EncodingFilter"
    // see http://issues.apache.org/bugzilla/show_bug.cgi?id=23255
    upload.setHeaderEncoding(request.getCharacterEncoding());
    // Set the maximum size before a FileUploadException will be thrown.
    upload.setSizeMax(getSizeMax(ac));
    // Set the maximum size that will be stored in memory.
    upload.setSizeThreshold((int) getSizeThreshold(ac));
    // Set the the location for saving data on disk.
    upload.setRepositoryPath(getRepositoryPath(ac));
    // Create the hash tables to be populated.
    elementsText = new Hashtable();
    elementsFile = new Hashtable();
    elementsAll = new Hashtable();
    // Parse the request into file items.
    List items = null;
    try {
        items = upload.parseRequest(request);
    } catch (DiskFileUpload.SizeLimitExceededException e) {
        // Special handling for uploads that are too big.
        request.setAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED, Boolean.TRUE);
        return;
    } catch (FileUploadException e) {
        log.error("Failed to parse multipart request", e);
        throw new ServletException(e);
    }
    // Partition the items into form fields and files.
    Iterator iter = items.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();
        if (item.isFormField()) {
            addTextParameter(request, item);
        } else {
            addFileParameter(item);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) DiskFileUpload(org.apache.commons.fileupload.DiskFileUpload) Hashtable(java.util.Hashtable) Iterator(java.util.Iterator) ModuleConfig(org.apache.struts.config.ModuleConfig) ArrayList(java.util.ArrayList) List(java.util.List) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ServletException (javax.servlet.ServletException)1 DiskFileUpload (org.apache.commons.fileupload.DiskFileUpload)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)1 ModuleConfig (org.apache.struts.config.ModuleConfig)1