use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class FormUtils method populateMapFromMultipart.
public static void populateMapFromMultipart(MultivaluedMap<String, String> params, MultipartBody body, Message m, boolean decode) {
List<Attachment> atts = body.getAllAttachments();
checkNumberOfParts(m, atts.size());
for (Attachment a : atts) {
ContentDisposition cd = a.getContentDisposition();
if (cd != null && !MULTIPART_FORM_DATA_TYPE.equalsIgnoreCase(cd.getType())) {
continue;
}
String cdName = cd == null ? null : cd.getParameter("name");
String contentId = a.getContentId();
String name = StringUtils.isEmpty(cdName) ? contentId : cdName.replace("\"", "").replace("'", "");
if (StringUtils.isEmpty(name)) {
throw ExceptionUtils.toBadRequestException(null, null);
}
if (CONTENT_DISPOSITION_FILES_PARAM.equals(name)) {
// this is a reserved name in Content-Disposition for parts containing files
continue;
}
try {
String value = IOUtils.toString(a.getDataHandler().getInputStream());
params.add(HttpUtils.urlDecode(name), decode ? HttpUtils.urlDecode(value) : value);
} catch (IllegalArgumentException ex) {
LOG.warning("Illegal URL-encoded characters, make sure that no " + "@FormParam and @Multipart annotations are mixed up");
throw ExceptionUtils.toInternalServerErrorException(ex, null);
} catch (IOException ex) {
throw ExceptionUtils.toBadRequestException(null, null);
}
}
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class JAXRSMultipartTest method testUploadFileWithSemicolonName.
@Test
public void testUploadFileWithSemicolonName() throws Exception {
String address = "http://localhost:" + PORT + "/bookstore/books/file/semicolon";
WebClient client = WebClient.create(address);
client.type("multipart/form-data").accept("text/plain");
ContentDisposition cd = new ContentDisposition("attachment;name=\"a\";filename=\"a;txt\"");
MultivaluedMap<String, String> headers = new MetadataMap<>();
headers.putSingle("Content-Disposition", cd.toString());
Attachment att = new Attachment(new ByteArrayInputStream("file name with semicolon".getBytes()), headers);
MultipartBody body = new MultipartBody(att);
String partContent = client.post(body, String.class);
assertEquals("file name with semicolon, filename:" + "a;txt", partContent);
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class JAXRSMultipartTest method testUploadImageFromForm.
@Test
public void testUploadImageFromForm() throws Exception {
InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
String address = "http://localhost:" + PORT + "/bookstore/books/formimage";
WebClient client = WebClient.create(address);
HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
conduit.getClient().setReceiveTimeout(1000000);
conduit.getClient().setConnectionTimeout(1000000);
client.type("multipart/form-data").accept("multipart/form-data");
ContentDisposition cd = new ContentDisposition("attachment;filename=java.jpg");
MultivaluedMap<String, String> headers = new MetadataMap<>();
headers.putSingle("Content-ID", "image");
headers.putSingle("Content-Disposition", cd.toString());
headers.putSingle("Content-Location", "http://host/bar");
headers.putSingle("custom-header", "custom");
Attachment att = new Attachment(is1, headers);
MultipartBody body = new MultipartBody(att);
MultipartBody body2 = client.post(body, MultipartBody.class);
InputStream is2 = body2.getRootAttachment().getDataHandler().getInputStream();
byte[] image1 = IOUtils.readBytesFromStream(getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));
byte[] image2 = IOUtils.readBytesFromStream(is2);
assertArrayEquals(image1, image2);
ContentDisposition cd2 = body2.getRootAttachment().getContentDisposition();
assertEquals("attachment;filename=java.jpg", cd2.toString());
assertEquals("java.jpg", cd2.getParameter("filename"));
assertEquals("http://host/location", body2.getRootAttachment().getHeader("Content-Location"));
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class JAXRSMultipartTest method doTestAddBookJaxbJsonImageWebClient.
private Map<String, String> doTestAddBookJaxbJsonImageWebClient(String multipartType) throws Exception {
String address = "http://localhost:" + PORT + "/bookstore/books/jaxbjsonimage";
WebClient client = WebClient.create(address);
WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor());
client.type(multipartType).accept(multipartType);
Book jaxb = new Book("jaxb", 1L);
Book json = new Book("json", 2L);
InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
Map<String, Object> objects = new LinkedHashMap<>();
objects.put(MediaType.APPLICATION_XML, jaxb);
objects.put(MediaType.APPLICATION_JSON, json);
objects.put(MediaType.APPLICATION_OCTET_STREAM, is1);
Collection<? extends Attachment> coll = client.postAndGetCollection(objects, Attachment.class);
List<Attachment> result = new ArrayList<>(coll);
Book jaxb2 = readBookFromInputStream(result.get(0).getDataHandler().getInputStream());
assertEquals("jaxb", jaxb2.getName());
assertEquals(1L, jaxb2.getId());
Book json2 = readJSONBookFromInputStream(result.get(1).getDataHandler().getInputStream());
assertEquals("json", json2.getName());
assertEquals(2L, json2.getId());
InputStream is2 = result.get(2).getDataHandler().getInputStream();
byte[] image1 = IOUtils.readBytesFromStream(getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));
byte[] image2 = IOUtils.readBytesFromStream(is2);
assertArrayEquals(image1, image2);
String ctString = client.getResponse().getMetadata().getFirst("Content-Type").toString();
MediaType mt = MediaType.valueOf(ctString);
return mt.getParameters();
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class JAXRSMultipartTest method testLargerThanDefaultHeader.
// The Content Disposition header will be accepted here, even though it is larger than the default,
// as we have configured a larger value on the service side
@Test
public void testLargerThanDefaultHeader() throws Exception {
InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
String address = "http://localhost:" + PORT + "/bookstore/books/image";
WebClient client = WebClient.create(address);
client.type("multipart/mixed").accept("multipart/mixed");
WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true");
StringBuilder sb = new StringBuilder(64);
sb.append("form-data;");
for (int i = 0; i < 35; i++) {
sb.append("aaaaaaaaaa");
}
MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
headers.putSingle("Content-ID", "root");
headers.putSingle("Content-Type", "application/octet-stream");
headers.putSingle("Content-Disposition", sb.toString());
DataHandler handler = new DataHandler(new InputStreamDataSource(is1, "application/octet-stream"));
Attachment att = new Attachment(headers, handler, null);
Response response = client.post(att);
assertEquals(response.getStatus(), 200);
client.close();
}
Aggregations