Search in sources :

Example 6 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testLFOnlyRequest.

@Test
public void testLFOnlyRequest() throws Exception {
    String str = "--AaB03x\n" + "content-disposition: form-data; name=\"field1\"\n" + "\n" + "Joe Blow\n" + "--AaB03x\n" + "content-disposition: form-data; name=\"field2\"\n" + "\n" + "Other\n" + "--AaB03x--\n";
    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(2));
    Part p1 = mpis.getPart("field1");
    assertThat(p1, notNullValue());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IO.copy(p1.getInputStream(), baos);
    assertThat(baos.toString("UTF-8"), is("Joe Blow"));
    Part p2 = mpis.getPart("field2");
    assertThat(p2, notNullValue());
    baos = new ByteArrayOutputStream();
    IO.copy(p2.getInputStream(), baos);
    assertThat(baos.toString("UTF-8"), is("Other"));
}
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 7 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testLeadingWhitespaceBodyWithoutCRLF.

@Test
public void testLeadingWhitespaceBodyWithoutCRLF() throws Exception {
    String body = "            " + "--AaB03x\r\n" + "content-disposition: form-data; name=\"field1\"\r\n" + "\r\n" + "Joe Blow\r\n" + "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"; filename=\"" + "foo.txt" + "\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "aaaa" + "bbbbb" + "\r\n" + "--AaB03x--\r\n";
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(body.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertThat(parts, notNullValue());
    assertThat(parts.size(), is(2));
    Part field1 = mpis.getPart("field1");
    assertThat(field1, notNullValue());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IO.copy(field1.getInputStream(), baos);
    assertThat(baos.toString("US-ASCII"), is("Joe Blow"));
    Part stuff = mpis.getPart("stuff");
    assertThat(stuff, notNullValue());
    baos = new ByteArrayOutputStream();
    IO.copy(stuff.getInputStream(), baos);
    assertTrue(baos.toString("US-ASCII").contains("bbbbb"));
}
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 8 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testMultiSameNames.

@Test
public void testMultiSameNames() throws Exception {
    String sameNames = "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"; filename=\"stuff1.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "00000\r\n" + "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"; filename=\"stuff2.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "110000000000000000000000000000000000000000000000000\r\n" + "--AaB03x--\r\n";
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(sameNames.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertEquals(2, parts.size());
    for (Part p : parts) assertEquals("stuff", p.getName());
    //if they all have the name name, then only retrieve the first one
    Part p = mpis.getPart("stuff");
    assertNotNull(p);
    assertEquals(5, p.getSize());
}
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 9 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testWhitespaceBody.

@Test
public void testWhitespaceBody() throws Exception {
    String whitespace = " ";
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(whitespace.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    try {
        mpis.getParts();
        fail("Multipart missing body");
    } catch (IOException e) {
        assertTrue(e.getMessage().startsWith("Missing initial"));
    }
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testMulti.

private void testMulti(String filename) throws IOException, ServletException, InterruptedException {
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(createMultipartRequestString(filename).getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertThat(parts.size(), is(2));
    //field 1 too small to go into tmp file, should be in internal buffer
    Part field1 = mpis.getPart("field1");
    assertThat(field1, notNullValue());
    assertThat(field1.getName(), is("field1"));
    InputStream is = field1.getInputStream();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    IO.copy(is, os);
    assertEquals("Joe Blow", new String(os.toByteArray()));
    assertEquals(8, field1.getSize());
    //in internal buffer
    assertNotNull(((MultiPartInputStreamParser.MultiPart) field1).getBytes());
    field1.write("field1.txt");
    //no longer in internal buffer
    assertNull(((MultiPartInputStreamParser.MultiPart) field1).getBytes());
    File f = new File(_dirname + File.separator + "field1.txt");
    assertTrue(f.exists());
    //write after having already written
    field1.write("another_field1.txt");
    File f2 = new File(_dirname + File.separator + "another_field1.txt");
    assertTrue(f2.exists());
    //should have been renamed
    assertFalse(f.exists());
    //file should be deleted
    field1.delete();
    //original file was renamed
    assertFalse(f.exists());
    //2nd written file was explicitly deleted
    assertFalse(f2.exists());
    MultiPart stuff = (MultiPart) mpis.getPart("stuff");
    assertThat(stuff.getSubmittedFileName(), is(filename));
    assertThat(stuff.getContentType(), is("text/plain"));
    assertThat(stuff.getHeader("Content-Type"), is("text/plain"));
    assertThat(stuff.getHeaders("content-type").size(), is(1));
    assertThat(stuff.getHeader("content-disposition"), is("form-data; name=\"stuff\"; filename=\"" + filename + "\""));
    assertThat(stuff.getHeaderNames().size(), is(2));
    assertThat(stuff.getSize(), is(51L));
    File tmpfile = ((MultiPartInputStreamParser.MultiPart) stuff).getFile();
    // longer than 100 bytes, should already be a tmp file
    assertThat(tmpfile, notNullValue());
    //not in an internal buffer
    assertThat(((MultiPartInputStreamParser.MultiPart) stuff).getBytes(), nullValue());
    assertThat(tmpfile.exists(), is(true));
    assertThat(tmpfile.getName(), is(not("stuff with space.txt")));
    stuff.write(filename);
    f = new File(_dirname + File.separator + filename);
    assertThat(f.exists(), is(true));
    assertThat(tmpfile.exists(), is(false));
    try {
        stuff.getInputStream();
    } catch (Exception e) {
        fail("Part.getInputStream() after file rename operation");
    }
    //clean up after test
    f.deleteOnExit();
}
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) ServletInputStream(javax.servlet.ServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

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