Search in sources :

Example 41 with Part

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

the class MultiPartInputStreamTest method testCorrectlyEncodedMSFilename.

@Test
public void testCorrectlyEncodedMSFilename() throws Exception {
    String contents = "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"; filename=\"" + "c:\\\\this\\\\really\\\\is\\\\some\\\\path\\\\to\\\\a\\\\file.txt" + "\"\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("c:\\this\\really\\is\\some\\path\\to\\a\\file.txt"));
}
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 Part

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

the class MultiPartInputStreamTest method testBase64EncodedContent.

@Test
public void testBase64EncodedContent() throws Exception {
    String contentWithEncodedPart = "--AaB03x\r\n" + "Content-disposition: form-data; name=\"other\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "other" + "\r\n" + "--AaB03x\r\n" + "Content-disposition: form-data; name=\"stuff\"; filename=\"stuff.txt\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-Type: application/octet-stream\r\n" + "\r\n" + B64Code.encode("hello jetty") + "\r\n" + "--AaB03x\r\n" + "Content-disposition: form-data; name=\"final\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "the end" + "\r\n" + "--AaB03x--\r\n";
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(contentWithEncodedPart.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertEquals(3, parts.size());
    Part p1 = mpis.getPart("other");
    assertNotNull(p1);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IO.copy(p1.getInputStream(), baos);
    assertEquals("other", baos.toString("US-ASCII"));
    Part p2 = mpis.getPart("stuff");
    assertNotNull(p2);
    baos = new ByteArrayOutputStream();
    IO.copy(p2.getInputStream(), baos);
    assertEquals("hello jetty", baos.toString("US-ASCII"));
    Part p3 = mpis.getPart("final");
    assertNotNull(p3);
    baos = new ByteArrayOutputStream();
    IO.copy(p3.getInputStream(), baos);
    assertEquals("the end", baos.toString("US-ASCII"));
}
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 43 with Part

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

the class MultiPartInputStreamTest method testBadlyEncodedMSFilename.

@Test
public void testBadlyEncodedMSFilename() throws Exception {
    String contents = "--AaB03x\r\n" + "content-disposition: form-data; name=\"stuff\"; filename=\"" + "c:\\this\\really\\is\\some\\path\\to\\a\\file.txt" + "\"\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("c:\\this\\really\\is\\some\\path\\to\\a\\file.txt"));
}
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 44 with Part

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

the class MultiPartInputStreamTest method testQuotedPrintableEncoding.

@Test
public void testQuotedPrintableEncoding() throws Exception {
    String contentWithEncodedPart = "--AaB03x\r\n" + "Content-disposition: form-data; name=\"other\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "other" + "\r\n" + "--AaB03x\r\n" + "Content-disposition: form-data; name=\"stuff\"; filename=\"stuff.txt\"\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "truth=3Dbeauty" + "\r\n" + "--AaB03x--\r\n";
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(contentWithEncodedPart.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = mpis.getParts();
    assertEquals(2, parts.size());
    Part p1 = mpis.getPart("other");
    assertNotNull(p1);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IO.copy(p1.getInputStream(), baos);
    assertEquals("other", baos.toString("US-ASCII"));
    Part p2 = mpis.getPart("stuff");
    assertNotNull(p2);
    baos = new ByteArrayOutputStream();
    IO.copy(p2.getInputStream(), baos);
    assertEquals("truth=beauty", baos.toString("US-ASCII"));
}
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 45 with Part

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

the class MultiPartInputStreamTest method testFileTooBig.

@Test
public void testFileTooBig() throws Exception {
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 40, 1024, 30);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(_multi.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = null;
    try {
        parts = mpis.getParts();
        fail("stuff.txt should have been larger than maxFileSize");
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().startsWith("Multipart Mime part"));
    }
}
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)

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