Search in sources :

Example 46 with Attachment

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

the class AttachmentSerializerTest method doTestMessageWrite.

private void doTestMessageWrite(boolean xop, String soapContentType) throws Exception {
    MessageImpl msg = new MessageImpl();
    Collection<Attachment> atts = new ArrayList<>();
    AttachmentImpl a = new AttachmentImpl("test.xml");
    InputStream is = getClass().getResourceAsStream("my.wav");
    ByteArrayDataSource ds = new ByteArrayDataSource(is, "application/octet-stream");
    a.setDataHandler(new DataHandler(ds));
    atts.add(a);
    msg.setAttachments(atts);
    // Set the SOAP content type
    msg.put(Message.CONTENT_TYPE, soapContentType);
    final String soapCtType;
    final String soapCtParams;
    final String soapCtParamsEscaped;
    int p = soapContentType.indexOf(';');
    if (p != -1) {
        soapCtParams = soapContentType.substring(p);
        soapCtParamsEscaped = escapeQuotes(soapCtParams);
        soapCtType = soapContentType.substring(0, p);
    } else {
        soapCtParams = "";
        soapCtParamsEscaped = "";
        soapCtType = soapContentType;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    msg.setContent(OutputStream.class, out);
    AttachmentSerializer serializer = new AttachmentSerializer(msg);
    if (!xop) {
        // default is "on"
        serializer.setXop(xop);
    }
    serializer.writeProlog();
    // we expect the following rules at the package header level
    // - the package header must have media type multipart/related.
    // - the start-info property must be present for mtom but otherwise optional. its
    // value must contain the content type associated with the root content's xml serialization,
    // including its parameters as appropriate.
    // - the action property should not appear directly in the package header level
    // - the type property must contain the media type type/subtype of the root content part.
    // namely application/xop+xml for mtom but otherwise text/xml or application/soap+xml
    // depending on the soap version 1.1 or 1.2, respectively.
    String ct = (String) msg.get(Message.CONTENT_TYPE);
    assertTrue(ct.indexOf("multipart/related;") == 0);
    assertTrue(ct.indexOf("start=\"<root.message@cxf.apache.org>\"") > -1);
    assertTrue(ct.indexOf("start-info=\"" + soapCtType + soapCtParamsEscaped + "\"") > -1);
    assertTrue(ct.indexOf("action=\"") == -1);
    if (xop) {
        assertTrue(ct.indexOf("type=\"application/xop+xml\"") > -1);
    } else {
        assertTrue(ct.indexOf("type=\"" + soapCtType + "\"") > -1);
    }
    out.write("<soap:Body/>".getBytes());
    serializer.writeAttachments();
    out.flush();
    DataSource source = new ByteArrayDataSource(new ByteArrayInputStream(out.toByteArray()), ct);
    MimeMultipart mpart = new MimeMultipart(source);
    Session session = Session.getDefaultInstance(new Properties());
    MimeMessage inMsg = new MimeMessage(session);
    inMsg.setContent(mpart);
    inMsg.addHeaderLine("Content-Type: " + ct);
    MimeMultipart multipart = (MimeMultipart) inMsg.getContent();
    MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(0);
    // - the action must appear if it was present in the original message (i.e., for soap 1.2)
    if (xop) {
        assertEquals("application/xop+xml; charset=UTF-8; type=\"" + soapCtType + soapCtParamsEscaped + "\"", part.getHeader("Content-Type")[0]);
    } else {
        assertEquals(soapCtType + "; charset=UTF-8" + soapCtParams, part.getHeader("Content-Type")[0]);
    }
    assertEquals("binary", part.getHeader("Content-Transfer-Encoding")[0]);
    assertEquals("<root.message@cxf.apache.org>", part.getHeader("Content-ID")[0]);
    InputStream in = part.getDataHandler().getInputStream();
    ByteArrayOutputStream bodyOut = new ByteArrayOutputStream();
    IOUtils.copy(in, bodyOut);
    out.close();
    in.close();
    assertEquals("<soap:Body/>", bodyOut.toString());
    MimeBodyPart part2 = (MimeBodyPart) multipart.getBodyPart(1);
    assertEquals("application/octet-stream", part2.getHeader("Content-Type")[0]);
    assertEquals("binary", part2.getHeader("Content-Transfer-Encoding")[0]);
    assertEquals("<test.xml>", part2.getHeader("Content-ID")[0]);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMultipart(javax.mail.internet.MimeMultipart) MimeMessage(javax.mail.internet.MimeMessage) MimeBodyPart(javax.mail.internet.MimeBodyPart) MessageImpl(org.apache.cxf.message.MessageImpl) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Session(javax.mail.Session)

Example 47 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 48 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 49 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 50 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)

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