Search in sources :

Example 26 with Part

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

the class RequestPartMethodArgumentResolverTests method resolvePartArrayArgument.

@Test
public void resolvePartArrayArgument() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MockPart part1 = new MockPart("requestPart", "Hello World 1".getBytes());
    MockPart part2 = new MockPart("requestPart", "Hello World 2".getBytes());
    request.addPart(part1);
    request.addPart(part2);
    request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object result = resolver.resolveArgument(paramPartArray, null, webRequest, null);
    assertTrue(result instanceof Part[]);
    Part[] parts = (Part[]) result;
    assertEquals(2, parts.length);
    assertEquals(parts[0], part1);
    assertEquals(parts[1], part2);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 27 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) {
    try {
        Collection<Part> parts = request.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) 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 28 with Part

use of javax.servlet.http.Part in project zxing by zxing.

the class DecodeServlet method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Collection<Part> parts;
    try {
        parts = request.getParts();
    } catch (IllegalStateException ise) {
        log.info("File upload was too large or invalid");
        errorResponse(request, response, "badimage");
        return;
    } catch (IOException ioe) {
        log.info(ioe.toString());
        errorResponse(request, response, "badurl");
        return;
    }
    Part fileUploadPart = null;
    for (Part part : parts) {
        if (part.getHeader(HttpHeaders.CONTENT_DISPOSITION) != null) {
            fileUploadPart = part;
            break;
        }
    }
    if (fileUploadPart == null) {
        log.info("File upload was not multipart");
        errorResponse(request, response, "badimage");
    } else {
        log.info("Decoding uploaded file " + fileUploadPart.getSubmittedFileName());
        try (InputStream is = fileUploadPart.getInputStream()) {
            processStream(is, request, response);
        }
    }
}
Also used : Part(javax.servlet.http.Part) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 29 with Part

use of javax.servlet.http.Part in project wildfly by wildfly.

the class MultipartFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    Part part = req.getPart("file");
    byte[] data = new byte[100];
    int c;
    InputStream inputStream = part.getInputStream();
    while ((c = inputStream.read(data)) > 0) {
        resp.getOutputStream().write(data, 0, c);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Part(javax.servlet.http.Part) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 30 with Part

use of javax.servlet.http.Part in project undertow by undertow-io.

the class HttpServletRequestImpl method loadParts.

private void loadParts() throws IOException, ServletException {
    final ServletRequestContext requestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (parts == null) {
        final List<Part> parts = new ArrayList<>();
        String mimeType = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE);
        if (mimeType != null && mimeType.startsWith(MultiPartParserDefinition.MULTIPART_FORM_DATA)) {
            FormData formData = parseFormData();
            if (formData != null) {
                for (final String namedPart : formData) {
                    for (FormData.FormValue part : formData.get(namedPart)) {
                        parts.add(new PartImpl(namedPart, part, requestContext.getOriginalServletPathMatch().getServletChain().getManagedServlet().getMultipartConfig(), servletContext, this));
                    }
                }
            }
        } else {
            throw UndertowServletMessages.MESSAGES.notAMultiPartRequest();
        }
        this.parts = parts;
    }
}
Also used : FormData(io.undertow.server.handlers.form.FormData) Part(javax.servlet.http.Part) ArrayList(java.util.ArrayList) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpString(io.undertow.util.HttpString)

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