Search in sources :

Example 56 with Part

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

the class MultiPartContentProviderTest method testFileFromPath.

@Test
public void testFileFromPath() throws Exception {
    // Prepare a file to upload.
    String data = "multipart_test_€";
    Path tmpDir = MavenTestingUtils.getTargetTestingPath();
    Path tmpPath = Files.createTempFile(tmpDir, "multipart_", ".txt");
    Charset encoding = StandardCharsets.UTF_8;
    try (BufferedWriter writer = Files.newBufferedWriter(tmpPath, encoding, StandardOpenOption.CREATE)) {
        writer.write(data);
    }
    String name = "file";
    String contentType = "text/plain; charset=" + encoding.name();
    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(tmpPath.getFileName().toString(), part.getSubmittedFileName());
            Assert.assertEquals(Files.size(tmpPath), part.getSize());
            Assert.assertEquals(data, IO.toString(part.getInputStream(), encoding));
        }
    });
    MultiPartContentProvider multiPart = new MultiPartContentProvider();
    PathContentProvider content = new PathContentProvider(contentType, tmpPath);
    content.setByteBufferPool(client.getByteBufferPool());
    multiPart.addFilePart(name, tmpPath.getFileName().toString(), content, null);
    multiPart.close();
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
    Assert.assertEquals(200, response.getStatus());
    Files.delete(tmpPath);
}
Also used : Path(java.nio.file.Path) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Charset(java.nio.charset.Charset) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) 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 57 with Part

use of javax.servlet.http.Part in project camel by apache.

the class AttachmentHttpBinding method populateAttachments.

@Override
protected void populateAttachments(HttpServletRequest request, HttpMessage message) {
    Object object = request.getAttribute("org.eclipse.jetty.servlet.MultiPartFile.multiPartInputStream");
    if (object instanceof MultiPartInputStreamParser) {
        MultiPartInputStreamParser parser = (MultiPartInputStreamParser) object;
        Collection<Part> parts;
        try {
            parts = parser.getParts();
            for (Part part : parts) {
                DataSource ds = new PartDataSource(part);
                Attachment attachment = new DefaultAttachment(ds);
                for (String headerName : part.getHeaderNames()) {
                    for (String headerValue : part.getHeaders(headerName)) {
                        attachment.addHeader(headerName, headerValue);
                    }
                }
                message.addAttachmentObject(part.getName(), attachment);
            }
        } catch (Exception e) {
            throw new RuntimeCamelException("Cannot populate attachments", e);
        }
    }
}
Also used : Part(javax.servlet.http.Part) MultiPartInputStreamParser(org.eclipse.jetty.util.MultiPartInputStreamParser) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.camel.Attachment) RuntimeCamelException(org.apache.camel.RuntimeCamelException) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) DataSource(javax.activation.DataSource)

Example 58 with Part

use of javax.servlet.http.Part in project sonarqube by SonarSource.

the class ServletRequestTest method read_input_stream.

@Test
public void read_input_stream() throws Exception {
    when(source.getContentType()).thenReturn("multipart/form-data");
    InputStream file = mock(InputStream.class);
    Part part = mock(Part.class);
    when(part.getInputStream()).thenReturn(file);
    when(part.getSize()).thenReturn(10L);
    when(source.getPart("param1")).thenReturn(part);
    assertThat(underTest.readInputStreamParam("param1")).isEqualTo(file);
    assertThat(underTest.readInputStreamParam("param2")).isNull();
}
Also used : InputStream(java.io.InputStream) Part(javax.servlet.http.Part) Test(org.junit.Test)

Example 59 with Part

use of javax.servlet.http.Part in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolvePartArray.

@Test
public void resolvePartArray() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockPart expected1 = new MockPart("pfilearray", "Hello World 1".getBytes());
    MockPart expected2 = new MockPart("pfilearray", "Hello World 2".getBytes());
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(expected1);
    request.addPart(expected2);
    request.addPart(new MockPart("other", "Hello World 3".getBytes()));
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof Part[]);
    Part[] parts = (Part[]) result;
    assertEquals(2, parts.length);
    assertEquals(parts[0], expected1);
    assertEquals(parts[1], expected2);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 60 with Part

use of javax.servlet.http.Part in project spring-framework by spring-projects.

the class MultipartResolutionDelegate method resolvePartArray.

private static Part[] resolvePartArray(HttpServletRequest servletRequest, String name) throws Exception {
    Collection<Part> parts = servletRequest.getParts();
    List<Part> result = new ArrayList<>(parts.size());
    for (Part part : parts) {
        if (part.getName().equals(name)) {
            result.add(part);
        }
    }
    return result.toArray(new Part[result.size()]);
}
Also used : Part(javax.servlet.http.Part) ArrayList(java.util.ArrayList)

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