Search in sources :

Example 36 with Attachment

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

the class JAXRSClientServerTikaTest method testUploadIndexAndSearchPdfFile.

@Test
public void testUploadIndexAndSearchPdfFile() {
    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));
    final Collection<ScoreDoc> hits = search("dcterms:modified=le=2007-09-16T09:00:00");
    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 37 with Attachment

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

the class BookCatalog method addBook.

@POST
@Consumes("multipart/form-data")
public Response addBook(final MultipartBody body) throws Exception {
    for (final Attachment attachment : body.getAllAttachments()) {
        final DataHandler handler = attachment.getDataHandler();
        if (handler != null) {
            final String source = handler.getName();
            final LuceneDocumentMetadata metadata = new LuceneDocumentMetadata().withSource(source).withField("modified", Date.class).withField("dcterms:modified", Date.class);
            final Document document = extractor.extract(handler.getInputStream(), metadata);
            if (document != null) {
                try (IndexWriter writer = getIndexWriter()) {
                    writer.addDocument(document);
                    writer.commit();
                }
            }
        }
    }
    return Response.ok().build();
}
Also used : LuceneDocumentMetadata(org.apache.cxf.jaxrs.ext.search.tika.LuceneDocumentMetadata) IndexWriter(org.apache.lucene.index.IndexWriter) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) DataHandler(javax.activation.DataHandler) Document(org.apache.lucene.document.Document) Date(java.util.Date) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 38 with Attachment

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

the class JAXRSMultipartTest method testLargeHeader.

// The large Content Disposition header will be rejected here
@Test
public void testLargeHeader() 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(100500);
    sb.append("form-data;");
    for (int i = 0; i < 10000; 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(), 413);
    client.close();
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Response(javax.ws.rs.core.Response) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InputStreamDataSource(org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) DataHandler(javax.activation.DataHandler) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 39 with Attachment

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

the class JAXRSMultipartTest method testAddBookJaxbJsonImageAttachments.

@Test
public void testAddBookJaxbJsonImageAttachments() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/jaxbimagejson";
    WebClient client = WebClient.create(address);
    client.type("multipart/mixed").accept("multipart/mixed");
    Book jaxb = new Book("jaxb", 1L);
    Book json = new Book("json", 2L);
    InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
    List<Attachment> objects = new ArrayList<>();
    objects.add(new Attachment("<theroot>", MediaType.APPLICATION_XML, jaxb));
    objects.add(new Attachment("thejson", MediaType.APPLICATION_JSON, json));
    objects.add(new Attachment("theimage", 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);
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 40 with Attachment

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

the class JAXRSMultipartTest method testAddBookJaxbJsonImageWebClientRelated2.

@Test
public void testAddBookJaxbJsonImageWebClientRelated2() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/jaxbimagejson";
    WebClient client = WebClient.create(address);
    WebClient.getConfig(client).getOutInterceptors().add(new LoggingOutInterceptor());
    WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor());
    client.type("multipart/mixed").accept("multipart/mixed");
    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<>();
    MultivaluedMap<String, String> headers = new MetadataMap<>();
    headers.putSingle("Content-Type", "application/xml");
    headers.putSingle("Content-ID", "theroot");
    headers.putSingle("Content-Transfer-Encoding", "customxml");
    Attachment attJaxb = new Attachment(headers, jaxb);
    headers = new MetadataMap<>();
    headers.putSingle("Content-Type", "application/json");
    headers.putSingle("Content-ID", "thejson");
    headers.putSingle("Content-Transfer-Encoding", "customjson");
    Attachment attJson = new Attachment(headers, json);
    headers = new MetadataMap<>();
    headers.putSingle("Content-Type", "application/octet-stream");
    headers.putSingle("Content-ID", "theimage");
    headers.putSingle("Content-Transfer-Encoding", "customstream");
    Attachment attIs = new Attachment(headers, is1);
    objects.put(MediaType.APPLICATION_XML, attJaxb);
    objects.put(MediaType.APPLICATION_JSON, attJson);
    objects.put(MediaType.APPLICATION_OCTET_STREAM, attIs);
    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);
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) LinkedHashMap(java.util.LinkedHashMap) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) 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