Search in sources :

Example 1 with ApplicationPart

use of org.apache.catalina.core.ApplicationPart in project jodd by oblac.

the class EchoServlet method copyFileName.

protected Map<String, String> copyFileName(HttpServletRequest req) {
    Map<String, String> parts = new HashMap<>();
    if (req.getContentType() != null && !req.getContentType().toLowerCase().contains("multipart/form-data")) {
        return parts;
    }
    try {
        Collection<Part> prs = req.getParts();
        for (Part p : prs) {
            if (p instanceof ApplicationPart) {
                ApplicationPart ap = (ApplicationPart) p;
                parts.put(p.getName(), ap.getSubmittedFileName());
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ServletException e) {
        e.printStackTrace();
    }
    return parts;
}
Also used : ServletException(javax.servlet.ServletException) HashMap(java.util.HashMap) ApplicationPart(org.apache.catalina.core.ApplicationPart) ApplicationPart(org.apache.catalina.core.ApplicationPart) IOException(java.io.IOException)

Example 2 with ApplicationPart

use of org.apache.catalina.core.ApplicationPart in project tomcat by apache.

the class Request method parseParts.

private void parseParts(boolean explicit) {
    // Return immediately if the parts have already been parsed
    if (parts != null || partsParseException != null) {
        return;
    }
    Context context = getContext();
    MultipartConfigElement mce = getWrapper().getMultipartConfigElement();
    if (mce == null) {
        if (context.getAllowCasualMultipartParsing()) {
            mce = new MultipartConfigElement(null, connector.getMaxPostSize(), connector.getMaxPostSize(), connector.getMaxPostSize());
        } else {
            if (explicit) {
                partsParseException = new IllegalStateException(sm.getString("coyoteRequest.noMultipartConfig"));
                return;
            } else {
                parts = Collections.emptyList();
                return;
            }
        }
    }
    Parameters parameters = coyoteRequest.getParameters();
    parameters.setLimit(getConnector().getMaxParameterCount());
    boolean success = false;
    try {
        File location;
        String locationStr = mce.getLocation();
        if (locationStr == null || locationStr.length() == 0) {
            location = ((File) context.getServletContext().getAttribute(ServletContext.TEMPDIR));
        } else {
            // If relative, it is relative to TEMPDIR
            location = new File(locationStr);
            if (!location.isAbsolute()) {
                location = new File((File) context.getServletContext().getAttribute(ServletContext.TEMPDIR), locationStr).getAbsoluteFile();
            }
        }
        if (!location.isDirectory()) {
            parameters.setParseFailedReason(FailReason.MULTIPART_CONFIG_INVALID);
            partsParseException = new IOException(sm.getString("coyoteRequest.uploadLocationInvalid", location));
            return;
        }
        // Create a new file upload handler
        DiskFileItemFactory factory = new DiskFileItemFactory();
        try {
            factory.setRepository(location.getCanonicalFile());
        } catch (IOException ioe) {
            parameters.setParseFailedReason(FailReason.IO_ERROR);
            partsParseException = ioe;
            return;
        }
        factory.setSizeThreshold(mce.getFileSizeThreshold());
        ServletFileUpload upload = new ServletFileUpload();
        upload.setFileItemFactory(factory);
        upload.setFileSizeMax(mce.getMaxFileSize());
        upload.setSizeMax(mce.getMaxRequestSize());
        parts = new ArrayList<>();
        try {
            List<FileItem> items = upload.parseRequest(new ServletRequestContext(this));
            int maxPostSize = getConnector().getMaxPostSize();
            int postSize = 0;
            String enc = getCharacterEncoding();
            Charset charset = null;
            if (enc != null) {
                try {
                    charset = B2CConverter.getCharset(enc);
                } catch (UnsupportedEncodingException e) {
                // Ignore
                }
            }
            for (FileItem item : items) {
                ApplicationPart part = new ApplicationPart(item, location);
                parts.add(part);
                if (part.getSubmittedFileName() == null) {
                    String name = part.getName();
                    String value = null;
                    try {
                        String encoding = parameters.getEncoding();
                        if (encoding == null) {
                            if (enc == null) {
                                encoding = Parameters.DEFAULT_ENCODING;
                            } else {
                                encoding = enc;
                            }
                        }
                        value = part.getString(encoding);
                    } catch (UnsupportedEncodingException uee) {
                        try {
                            value = part.getString(Parameters.DEFAULT_ENCODING);
                        } catch (UnsupportedEncodingException e) {
                        // Should not be possible
                        }
                    }
                    if (maxPostSize >= 0) {
                        // accurate but close enough.
                        if (charset == null) {
                            // Name length
                            postSize += name.getBytes().length;
                        } else {
                            postSize += name.getBytes(charset).length;
                        }
                        if (value != null) {
                            // Equals sign
                            postSize++;
                            // Value length
                            postSize += part.getSize();
                        }
                        // Value separator
                        postSize++;
                        if (postSize > maxPostSize) {
                            parameters.setParseFailedReason(FailReason.POST_TOO_LARGE);
                            throw new IllegalStateException(sm.getString("coyoteRequest.maxPostSizeExceeded"));
                        }
                    }
                    parameters.addParameter(name, value);
                }
            }
            success = true;
        } catch (InvalidContentTypeException e) {
            parameters.setParseFailedReason(FailReason.INVALID_CONTENT_TYPE);
            partsParseException = new ServletException(e);
        } catch (FileUploadBase.SizeException e) {
            parameters.setParseFailedReason(FailReason.POST_TOO_LARGE);
            checkSwallowInput();
            partsParseException = new IllegalStateException(e);
        } catch (FileUploadException e) {
            parameters.setParseFailedReason(FailReason.IO_ERROR);
            partsParseException = new IOException(e);
        } catch (IllegalStateException e) {
            // addParameters() will set parseFailedReason
            checkSwallowInput();
            partsParseException = e;
        }
    } finally {
        if (partsParseException != null || !success) {
            parameters.setParseFailedReason(FailReason.UNKNOWN);
        }
    }
}
Also used : ServletRequestContext(org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext) AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Parameters(org.apache.tomcat.util.http.Parameters) InvalidContentTypeException(org.apache.tomcat.util.http.fileupload.FileUploadBase.InvalidContentTypeException) ServletRequestContext(org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) DiskFileItemFactory(org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory) ServletException(javax.servlet.ServletException) FileItem(org.apache.tomcat.util.http.fileupload.FileItem) MultipartConfigElement(javax.servlet.MultipartConfigElement) ServletFileUpload(org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload) FileUploadBase(org.apache.tomcat.util.http.fileupload.FileUploadBase) ApplicationPart(org.apache.catalina.core.ApplicationPart) File(java.io.File) FileUploadException(org.apache.tomcat.util.http.fileupload.FileUploadException)

Aggregations

IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 ApplicationPart (org.apache.catalina.core.ApplicationPart)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 AsyncContext (javax.servlet.AsyncContext)1 MultipartConfigElement (javax.servlet.MultipartConfigElement)1 ServletContext (javax.servlet.ServletContext)1 Context (org.apache.catalina.Context)1 Parameters (org.apache.tomcat.util.http.Parameters)1 FileItem (org.apache.tomcat.util.http.fileupload.FileItem)1 FileUploadBase (org.apache.tomcat.util.http.fileupload.FileUploadBase)1 InvalidContentTypeException (org.apache.tomcat.util.http.fileupload.FileUploadBase.InvalidContentTypeException)1 FileUploadException (org.apache.tomcat.util.http.fileupload.FileUploadException)1 DiskFileItemFactory (org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload)1 ServletRequestContext (org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext)1