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"));
}
}
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> " + parts.size());
for (Part p : parts) {
out.println("<h3>" + p.getName() + "</h3>");
out.println("<b>Size:</b> " + 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);
}
}
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");
}
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();
}
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>");
}
}
Aggregations