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);
}
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();
}
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();
}
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);
}
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);
}
Aggregations