Search in sources :

Example 41 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testCharsetEncoding.

@Test
public void testCharsetEncoding() throws Exception {
    String contentType = "multipart/form-data; boundary=TheBoundary; charset=ISO-8859-1";
    String str = "--TheBoundary\r" + "content-disposition: form-data; name=\"field1\"\r" + "\r" + "\nJoe Blow\n" + "\r" + "--TheBoundary--\r";
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertThat(parts.size(), is(1));
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) Part(javax.servlet.http.Part) Test(org.junit.Test)

Example 42 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.

the class MultiPartConfigAnnotationHandler method doHandle.

/** 
     * @see org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler#doHandle(java.lang.Class)
     */
public void doHandle(Class clazz) {
    if (!Servlet.class.isAssignableFrom(clazz))
        return;
    MultipartConfig multi = (MultipartConfig) clazz.getAnnotation(MultipartConfig.class);
    if (multi == null)
        return;
    MetaData metaData = _context.getMetaData();
    //TODO: The MultipartConfigElement needs to be set on the ServletHolder's Registration.
    //How to identify the correct Servlet?  If the Servlet has no WebServlet annotation on it, does it mean that this MultipartConfig
    //annotation applies to all declared instances in web.xml/programmatically?
    //Assuming TRUE for now.
    ServletHolder holder = getServletHolderForClass(clazz);
    if (holder != null) {
        Descriptor d = metaData.getOriginDescriptor(holder.getName() + ".servlet.multipart-config");
        //let the annotation override it
        if (d == null) {
            metaData.setOrigin(holder.getName() + ".servlet.multipart-config", multi, clazz);
            holder.getRegistration().setMultipartConfig(new MultipartConfigElement(multi));
        }
    }
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) MultipartConfig(javax.servlet.annotation.MultipartConfig) MetaData(org.eclipse.jetty.webapp.MetaData) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Servlet(javax.servlet.Servlet) Descriptor(org.eclipse.jetty.webapp.Descriptor)

Example 43 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.

the class MultiPartFilter method doFilter.

/* ------------------------------------------------------------------------------- */
/**
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest srequest = (HttpServletRequest) request;
    if (srequest.getContentType() == null || !srequest.getContentType().startsWith("multipart/form-data")) {
        chain.doFilter(request, response);
        return;
    }
    String content_type = srequest.getContentType();
    //Get current parameters so we can merge into them
    MultiMap params = new MultiMap();
    for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
        Object value = entry.getValue();
        if (value instanceof String[])
            params.addValues(entry.getKey(), (String[]) value);
        else
            params.add(entry.getKey(), value);
    }
    MultipartConfigElement config = new MultipartConfigElement(tempdir.getCanonicalPath(), _maxFileSize, _maxRequestSize, _fileOutputBuffer);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(request.getInputStream(), content_type, config, tempdir);
    mpis.setDeleteOnExit(_deleteFiles);
    mpis.setWriteFilesWithFilenames(_writeFilesWithFilenames);
    request.setAttribute(MULTIPART, mpis);
    try {
        Collection<Part> parts = mpis.getParts();
        if (parts != null) {
            Iterator<Part> itor = parts.iterator();
            while (itor.hasNext() && params.size() < _maxFormKeys) {
                Part p = itor.next();
                if (LOG.isDebugEnabled())
                    LOG.debug("{}", p);
                MultiPartInputStreamParser.MultiPart mp = (MultiPartInputStreamParser.MultiPart) p;
                if (mp.getFile() != null) {
                    request.setAttribute(mp.getName(), mp.getFile());
                    if (mp.getContentDispositionFilename() != null) {
                        params.add(mp.getName(), mp.getContentDispositionFilename());
                        if (mp.getContentType() != null)
                            params.add(mp.getName() + CONTENT_TYPE_SUFFIX, mp.getContentType());
                    }
                } else {
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    IO.copy(p.getInputStream(), bytes);
                    params.add(p.getName(), bytes.toByteArray());
                    if (p.getContentType() != null)
                        params.add(p.getName() + CONTENT_TYPE_SUFFIX, p.getContentType());
                }
            }
        }
        // handle request
        chain.doFilter(new Wrapper(srequest, params), response);
    } finally {
        deleteFiles(request);
    }
}
Also used : HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) MultiPartInputStreamParser(org.eclipse.jetty.util.MultiPartInputStreamParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) MultiMap(org.eclipse.jetty.util.MultiMap) MultipartConfigElement(javax.servlet.MultipartConfigElement) Part(javax.servlet.http.Part) HashMap(java.util.HashMap) Map(java.util.Map) MultiMap(org.eclipse.jetty.util.MultiMap)

Example 44 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.

the class Request method getParts.

private Collection<Part> getParts(MultiMap<String> params) throws IOException, ServletException {
    if (_multiPartInputStream == null)
        _multiPartInputStream = (MultiPartInputStreamParser) getAttribute(__MULTIPART_INPUT_STREAM);
    if (_multiPartInputStream == null) {
        MultipartConfigElement config = (MultipartConfigElement) getAttribute(__MULTIPART_CONFIG_ELEMENT);
        if (config == null)
            throw new IllegalStateException("No multipart config for servlet");
        _multiPartInputStream = new MultiPartInputStreamParser(getInputStream(), getContentType(), config, (_context != null ? (File) _context.getAttribute("javax.servlet.context.tempdir") : null));
        setAttribute(__MULTIPART_INPUT_STREAM, _multiPartInputStream);
        setAttribute(__MULTIPART_CONTEXT, _context);
        //causes parsing
        Collection<Part> parts = _multiPartInputStream.getParts();
        ByteArrayOutputStream os = null;
        for (Part p : parts) {
            MultiPartInputStreamParser.MultiPart mp = (MultiPartInputStreamParser.MultiPart) p;
            if (mp.getContentDispositionFilename() == null) {
                // Servlet Spec 3.0 pg 23, parts without filename must be put into params.
                String charset = null;
                if (mp.getContentType() != null)
                    charset = MimeTypes.getCharsetFromContentType(mp.getContentType());
                try (InputStream is = mp.getInputStream()) {
                    if (os == null)
                        os = new ByteArrayOutputStream();
                    IO.copy(is, os);
                    String content = new String(os.toByteArray(), charset == null ? StandardCharsets.UTF_8 : Charset.forName(charset));
                    if (_contentParameters == null)
                        _contentParameters = params == null ? new MultiMap<>() : params;
                    _contentParameters.add(mp.getName(), content);
                }
                os.reset();
            }
        }
    }
    return _multiPartInputStream.getParts();
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) Part(javax.servlet.http.Part) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) MultiPartInputStreamParser(org.eclipse.jetty.util.MultiPartInputStreamParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File)

Example 45 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement in project jetty.project by eclipse.

the class ServletHolder method prepare.

/* ------------------------------------------------------------ */
/**
     * Prepare to service a request.
     *
     * @param baseRequest the base request
     * @param request the request
     * @param response the response
     * @throws ServletException if unable to prepare the servlet
     * @throws UnavailableException if not available
     */
protected void prepare(Request baseRequest, ServletRequest request, ServletResponse response) throws ServletException, UnavailableException {
    ensureInstance();
    MultipartConfigElement mpce = ((Registration) getRegistration()).getMultipartConfig();
    if (mpce != null)
        baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, mpce);
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ServletRegistration(javax.servlet.ServletRegistration)

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