Search in sources :

Example 31 with MultipartBody

use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody 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"));
}
Also used : HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 32 with MultipartBody

use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project cxf by apache.

the class JAXRSClientServerTikaTest method testUploadIndexAndSearchPdfFileUsingUserDefinedDatePattern.

@Test
public void testUploadIndexAndSearchPdfFileUsingUserDefinedDatePattern() {
    final WebClient wc = createWebClient("/catalog").type(MediaType.MULTIPART_FORM_DATA);
    final ContentDisposition disposition = new ContentDisposition("attachment;filename=testPDF.pdf");
    final Attachment attachment = new Attachment("root", getClass().getResourceAsStream("/files/testPDF.pdf"), disposition);
    wc.post(new MultipartBody(attachment));
    // Use user-defined date pattern
    final Collection<ScoreDoc> hits = search("dcterms:modified=le=2007/09/16");
    assertEquals(hits.size(), 1);
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) ScoreDoc(org.apache.lucene.search.ScoreDoc) Test(org.junit.Test)

Example 33 with MultipartBody

use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project tesb-rt-se by Talend.

the class RESTClient method useAttachmentServiceWithProxy.

/**
 * Writes and reads the multipart/mixed attachments using a CXF JAX-RS Proxy
 * Note that a custom JAXB-driven JSONProvider is registered to simpify dealing
 * with one of the parts in the JSON format: it is configured to drop namespace
 * prefixes on the write and add a namespace to the incoming payload so that it
 * can be read into the namespace-qualified JAXB Book bean.
 *
 * @throws Exception
 */
public void useAttachmentServiceWithProxy() throws Exception {
    final String serviceURI = "http://localhost:" + port + "/services/attachments";
    JSONProvider provider = new JSONProvider();
    provider.setIgnoreNamespaces(true);
    provider.setInTransformElements(Collections.singletonMap("Book", "{http://books}Book"));
    MultipartsService client = JAXRSClientFactory.create(serviceURI, MultipartsService.class, Collections.singletonList(provider));
    MultipartBody body = createMultipartBody();
    System.out.println();
    System.out.println("Posting Book attachments with a proxy");
    MultipartBody bodyResponse = client.echoAttachment(body);
    verifyMultipartResponse(bodyResponse);
}
Also used : MultipartsService(common.attachment.MultipartsService) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) JSONProvider(org.apache.cxf.jaxrs.provider.json.JSONProvider)

Example 34 with MultipartBody

use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project tesb-rt-se by Talend.

the class RESTClient method createMultipartBody.

/**
 * Creates MultipartBody. It contains 3 parts, "book1", "book2" and "image".
 * These individual parts have their Content-Type set to application/xml,
 * application/json and application/octet-stream
 *
 * MultipartBody will use the Content-Type value of the individual
 * part to write its data by delegating to a matching JAX-RS MessageBodyWriter
 * provider
 *
 * @return
 * @throws Exception
 */
private MultipartBody createMultipartBody() throws Exception {
    List<Attachment> atts = new LinkedList<Attachment>();
    atts.add(new Attachment("book1", "application/xml", new Book("JAXB", 1L)));
    atts.add(new Attachment("book2", "application/json", new Book("JSON", 2L)));
    atts.add(new Attachment("image", "application/octet-stream", getClass().getResourceAsStream("/java.jpg")));
    return new MultipartBody(atts, true);
}
Also used : Book(common.attachment.Book) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) LinkedList(java.util.LinkedList)

Example 35 with MultipartBody

use of org.apache.cxf.jaxrs.ext.multipart.MultipartBody in project tesb-rt-se by Talend.

the class RESTClient method useAttachmentServiceWithWebClient.

/**
 * Writes and reads the multipart/mixed attachments using a CXF JAX-RS WebClient
 * Note that a custom JAXB-driven JSONProvider is registered to simpify dealing
 * with one of the parts in the JSON format: it is configured to drop namespace
 * prefixes on the write and add a namespace to the incoming payload so that it
 * can be read into the namespace-qualified JAXB Book bean.
 *
 * @throws Exception
 */
public void useAttachmentServiceWithWebClient() throws Exception {
    final String serviceURI = "http://localhost:" + port + "/services/attachments/multipart";
    JSONProvider provider = new JSONProvider();
    provider.setIgnoreNamespaces(true);
    provider.setInTransformElements(Collections.singletonMap("Book", "{http://books}Book"));
    WebClient client = WebClient.create(serviceURI, Collections.singletonList(provider));
    client.type("multipart/mixed").accept("multipart/mixed");
    MultipartBody body = createMultipartBody();
    System.out.println();
    System.out.println("Posting Book attachments with a WebClient");
    MultipartBody bodyResponse = client.post(body, MultipartBody.class);
    verifyMultipartResponse(bodyResponse);
}
Also used : MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) JSONProvider(org.apache.cxf.jaxrs.provider.json.JSONProvider) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Aggregations

MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)35 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)25 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)15 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 WebClient (org.apache.cxf.jaxrs.client.WebClient)7 CatalogFramework (ddf.catalog.CatalogFramework)6 InputStream (java.io.InputStream)6 IOException (java.io.IOException)5 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)5 Response (javax.ws.rs.core.Response)5 InputTransformer (ddf.catalog.transform.InputTransformer)4 LinkedHashMap (java.util.LinkedHashMap)4 HttpHeaders (javax.ws.rs.core.HttpHeaders)4 MediaType (javax.ws.rs.core.MediaType)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)3 Map (java.util.Map)3 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)3