Search in sources :

Example 46 with Part

use of javax.servlet.http.Part in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testNoBoundaryRequest.

@Test
public void testNoBoundaryRequest() throws Exception {
    String str = "--\r\n" + "Content-Disposition: form-data; name=\"fileName\"\r\n" + "Content-Type: text/plain; charset=US-ASCII\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "abc\r\n" + "--\r\n" + "Content-Disposition: form-data; name=\"desc\"\r\n" + "Content-Type: text/plain; charset=US-ASCII\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "123\r\n" + "--\r\n" + "Content-Disposition: form-data; name=\"title\"\r\n" + "Content-Type: text/plain; charset=US-ASCII\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "\r\n" + "ttt\r\n" + "--\r\n" + "Content-Disposition: form-data; name=\"datafile5239138112980980385.txt\"; filename=\"datafile5239138112980980385.txt\"\r\n" + "Content-Type: application/octet-stream; charset=ISO-8859-1\r\n" + "Content-Transfer-Encoding: binary\r\n" + "\r\n" + "000\r\n" + "----\r\n";
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(str.getBytes()), "multipart/form-data", config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertThat(parts.size(), is(4));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Part fileName = mpis.getPart("fileName");
    assertThat(fileName, notNullValue());
    assertThat(fileName.getSize(), is(3L));
    IO.copy(fileName.getInputStream(), baos);
    assertThat(baos.toString("US-ASCII"), is("abc"));
    baos = new ByteArrayOutputStream();
    Part desc = mpis.getPart("desc");
    assertThat(desc, notNullValue());
    assertThat(desc.getSize(), is(3L));
    IO.copy(desc.getInputStream(), baos);
    assertThat(baos.toString("US-ASCII"), is("123"));
    baos = new ByteArrayOutputStream();
    Part title = mpis.getPart("title");
    assertThat(title, notNullValue());
    assertThat(title.getSize(), is(3L));
    IO.copy(title.getInputStream(), baos);
    assertThat(baos.toString("US-ASCII"), is("ttt"));
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) Part(javax.servlet.http.Part) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 47 with Part

use of javax.servlet.http.Part in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testBadlyEncodedFilename.

@Test
public void testBadlyEncodedFilename() throws Exception {
    String contents = "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"; filename=\"" + "Taken on Aug 22 \\ 2012.jpg" + "\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "stuff" + "aaa" + "\r\n" + "--AaB03x--\r\n";
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(contents.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertThat(parts.size(), is(1));
    assertThat(((MultiPartInputStreamParser.MultiPart) parts.iterator().next()).getSubmittedFileName(), is("Taken on Aug 22 \\ 2012.jpg"));
}
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 48 with Part

use of javax.servlet.http.Part in project jetty.project by eclipse.

the class MultiPartInputStreamTest method testWriteFilesIfContentDispositionFilename.

@Test
public void testWriteFilesIfContentDispositionFilename() throws Exception {
    String s = "--AaB03x\r\n" + "content-disposition: form-data; name=\"field1\"; filename=\"frooble.txt\"\r\n" + "\r\n" + "Joe Blow\r\n" + "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "sss" + "aaa" + "\r\n" + "--AaB03x--\r\n";
    //all default values for multipartconfig, ie file size threshold 0
    MultipartConfigElement config = new MultipartConfigElement(_dirname);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(s.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    mpis.setWriteFilesWithFilenames(true);
    Collection<Part> parts = mpis.getParts();
    assertThat(parts.size(), is(2));
    //has a filename, should be written to a file
    Part field1 = mpis.getPart("field1");
    File f = ((MultiPartInputStreamParser.MultiPart) field1).getFile();
    // longer than 100 bytes, should already be a tmp file
    assertThat(f, notNullValue());
    Part stuff = mpis.getPart("stuff");
    //should only be in memory, no filename
    f = ((MultiPartInputStreamParser.MultiPart) stuff).getFile();
    assertThat(f, nullValue());
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiPart(org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart) Part(javax.servlet.http.Part) File(java.io.File) Test(org.junit.Test)

Example 49 with Part

use of javax.servlet.http.Part 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 50 with Part

use of javax.servlet.http.Part 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)

Aggregations

Part (javax.servlet.http.Part)68 Test (org.junit.Test)42 ByteArrayInputStream (java.io.ByteArrayInputStream)27 MultipartConfigElement (javax.servlet.MultipartConfigElement)27 MultiPart (org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart)24 IOException (java.io.IOException)19 InputStream (java.io.InputStream)12 ArrayList (java.util.ArrayList)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 ServletException (javax.servlet.ServletException)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 AbstractHttpClientServerTest (org.eclipse.jetty.client.AbstractHttpClientServerTest)7 File (java.io.File)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 MockPart (org.springframework.mock.web.test.MockPart)5 RequestPart (org.springframework.web.bind.annotation.RequestPart)5 PrintWriter (java.io.PrintWriter)4