Search in sources :

Example 21 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class ReadHeaderInterceptorTest method testBadSOAPEnvelopeName.

@Test
public void testBadSOAPEnvelopeName() throws Exception {
    soapMessage = TestUtil.createEmptySoapMessage(Soap12.getInstance(), chain);
    InputStream in = getClass().getResourceAsStream("test-bad-envname.xml");
    assertNotNull(in);
    ByteArrayDataSource bads = new ByteArrayDataSource(in, "test/xml");
    soapMessage.setContent(InputStream.class, bads.getInputStream());
    ReadHeadersInterceptor r = new ReadHeadersInterceptor(BusFactory.getDefaultBus());
    try {
        r.handleMessage(soapMessage);
        fail("Did not throw exception");
    } catch (SoapFault f) {
        assertEquals(Soap11.getInstance().getSender(), f.getFaultCode());
    }
}
Also used : ReadHeadersInterceptor(org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor) InputStream(java.io.InputStream) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 22 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class ReadHeaderInterceptorTest method prepareSoapMessage.

private void prepareSoapMessage(String message) throws IOException {
    soapMessage = TestUtil.createEmptySoapMessage(Soap12.getInstance(), chain);
    ByteArrayDataSource bads = new ByteArrayDataSource(this.getClass().getResourceAsStream(message), "Application/xop+xml");
    String cid = AttachmentUtil.createContentID("http://cxf.apache.org");
    soapMessage.setContent(Attachment.class, new AttachmentImpl(cid, new DataHandler(bads)));
    soapMessage.setContent(InputStream.class, bads.getInputStream());
}
Also used : AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 23 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource 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)

Example 24 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class AttachmentTest method createAttachment.

private Attachment createAttachment(String id) {
    MetadataMap<String, String> map = new MetadataMap<String, String>();
    map.add("foo", "bar");
    return new Attachment(id, new DataHandler(new ByteArrayDataSource(new byte[] { 1 }, "application/octet-stream")), map);
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 25 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project cxf by apache.

the class MultipartStore method addBookXop.

@POST
@Path("/xop")
@Consumes("multipart/related")
@Produces("multipart/related;type=text/xml")
@Multipart("xop")
public XopType addBookXop(@Multipart XopType type) throws Exception {
    if (!"xopName".equals(type.getName())) {
        throw new RuntimeException("Wrong name property");
    }
    String bookXsd = IOUtils.readStringFromStream(type.getAttachinfo().getInputStream());
    String bookXsd2 = IOUtils.readStringFromStream(getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/book.xsd"));
    if (!bookXsd.equals(bookXsd2)) {
        throw new RuntimeException("Wrong attachinfo property");
    }
    String bookXsdRef = IOUtils.readStringFromStream(type.getAttachInfoRef().getInputStream());
    if (!bookXsdRef.equals(bookXsd2)) {
        throw new RuntimeException("Wrong attachinforef property");
    }
    if (!Boolean.getBoolean("java.awt.headless") && type.getImage() == null) {
        throw new RuntimeException("Wrong image property");
    }
    context.put(org.apache.cxf.message.Message.MTOM_ENABLED, "true");
    XopType xop = new XopType();
    xop.setName("xopName");
    InputStream is = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/book.xsd");
    byte[] data = IOUtils.readBytesFromStream(is);
    xop.setAttachinfo(new DataHandler(new ByteArrayDataSource(data, "application/octet-stream")));
    xop.setAttachInfoRef(new DataHandler(new ByteArrayDataSource(data, "application/octet-stream")));
    xop.setAttachinfo2(bookXsd.getBytes());
    xop.setImage(ImageIO.read(getClass().getResource("/org/apache/cxf/systest/jaxrs/resources/java.jpg")));
    return xop;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Path(javax.ws.rs.Path) Multipart(org.apache.cxf.jaxrs.ext.multipart.Multipart) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)76 DataHandler (javax.activation.DataHandler)58 InputStream (java.io.InputStream)33 IOException (java.io.IOException)25 MimeMultipart (javax.mail.internet.MimeMultipart)23 Test (org.junit.Test)19 DataSource (javax.activation.DataSource)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 MimeBodyPart (javax.mail.internet.MimeBodyPart)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ArrayList (java.util.ArrayList)14 MessagingException (javax.mail.MessagingException)14 MimeMessage (javax.mail.internet.MimeMessage)12 Holder (javax.xml.ws.Holder)7 List (java.util.List)6 AttachmentImpl (org.apache.cxf.attachment.AttachmentImpl)6 Attachment (org.apache.cxf.message.Attachment)6 Date (java.util.Date)5 HashMap (java.util.HashMap)5 ContentDisposition (com.zimbra.common.mime.ContentDisposition)4