Search in sources :

Example 51 with Part

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

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

the class MultiPartContentProviderTest method testFieldDeferred.

@Test
public void testFieldDeferred() throws Exception {
    String name = "field";
    byte[] data = "Hello, World".getBytes(StandardCharsets.US_ASCII);
    start(new AbstractMultiPartHandler() {

        @Override
        protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Collection<Part> parts = request.getParts();
            Assert.assertEquals(1, parts.size());
            Part part = parts.iterator().next();
            Assert.assertEquals(name, part.getName());
            Assert.assertEquals("text/plain", part.getContentType());
            Assert.assertArrayEquals(data, IO.readBytes(part.getInputStream()));
        }
    });
    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    DeferredContentProvider content = new DeferredContentProvider();
    multiPart.addFieldPart(name, content, null);
    multiPart.close();
    CountDownLatch responseLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send(result -> {
        Assert.assertTrue(String.valueOf(result.getFailure()), result.isSucceeded());
        Assert.assertEquals(200, result.getResponse().getStatus());
        responseLatch.countDown();
    });
    // Wait until the request has been sent.
    Thread.sleep(1000);
    // Provide the content.
    content.offer(ByteBuffer.wrap(data));
    content.close();
    Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Part(javax.servlet.http.Part) Collection(java.util.Collection) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 53 with Part

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

the class MultiPartContentProviderTest method testFileFromInputStream.

@Test
public void testFileFromInputStream() throws Exception {
    String name = "file";
    String fileName = "upload.png";
    String contentType = "image/png";
    byte[] data = new byte[512];
    new Random().nextBytes(data);
    start(new AbstractMultiPartHandler() {

        @Override
        protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Collection<Part> parts = request.getParts();
            Assert.assertEquals(1, parts.size());
            Part part = parts.iterator().next();
            Assert.assertEquals(name, part.getName());
            Assert.assertEquals(contentType, part.getContentType());
            Assert.assertEquals(fileName, part.getSubmittedFileName());
            Assert.assertEquals(data.length, part.getSize());
            Assert.assertArrayEquals(data, IO.readBytes(part.getInputStream()));
        }
    });
    CountDownLatch closeLatch = new CountDownLatch(1);
    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    InputStreamContentProvider content = new InputStreamContentProvider(new ByteArrayInputStream(data) {

        @Override
        public void close() throws IOException {
            super.close();
            closeLatch.countDown();
        }
    });
    HttpFields fields = new HttpFields();
    fields.put(HttpHeader.CONTENT_TYPE, contentType);
    multiPart.addFilePart(name, fileName, content, fields);
    multiPart.close();
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
    Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) Part(javax.servlet.http.Part) HttpFields(org.eclipse.jetty.http.HttpFields) Collection(java.util.Collection) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 54 with Part

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

the class MultiPartContentProviderTest method testFieldDeferredAndFileDeferred.

@Test
public void testFieldDeferredAndFileDeferred() throws Exception {
    String value = "text";
    Charset encoding = StandardCharsets.US_ASCII;
    byte[] fileData = new byte[1024];
    new Random().nextBytes(fileData);
    start(new AbstractMultiPartHandler() {

        @Override
        protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            List<Part> parts = new ArrayList<>(request.getParts());
            Assert.assertEquals(2, parts.size());
            Part fieldPart = parts.get(0);
            Part filePart = parts.get(1);
            if (!"field".equals(fieldPart.getName())) {
                Part swap = filePart;
                filePart = fieldPart;
                fieldPart = swap;
            }
            Assert.assertEquals(value, IO.toString(fieldPart.getInputStream(), encoding));
            Assert.assertEquals("file", filePart.getName());
            Assert.assertEquals("application/octet-stream", filePart.getContentType());
            Assert.assertEquals("fileName", filePart.getSubmittedFileName());
            Assert.assertArrayEquals(fileData, IO.readBytes(filePart.getInputStream()));
        }
    });
    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    DeferredContentProvider fieldContent = new DeferredContentProvider();
    multiPart.addFieldPart("field", fieldContent, null);
    DeferredContentProvider fileContent = new DeferredContentProvider();
    multiPart.addFilePart("file", "fileName", fileContent, null);
    CountDownLatch responseLatch = new CountDownLatch(1);
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send(result -> {
        Assert.assertTrue(String.valueOf(result.getFailure()), result.isSucceeded());
        Assert.assertEquals(200, result.getResponse().getStatus());
        responseLatch.countDown();
    });
    // Wait until the request has been sent.
    Thread.sleep(1000);
    // Provide the content, in reversed part order.
    fileContent.offer(ByteBuffer.wrap(fileData));
    fileContent.close();
    Thread.sleep(1000);
    fieldContent.offer(encoding.encode(value));
    fieldContent.close();
    multiPart.close();
    Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
}
Also used : Charset(java.nio.charset.Charset) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) Part(javax.servlet.http.Part) ArrayList(java.util.ArrayList) List(java.util.List) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) Test(org.junit.Test)

Example 55 with Part

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

the class MultiPartContentProviderTest method testSimpleField.

@Test
public void testSimpleField() throws Exception {
    String name = "field";
    String value = "value";
    start(new AbstractMultiPartHandler() {

        @Override
        protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Collection<Part> parts = request.getParts();
            Assert.assertEquals(1, parts.size());
            Part part = parts.iterator().next();
            Assert.assertEquals(name, part.getName());
            Assert.assertEquals(value, IO.toString(part.getInputStream()));
        }
    });
    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    multiPart.addFieldPart(name, new StringContentProvider(value), null);
    multiPart.close();
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Part(javax.servlet.http.Part) HttpServletResponse(javax.servlet.http.HttpServletResponse) Collection(java.util.Collection) IOException(java.io.IOException) AbstractHttpClientServerTest(org.eclipse.jetty.client.AbstractHttpClientServerTest) 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