Search in sources :

Example 26 with FileUploadException

use of org.apache.commons.fileupload.FileUploadException in project sling by apache.

the class ParameterSupport method getRequestParameterMapInternal.

private ParameterMap getRequestParameterMapInternal() {
    if (this.postParameterMap == null) {
        // SLING-508 Try to force servlet container to decode parameters
        // as ISO-8859-1 such that we can recode later
        String encoding = getServletRequest().getCharacterEncoding();
        if (encoding == null) {
            encoding = Util.ENCODING_DIRECT;
            try {
                getServletRequest().setCharacterEncoding(encoding);
            } catch (UnsupportedEncodingException uee) {
                throw new SlingUnsupportedEncodingException(uee);
            }
        }
        // SLING-152 Get parameters from the servlet Container
        ParameterMap parameters = new ParameterMap();
        // fallback is only used if this request has been started by a service call
        boolean useFallback = getServletRequest().getAttribute(MARKER_IS_SERVICE_PROCESSING) != null;
        boolean addContainerParameters = false;
        // Query String
        final String query = getServletRequest().getQueryString();
        if (query != null) {
            try {
                InputStream input = Util.toInputStream(query);
                Util.parseQueryString(input, encoding, parameters, false);
                addContainerParameters = checkForAdditionalParameters;
            } catch (IllegalArgumentException e) {
                this.log.error("getRequestParameterMapInternal: Error parsing request", e);
            } catch (UnsupportedEncodingException e) {
                throw new SlingUnsupportedEncodingException(e);
            } catch (IOException e) {
                this.log.error("getRequestParameterMapInternal: Error parsing request", e);
            }
            useFallback = false;
        } else {
            addContainerParameters = checkForAdditionalParameters;
            useFallback = true;
        }
        // POST requests
        final boolean isPost = "POST".equals(this.getServletRequest().getMethod());
        if (isPost) {
            // WWW URL Form Encoded POST
            if (isWWWFormEncodedContent(this.getServletRequest())) {
                try {
                    InputStream input = this.getServletRequest().getInputStream();
                    Util.parseQueryString(input, encoding, parameters, false);
                    addContainerParameters = checkForAdditionalParameters;
                } catch (IllegalArgumentException e) {
                    this.log.error("getRequestParameterMapInternal: Error parsing request", e);
                } catch (UnsupportedEncodingException e) {
                    throw new SlingUnsupportedEncodingException(e);
                } catch (IOException e) {
                    this.log.error("getRequestParameterMapInternal: Error parsing request", e);
                }
                this.requestDataUsed = true;
                useFallback = false;
            }
            // Multipart POST
            if (ServletFileUpload.isMultipartContent(new ServletRequestContext(this.getServletRequest()))) {
                if (isStreamed(parameters, this.getServletRequest())) {
                    // special case, the request is Multipart and streamed processing has been requested
                    try {
                        this.getServletRequest().setAttribute(REQUEST_PARTS_ITERATOR_ATTRIBUTE, new RequestPartsIterator(this.getServletRequest()));
                        this.log.debug("getRequestParameterMapInternal: Iterator<javax.servlet.http.Part> available as request attribute named request-parts-iterator");
                    } catch (IOException e) {
                        this.log.error("getRequestParameterMapInternal: Error parsing multipart streamed request", e);
                    } catch (FileUploadException e) {
                        this.log.error("getRequestParameterMapInternal: Error parsing multipart streamed request", e);
                    }
                    // The request data has been passed to the RequestPartsIterator, hence from a RequestParameter pov its been used, and must not be used again.
                    this.requestDataUsed = true;
                    // must not try and get anything from the request at this point so avoid jumping through the stream.
                    addContainerParameters = false;
                    useFallback = false;
                } else {
                    this.parseMultiPartPost(parameters);
                    this.requestDataUsed = true;
                    addContainerParameters = checkForAdditionalParameters;
                    useFallback = false;
                }
            }
        }
        if (useFallback) {
            getContainerParameters(parameters, encoding, true);
        } else if (addContainerParameters) {
            getContainerParameters(parameters, encoding, false);
        }
        // apply any form encoding (from '_charset_') in the parameter map
        Util.fixEncoding(parameters);
        this.postParameterMap = parameters;
    }
    return this.postParameterMap;
}
Also used : InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) RequestParameterMap(org.apache.sling.api.request.RequestParameterMap) IOException(java.io.IOException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 27 with FileUploadException

use of org.apache.commons.fileupload.FileUploadException in project sling by apache.

the class ParameterSupport method parseMultiPartPost.

private void parseMultiPartPost(ParameterMap parameters) {
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload();
    upload.setSizeMax(ParameterSupport.maxRequestSize);
    upload.setFileSizeMax(ParameterSupport.maxFileSize);
    upload.setFileItemFactory(new DiskFileItemFactory(ParameterSupport.fileSizeThreshold, ParameterSupport.location));
    RequestContext rc = new ServletRequestContext(this.getServletRequest()) {

        @Override
        public String getCharacterEncoding() {
            String enc = super.getCharacterEncoding();
            return (enc != null) ? enc : Util.ENCODING_DIRECT;
        }
    };
    // Parse the request
    List<?> /* FileItem */
    items = null;
    try {
        items = upload.parseRequest(rc);
    } catch (FileUploadException fue) {
        this.log.error("parseMultiPartPost: Error parsing request", fue);
    }
    if (items != null && items.size() > 0) {
        for (Iterator<?> ii = items.iterator(); ii.hasNext(); ) {
            FileItem fileItem = (FileItem) ii.next();
            RequestParameter pp = new MultipartRequestParameter(fileItem);
            parameters.addParameter(pp, false);
        }
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) RequestParameter(org.apache.sling.api.request.RequestParameter) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) RequestContext(org.apache.commons.fileupload.RequestContext) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

FileUploadException (org.apache.commons.fileupload.FileUploadException)27 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)20 FileItem (org.apache.commons.fileupload.FileItem)16 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)14 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)8 File (java.io.File)5 InputStream (java.io.InputStream)5 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)5 FileItemStream (org.apache.commons.fileupload.FileItemStream)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 List (java.util.List)4 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)4 ServletException (javax.servlet.ServletException)3 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)3 HttpRequestCallback (ghostdriver.server.HttpRequestCallback)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2