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();
}
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));
}
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());
}
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));
}
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());
}
Aggregations