use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class MultipartStore method addBookFormImage.
@POST
@Path("/books/formimage")
@Consumes("multipart/form-data")
@Produces("multipart/form-data")
public MultipartBody addBookFormImage(MultipartBody image) throws Exception {
List<Attachment> atts = image.getAllAttachments();
if (atts.size() != 1) {
throw new WebApplicationException();
}
List<Attachment> newAtts = new ArrayList<>();
Attachment at = atts.get(0);
MultivaluedMap<String, String> headers = at.getHeaders();
if (!"http://host/bar".equals(headers.getFirst("Content-Location"))) {
throw new WebApplicationException();
}
if (!"custom".equals(headers.getFirst("Custom-Header"))) {
throw new WebApplicationException();
}
headers.putSingle("Content-Location", "http://host/location");
newAtts.add(new Attachment(at.getContentId(), at.getDataHandler(), headers));
return new MultipartBody(newAtts);
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class MultipartModificationFilter method filter.
@Override
public void filter(List<Attachment> atts) {
Attachment dataPart = atts.remove(0);
Attachment newDataPart = new Attachment(new ByteArrayInputStream("Hi".getBytes()), dataPart.getHeaders());
atts.add(0, newDataPart);
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class JAXRSClientServerResourceJacksonSpringProviderTest method testMultipart.
@Test
public void testMultipart() throws Exception {
String endpointAddress = "http://localhost:" + PORT + "/webapp/multipart";
MultipartStore proxy = JAXRSClientFactory.create(endpointAddress, MultipartStore.class, Collections.singletonList(new JacksonJsonProvider()));
Book json = new Book("json", 1L);
InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
Map<String, Object> attachments = proxy.addBookJsonImageStream(json, is1);
assertEquals(2, attachments.size());
Book json2 = ((Attachment) attachments.get("application/json")).getObject(Book.class);
assertEquals("json", json2.getName());
assertEquals(1L, json2.getId());
InputStream is2 = ((Attachment) attachments.get("application/octet-stream")).getObject(InputStream.class);
byte[] image1 = IOUtils.readBytesFromStream(getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));
byte[] image2 = IOUtils.readBytesFromStream(is2);
assertArrayEquals(image1, image2);
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class JAXRSMultipartTest method doTestNullPart.
private void doTestNullPart(String address) throws Exception {
WebClient client = WebClient.create(address);
client.type("multipart/form-data").accept("text/plain");
List<Attachment> atts = new LinkedList<>();
atts.add(new Attachment("somepart", "text/plain", "hello there"));
Response r = client.postCollection(atts, Attachment.class);
assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
assertEquals("nobody home", IOUtils.readStringFromStream((InputStream) r.getEntity()));
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class JAXRSMultipartTest method testNullableParamsPrimitive.
@Test
public void testNullableParamsPrimitive() throws Exception {
String address = "http://localhost:" + PORT + "/bookstore/books/testnullpartprimitive";
WebClient client = WebClient.create(address);
client.type("multipart/form-data").accept("text/plain");
List<Attachment> atts = new LinkedList<>();
atts.add(new Attachment("somepart", "text/plain", "hello there"));
Response r = client.postCollection(atts, Attachment.class);
assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
assertEquals((Integer) 0, Integer.valueOf(IOUtils.readStringFromStream((InputStream) r.getEntity())));
}
Aggregations