Search in sources :

Example 51 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project undertow by undertow-io.

the class ManagedServlet method setupMultipart.

public void setupMultipart(ServletContextImpl servletContext) {
    FormEncodedDataDefinition formDataParser = new FormEncodedDataDefinition().setDefaultEncoding(servletContext.getDeployment().getDefaultCharset().name());
    MultipartConfigElement multipartConfig = servletInfo.getMultipartConfig();
    if (multipartConfig == null) {
        multipartConfig = servletContext.getDeployment().getDeploymentInfo().getDefaultMultipartConfig();
    }
    this.multipartConfig = multipartConfig;
    if (multipartConfig != null) {
        //todo: fileSizeThreshold
        MultipartConfigElement config = multipartConfig;
        if (config.getMaxRequestSize() != -1) {
            maxRequestSize = config.getMaxRequestSize();
        } else {
            maxRequestSize = -1;
        }
        final Path tempDir;
        if (config.getLocation() == null || config.getLocation().isEmpty()) {
            tempDir = servletContext.getDeployment().getDeploymentInfo().getTempPath();
        } else {
            String location = config.getLocation();
            Path locFile = Paths.get(location);
            if (locFile.isAbsolute()) {
                tempDir = locFile;
            } else {
                tempDir = servletContext.getDeployment().getDeploymentInfo().getTempPath().resolve(location);
            }
        }
        MultiPartParserDefinition multiPartParserDefinition = new MultiPartParserDefinition(tempDir);
        if (config.getMaxFileSize() > 0) {
            multiPartParserDefinition.setMaxIndividualFileSize(config.getMaxFileSize());
        }
        multiPartParserDefinition.setDefaultEncoding(servletContext.getDeployment().getDefaultCharset().name());
        formParserFactory = FormParserFactory.builder(false).addParser(formDataParser).addParser(multiPartParserDefinition).build();
    } else {
        //no multipart config we don't allow multipart requests
        formParserFactory = FormParserFactory.builder(false).addParser(formDataParser).build();
        maxRequestSize = -1;
    }
}
Also used : Path(java.nio.file.Path) MultipartConfigElement(javax.servlet.MultipartConfigElement) FormEncodedDataDefinition(io.undertow.server.handlers.form.FormEncodedDataDefinition) MultiPartParserDefinition(io.undertow.server.handlers.form.MultiPartParserDefinition)

Aggregations

MultipartConfigElement (javax.servlet.MultipartConfigElement)51 Test (org.junit.Test)35 ByteArrayInputStream (java.io.ByteArrayInputStream)30 Part (javax.servlet.http.Part)27 MultiPart (org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 IOException (java.io.IOException)8 File (java.io.File)7 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)5 ServletException (javax.servlet.ServletException)4 ServletInputStream (javax.servlet.ServletInputStream)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Servlet (javax.servlet.Servlet)2 ServletRegistration (javax.servlet.ServletRegistration)2 MultipartConfig (javax.servlet.annotation.MultipartConfig)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MultiPartInputStreamParser (org.eclipse.jetty.util.MultiPartInputStreamParser)2 DbxException (com.dropbox.core.DbxException)1