Search in sources :

Example 41 with Attachment

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);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 42 with Attachment

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment)

Example 43 with Attachment

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);
}
Also used : InputStream(java.io.InputStream) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Test(org.junit.Test)

Example 44 with Attachment

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()));
}
Also used : Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) LinkedList(java.util.LinkedList)

Example 45 with Attachment

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())));
}
Also used : Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)79 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)29 InputStream (java.io.InputStream)25 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)25 ByteArrayInputStream (java.io.ByteArrayInputStream)23 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)23 Response (javax.ws.rs.core.Response)17 IOException (java.io.IOException)15 WebClient (org.apache.cxf.jaxrs.client.WebClient)15 CatalogFramework (ddf.catalog.CatalogFramework)10 LinkedHashMap (java.util.LinkedHashMap)9 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)9 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)8 PushbackInputStream (java.io.PushbackInputStream)8 POST (javax.ws.rs.POST)8 Path (javax.ws.rs.Path)8 Metacard (ddf.catalog.data.Metacard)7 InputTransformer (ddf.catalog.transform.InputTransformer)7 LinkedList (java.util.LinkedList)7