Search in sources :

Example 16 with Part

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

the class MultiPartInputStreamTest method testRequestTooBigThrowsErrorOnGetParts.

@Test
public void testRequestTooBigThrowsErrorOnGetParts() throws Exception {
    MultipartConfigElement config = new MultipartConfigElement(_dirname, 60, 100, 50);
    MultiPartInputStreamParser mpis = new MultiPartInputStreamParser(new ByteArrayInputStream(_multi.getBytes()), _contentType, config, _tmpDir);
    mpis.setDeleteOnExit(true);
    Collection<Part> parts = null;
    //cause parsing
    try {
        parts = mpis.getParts();
        fail("Request should have exceeded maxRequestSize");
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().startsWith("Request exceeds maxRequestSize"));
    }
    //try again
    try {
        parts = mpis.getParts();
        fail("Request should have exceeded maxRequestSize");
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().startsWith("Request exceeds maxRequestSize"));
    }
}
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 17 with Part

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

the class MultiPartTest method doPost.

/* ------------------------------------------------------------ */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        response.setContentType("text/html");
        ServletOutputStream out = response.getOutputStream();
        out.println("<html>");
        out.println("<HEAD><link rel=\"stylesheet\" type=\"text/css\"  href=\"stylesheet.css\"/></HEAD>");
        out.println("<body>");
        out.println("<h1>Results</h1>");
        out.println("<p>");
        Collection<Part> parts = request.getParts();
        out.println("<b>Parts:</b>&nbsp;" + parts.size());
        for (Part p : parts) {
            out.println("<h3>" + p.getName() + "</h3>");
            out.println("<b>Size:</b>&nbsp;" + p.getSize());
            if (p.getContentType() == null || p.getContentType().startsWith("text/plain")) {
                out.println("<p>");
                IO.copy(p.getInputStream(), out);
                out.println("</p>");
            }
        }
        out.println("</body>");
        out.println("</html>");
        out.flush();
    } catch (ServletException e) {
        throw e;
    } catch (Exception e) {
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletOutputStream(javax.servlet.ServletOutputStream) Part(javax.servlet.http.Part) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 18 with Part

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

the class ServletRequestTest method throw_ISE_when_invalid_part.

@Test
public void throw_ISE_when_invalid_part() throws Exception {
    when(source.getContentType()).thenReturn("multipart/form-data");
    InputStream file = mock(InputStream.class);
    Part part = mock(Part.class);
    when(part.getSize()).thenReturn(0L);
    when(part.getInputStream()).thenReturn(file);
    doThrow(IllegalArgumentException.class).when(source).getPart("param1");
    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("Can't read file part");
    underTest.readInputStreamParam("param1");
}
Also used : InputStream(java.io.InputStream) Part(javax.servlet.http.Part) Test(org.junit.Test)

Example 19 with Part

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

the class ServletRequestTest method read_no_input_stream_when_part_size_is_zero.

@Test
public void read_no_input_stream_when_part_size_is_zero() 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(0L);
    when(source.getPart("param1")).thenReturn(part);
    assertThat(underTest.readInputStreamParam("param1")).isNull();
}
Also used : InputStream(java.io.InputStream) Part(javax.servlet.http.Part) Test(org.junit.Test)

Example 20 with Part

use of javax.servlet.http.Part in project javaee7-samples by javaee-samples.

the class TestServlet method processRequest.

/**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>File Upload Servlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>File Upload Servlet</h1>");
        out.println("Receiving the uploaded file ...<br>");
        out.println("Received " + request.getParts().size() + " parts ...<br>");
        String fileName = "";
        for (Part part : request.getParts()) {
            fileName = part.getSubmittedFileName();
            out.println("... writing " + fileName + " part<br>");
            part.write(fileName);
            out.println("... written<br>");
        }
        out.println("... uploaded to: /tmp/" + fileName);
        out.println("</body>");
        out.println("</html>");
    }
}
Also used : Part(javax.servlet.http.Part) PrintWriter(java.io.PrintWriter)

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