Search in sources :

Example 31 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement 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 32 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement 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 33 with MultipartConfigElement

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

the class MultiPartInputStreamTest method testNonMultiPartRequest.

@Test
public void testNonMultiPartRequest() throws Exception {
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 1024, 3072, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(_multi.getBytes()), "Content-type: text/plain", config, _tmpDir);
    mpis.setDeleteOnExit(true);
    assertTrue(mpis.getParts().isEmpty());
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 34 with MultipartConfigElement

use of javax.servlet.MultipartConfigElement 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 35 with MultipartConfigElement

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

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