Search in sources :

Example 16 with Attachment

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

the class AttachmentDeserializerTest method testDoesntReturnZero.

@Test
public void testDoesntReturnZero() throws Exception {
    String contentType = "multipart/mixed;boundary=----=_Part_1";
    byte[] messageBytes = ("------=_Part_1\n\n" + "JJJJ\n" + "------=_Part_1" + "\n\nContent-Transfer-Encoding: binary\n\n" + "ABCD1\r\n" + "------=_Part_1" + "\n\nContent-Transfer-Encoding: binary\n\n" + "ABCD2\r\n" + "------=_Part_1" + "\n\nContent-Transfer-Encoding: binary\n\n" + "ABCD3\r\n" + "------=_Part_1--").getBytes(StandardCharsets.UTF_8);
    ByteArrayInputStream in = new ByteArrayInputStream(messageBytes) {

        public int read(byte[] b, int off, int len) {
            return super.read(b, off, len >= 2 ? 2 : len);
        }
    };
    Message message = new MessageImpl();
    message.put(Message.CONTENT_TYPE, contentType);
    message.setContent(InputStream.class, in);
    message.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, System.getProperty("java.io.tmpdir"));
    message.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String.valueOf(AttachmentDeserializer.THRESHOLD));
    AttachmentDeserializer ad = new AttachmentDeserializer(message, Collections.singletonList("multipart/mixed"));
    ad.initializeAttachments();
    String s = IOUtils.toString(message.getContent(InputStream.class));
    assertEquals("JJJJ", s.trim());
    int count = 1;
    for (Attachment a : message.getAttachments()) {
        s = IOUtils.toString(a.getDataHandler().getInputStream());
        assertEquals("ABCD" + count++, s);
    }
    in.close();
}
Also used : Message(org.apache.cxf.message.Message) XMLMessage(org.apache.cxf.message.XMLMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.cxf.message.Attachment) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 17 with Attachment

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

the class AttachmentDeserializerTest method testInvalidContentDispositionFilename.

@Test
public void testInvalidContentDispositionFilename() throws Exception {
    StringBuilder sb = new StringBuilder(1000);
    sb.append("SomeHeader: foo\n").append("------=_Part_34950_1098328613.1263781527359\n").append("Content-Type: text/xml; charset=UTF-8\n").append("Content-Transfer-Encoding: binary\n").append("Content-Id: <318731183421.1263781527359.IBM.WEBSERVICES@auhpap02>\n").append('\n').append("<envelope/>\n");
    sb.append("------=_Part_34950_1098328613.1263781527359\n").append("Content-Type: text/xml\n").append("Content-Transfer-Encoding: binary\n").append("Content-Id: <b86a5f2d-e7af-4e5e-b71a-9f6f2307cab0>\n").append("Content-Disposition: attachment; filename=../../../../../../../../etc/passwd\n").append('\n').append("<message>\n").append("------=_Part_34950_1098328613.1263781527359--\n");
    msg = new MessageImpl();
    msg.setContent(InputStream.class, new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8)));
    msg.put(Message.CONTENT_TYPE, "multipart/related");
    AttachmentDeserializer ad = new AttachmentDeserializer(msg);
    ad.initializeAttachments();
    // Force it to load the attachments
    assertEquals(1, msg.getAttachments().size());
    Attachment attachment = msg.getAttachments().iterator().next();
    AttachmentDataSource dataSource = (AttachmentDataSource) attachment.getDataHandler().getDataSource();
    assertEquals("passwd", dataSource.getName());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Attachment(org.apache.cxf.message.Attachment) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 18 with Attachment

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

the class AttachmentDeserializerTest method testDeserializerSwAWithoutBoundryInContentType.

@Test
public void testDeserializerSwAWithoutBoundryInContentType() throws Exception {
    InputStream is = getClass().getResourceAsStream("swadata");
    String ct = "multipart/related; type=\"text/xml\"; ";
    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);
    assertFalse(itr.hasNext());
    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 19 with Attachment

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

the class AttachmentSerializerTest method testMessageMTOM.

@Test
public void testMessageMTOM() 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, "application/soap+xml");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    msg.setContent(OutputStream.class, out);
    AttachmentSerializer serializer = new AttachmentSerializer(msg);
    serializer.writeProlog();
    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=\"application/soap+xml\"") > -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);
    assertEquals("application/xop+xml; charset=UTF-8; type=\"application/soap+xml\"", 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) Test(org.junit.Test)

Example 20 with Attachment

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

the class WrappedMessageContextTest method testPutAndGetJaxwsAttachments.

@Test
public void testPutAndGetJaxwsAttachments() throws Exception {
    WrappedMessageContext context = new WrappedMessageContext(new HashMap<String, Object>(), null, Scope.APPLICATION);
    DataHandler dh1 = new DataHandler(new ByteArrayDataSource("Hello world!".getBytes(), "text/plain"));
    DataHandler dh2 = new DataHandler(new ByteArrayDataSource("Hola mundo!".getBytes(), "text/plain"));
    DataHandler dh3 = new DataHandler(new ByteArrayDataSource("Bonjour tout le monde!".getBytes(), "text/plain"));
    Map<String, DataHandler> jattachments = new HashMap<>();
    context.put(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS, jattachments);
    jattachments.put("attachment-1", dh1);
    Set<Attachment> cattachments = CastUtils.cast((Set<?>) context.get(Message.ATTACHMENTS));
    assertNotNull(cattachments);
    assertEquals(1, cattachments.size());
    jattachments.put("attachment-2", dh2);
    assertEquals(2, cattachments.size());
    AttachmentImpl ca = new AttachmentImpl("attachment-3", dh3);
    ca.setHeader("X-test", "true");
    cattachments.add(ca);
    assertEquals(3, jattachments.size());
    assertEquals(3, cattachments.size());
    for (Attachment a : cattachments) {
        if ("attachment-1".equals(a.getId())) {
            assertEquals("Hello world!", a.getDataHandler().getContent());
        } else if ("attachment-2".equals(a.getId())) {
            assertEquals("Hola mundo!", a.getDataHandler().getContent());
        } else if ("attachment-3".equals(a.getId())) {
            assertEquals("Bonjour tout le monde!", a.getDataHandler().getContent());
            assertEquals("true", a.getHeader("X-test"));
        } else {
            fail("unknown attachment");
        }
    }
}
Also used : HashMap(java.util.HashMap) Attachment(org.apache.cxf.message.Attachment) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) 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