Search in sources :

Example 36 with Attachment

use of org.apache.cxf.message.Attachment in project cxf by apache.

the class AttachmentDeserializerTest method testDeserializerSwA.

@Test
public void testDeserializerSwA() throws Exception {
    InputStream is = getClass().getResourceAsStream("swadata");
    String ct = "multipart/related; type=\"text/xml\"; " + "start=\"<86048FF3556694F7DA1918466DDF8143>\";    " + "boundary=\"----=_Part_0_14158819.1167275505862\"";
    msg.put(Message.CONTENT_TYPE, ct);
    msg.setContent(InputStream.class, is);
    AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
    deserializer.initializeAttachments();
    InputStream attBody = msg.getContent(InputStream.class);
    assertTrue(attBody != is);
    assertTrue(attBody instanceof DelegatingInputStream);
    Collection<Attachment> atts = msg.getAttachments();
    assertNotNull(atts);
    Iterator<Attachment> itr = atts.iterator();
    assertTrue(itr.hasNext());
    Attachment a = itr.next();
    assertNotNull(a);
    InputStream attIs = a.getDataHandler().getInputStream();
    // check the cached output stream
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        IOUtils.copy(attBody, out);
        assertTrue(out.toString().startsWith("<?xml"));
    }
    // try streaming a character off the wire
    assertTrue(attIs.read() == 'f');
    assertTrue(attIs.read() == 'o');
    assertTrue(attIs.read() == 'o');
    assertTrue(attIs.read() == 'b');
    assertTrue(attIs.read() == 'a');
    assertTrue(attIs.read() == 'r');
    assertTrue(attIs.read() == -1);
    is.close();
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.message.Attachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 37 with Attachment

use of org.apache.cxf.message.Attachment in project cxf by apache.

the class AttachmentDeserializerTest method testDeserializerMtom.

@Test
public void testDeserializerMtom() throws Exception {
    InputStream is = getClass().getResourceAsStream("mimedata");
    String ct = "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml; charset=utf-8\"; " + "boundary=\"----=_Part_4_701508.1145579811786\"";
    msg.put(Message.CONTENT_TYPE, ct);
    msg.setContent(InputStream.class, is);
    AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
    deserializer.initializeAttachments();
    InputStream attBody = msg.getContent(InputStream.class);
    assertTrue(attBody != is);
    assertTrue(attBody instanceof DelegatingInputStream);
    Collection<Attachment> atts = msg.getAttachments();
    assertNotNull(atts);
    Iterator<Attachment> itr = atts.iterator();
    assertTrue(itr.hasNext());
    Attachment a = itr.next();
    assertNotNull(a);
    InputStream attIs = a.getDataHandler().getInputStream();
    // check the cached output stream
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        IOUtils.copy(attBody, out);
        assertTrue(out.toString().startsWith("<env:Envelope"));
    }
    // try streaming a character off the wire
    assertEquals(255, attIs.read());
    assertEquals(216, (char) attIs.read());
    // Attachment invalid = atts.get("INVALID");
    // assertNull(invalid.getDataHandler().getInputStream());
    // 
    // assertTrue(attIs instanceof ByteArrayInputStream);
    is.close();
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.message.Attachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 38 with Attachment

use of org.apache.cxf.message.Attachment in project cxf by apache.

the class AttachmentDeserializerTest method testDeserializerWithCachedFile.

@Test
public void testDeserializerWithCachedFile() throws Exception {
    InputStream is = getClass().getResourceAsStream("mimedata");
    String ct = "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml; charset=utf-8\"; " + "boundary=\"----=_Part_4_701508.1145579811786\"";
    msg.put(Message.CONTENT_TYPE, ct);
    msg.setContent(InputStream.class, is);
    msg.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, "10");
    AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
    deserializer.initializeAttachments();
    InputStream attBody = msg.getContent(InputStream.class);
    assertTrue(attBody != is);
    assertTrue(attBody instanceof DelegatingInputStream);
    Collection<Attachment> atts = msg.getAttachments();
    assertNotNull(atts);
    Iterator<Attachment> itr = atts.iterator();
    assertTrue(itr.hasNext());
    Attachment a = itr.next();
    assertNotNull(a);
    InputStream attIs = a.getDataHandler().getInputStream();
    assertFalse(itr.hasNext());
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        IOUtils.copy(attIs, out);
        assertTrue(out.size() > 1000);
    }
    is.close();
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.message.Attachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 39 with Attachment

use of org.apache.cxf.message.Attachment in project cxf by apache.

the class AttachmentDeserializerTest method testLazyAttachmentCollection.

@Test
public void testLazyAttachmentCollection() throws Exception {
    InputStream is = getClass().getResourceAsStream("mimedata2");
    String ct = "multipart/related; type=\"application/xop+xml\"; " + "start=\"<soap.xml@xfire.codehaus.org>\"; " + "start-info=\"text/xml; charset=utf-8\"; " + "boundary=\"----=_Part_4_701508.1145579811786\"";
    msg.put(Message.CONTENT_TYPE, ct);
    msg.setContent(InputStream.class, is);
    AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
    deserializer.initializeAttachments();
    InputStream attBody = msg.getContent(InputStream.class);
    assertTrue(attBody != is);
    assertTrue(attBody instanceof DelegatingInputStream);
    attBody.close();
    assertEquals(2, msg.getAttachments().size());
    List<String> cidlist = new ArrayList<>();
    cidlist.add("xfire_logo.jpg");
    cidlist.add("xfire_logo2.jpg");
    for (Iterator<Attachment> it = msg.getAttachments().iterator(); it.hasNext(); ) {
        Attachment a = it.next();
        assertTrue(cidlist.remove(a.getId()));
        it.remove();
    }
    assertEquals(0, cidlist.size());
    assertEquals(0, msg.getAttachments().size());
    is.close();
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.message.Attachment) Test(org.junit.Test)

Example 40 with Attachment

use of org.apache.cxf.message.Attachment in project cxf by apache.

the class JAXBAttachmentMarshaller method addMtomAttachment.

public String addMtomAttachment(DataHandler handler, String elementNS, String elementLocalName) {
    Attachment att = AttachmentUtil.createMtomAttachmentFromDH(isXop, handler, elementNS, threshold);
    if (att != null) {
        atts.add(att);
        lastElementName = new QName(elementNS, elementLocalName);
        return "cid:" + att.getId();
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) Attachment(org.apache.cxf.message.Attachment)

Aggregations

Attachment (org.apache.cxf.message.Attachment)51 DataHandler (javax.activation.DataHandler)18 Test (org.junit.Test)18 InputStream (java.io.InputStream)14 Message (org.apache.cxf.message.Message)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 AttachmentImpl (org.apache.cxf.attachment.AttachmentImpl)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 MessageImpl (org.apache.cxf.message.MessageImpl)11 HashMap (java.util.HashMap)10 List (java.util.List)9 PushbackInputStream (java.io.PushbackInputStream)7 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)6 Fault (org.apache.cxf.interceptor.Fault)6 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)6 Map (java.util.Map)5 OutputStream (java.io.OutputStream)4 TreeMap (java.util.TreeMap)4