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