Search in sources :

Example 1 with Book

use of common.attachment.Book in project tesb-rt-se by Talend.

the class MultipartsServiceImpl method duplicateMultipartBody.

/**
 * Verifies the MultipartBody by reading the individual parts
 * and copying them to a new MultipartBody instance which
 * will be written out in the multipart/mixed format.
 *
 * @param body the incoming MultipartBody
 * @return new MultipartBody
 */
private MultipartBody duplicateMultipartBody(MultipartBody body) {
    // It is possible to access individual parts by the Content-Id values
    // This MultipartBody is expected to contain 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 read its data in a type safe way by delegating to a matching
    // JAX-RS MessageBodyReader provider
    Book jaxbBook = body.getAttachmentObject("book1", Book.class);
    Book jsonBook = body.getAttachmentObject("book2", Book.class);
    // Accessing individual attachment part, its DataHandler will be
    // used to access the underlying input stream, the type-safe access
    // is also possible
    Attachment imageAtt = body.getAttachment("image");
    if ("JAXB".equals(jaxbBook.getName()) && 1L == jaxbBook.getId() && "JSON".equals(jsonBook.getName()) && 2L == jsonBook.getId() && imageAtt != null) {
        return createMultipartBody(jaxbBook, jsonBook, imageAtt.getDataHandler());
    }
    throw new RuntimeException("Received Book attachment is corrupted");
}
Also used : Book(common.attachment.Book) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment)

Example 2 with Book

use of common.attachment.Book 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 3 with Book

use of common.attachment.Book in project tesb-rt-se by Talend.

the class RESTClient method verifyMultipartResponse.

private void verifyMultipartResponse(MultipartBody bodyResponse) throws Exception {
    Book jaxbBook = bodyResponse.getAttachmentObject("book1", Book.class);
    Book jsonBook = bodyResponse.getAttachmentObject("book2", Book.class);
    byte[] receivedImageBytes = bodyResponse.getAttachmentObject("image", byte[].class);
    InputStream is = getClass().getResourceAsStream("/java.jpg");
    byte[] imageBytes = IOUtils.readBytesFromStream(is);
    if ("JAXB".equals(jaxbBook.getName()) && 1L == jaxbBook.getId() && "JSON".equals(jsonBook.getName()) && 2L == jsonBook.getId() && Arrays.equals(imageBytes, receivedImageBytes)) {
        System.out.println();
        System.out.println("Book attachments have been successfully received");
    } else {
        throw new RuntimeException("Received Book attachment is corrupted");
    }
}
Also used : Book(common.attachment.Book) InputStream(java.io.InputStream)

Aggregations

Book (common.attachment.Book)3 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)2 InputStream (java.io.InputStream)1 LinkedList (java.util.LinkedList)1 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)1