Search in sources :

Example 66 with ByteArrayDataSource

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

the class WrappedAttachmentsTest method testCreateAndModify.

@Test
public void testCreateAndModify() {
    Map<String, DataHandler> content = new HashMap<>();
    content.put("att-1", new DataHandler(new ByteArrayDataSource("Hello world!".getBytes(), "text/plain")));
    content.put("att-2", new DataHandler(new ByteArrayDataSource("Hola mundo!".getBytes(), "text/plain")));
    WrappedAttachments attachments = new WrappedAttachments(content);
    Attachment att3 = new AttachmentImpl("att-3", new DataHandler(new ByteArrayDataSource("Bonjour tout le monde!".getBytes(), "text/plain")));
    assertEquals(2, attachments.size());
    assertFalse(attachments.isEmpty());
    attachments.add(att3);
    assertEquals(3, attachments.size());
    attachments.add(att3);
    assertEquals(3, attachments.size());
    attachments.remove(att3);
    assertEquals(2, attachments.size());
    Attachment attx = attachments.iterator().next();
    attachments.remove(attx);
    assertEquals(1, attachments.size());
    Attachment[] atts = attachments.toArray(new Attachment[attachments.size()]);
    assertEquals(1, atts.length);
    assertEquals("att-1".equals(attx.getId()) ? "att-2" : "att-1", atts[0].getId());
    attachments.clear();
    assertTrue(attachments.isEmpty());
    assertTrue(content.isEmpty());
}
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 67 with ByteArrayDataSource

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

the class TestMtomProviderImpl method invoke.

public SOAPMessage invoke(final SOAPMessage request) {
    try {
        System.out.println("=== Received client request ===");
        // create the SOAPMessage
        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPPart part = message.getSOAPPart();
        SOAPEnvelope envelope = part.getEnvelope();
        SOAPBody body = envelope.getBody();
        SOAPBodyElement testResponse = body.addBodyElement(envelope.createName("testXopResponse", null, "http://cxf.apache.org/mime/types"));
        SOAPElement name = testResponse.addChildElement("name", null, "http://cxf.apache.org/mime/types");
        name.setTextContent("return detail + call detail");
        SOAPElement attachinfo = testResponse.addChildElement("attachinfo", null, "http://cxf.apache.org/mime/types");
        SOAPElement include = attachinfo.addChildElement("Include", "xop", "http://www.w3.org/2004/08/xop/include");
        int fileSize = 0;
        try (InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl")) {
            for (int i = pre.read(); i != -1; i = pre.read()) {
                fileSize++;
            }
        }
        int count = 50;
        byte[] data = new byte[fileSize * count];
        for (int x = 0; x < count; x++) {
            this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl").read(data, fileSize * x, fileSize);
        }
        DataHandler dh = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));
        // create the image attachment
        AttachmentPart attachment = message.createAttachmentPart(dh);
        attachment.setContentId("mtom_xop.wsdl");
        message.addAttachmentPart(attachment);
        System.out.println("Adding attachment: " + attachment.getContentId() + ":" + attachment.getSize());
        // add the reference to the image attachment
        include.addAttribute(envelope.createName("href"), "cid:" + attachment.getContentId());
        return message;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : InputStream(java.io.InputStream) AttachmentPart(javax.xml.soap.AttachmentPart) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) DataHandler(javax.activation.DataHandler) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPBody(javax.xml.soap.SOAPBody) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) SOAPBodyElement(javax.xml.soap.SOAPBodyElement)

Example 68 with ByteArrayDataSource

use of javax.mail.util.ByteArrayDataSource in project kernel by exoplatform.

the class MailServiceImpl method sendMessage.

/**
 * {@inheritDoc}
 */
public void sendMessage(Message message) throws Exception {
    MimeMessage mimeMessage = new MimeMessage(getMailSession());
    String FROM = message.getFrom();
    String TO = message.getTo();
    String CC = message.getCC();
    String BCC = message.getBCC();
    String subject = message.getSubject();
    String mimeType = message.getMimeType();
    String body = message.getBody();
    List<Attachment> attachment = message.getAttachment();
    // set From to the message
    if (FROM != null && !FROM.equals("")) {
        InternetAddress sentFrom = new InternetAddress(FROM);
        if (sentFrom.getPersonal() != null) {
            sentFrom.setPersonal(sentFrom.getPersonal(), "UTF-8");
        }
        mimeMessage.setFrom(sentFrom);
    }
    // set To to the message
    InternetAddress[] sendTo = new InternetAddress[getArrs(TO).length];
    for (int i = 0; i < getArrs(TO).length; i++) {
        sendTo[i] = new InternetAddress(getArrs(TO)[i]);
        if (sendTo[i].getPersonal() != null) {
            sendTo[i].setPersonal(sendTo[i].getPersonal(), "UTF-8");
        }
    }
    mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, sendTo);
    // set CC to the message
    if ((getArrs(CC) != null) && (getArrs(CC).length > 0)) {
        InternetAddress[] copyTo = new InternetAddress[getArrs(CC).length];
        for (int i = 0; i < getArrs(CC).length; i++) {
            copyTo[i] = new InternetAddress(getArrs(CC)[i]);
            if (copyTo[i].getPersonal() != null) {
                copyTo[i].setPersonal(copyTo[i].getPersonal(), "UTF-8");
            }
        }
        mimeMessage.setRecipients(javax.mail.Message.RecipientType.CC, copyTo);
    }
    // set BCC to the message
    if ((getArrs(BCC) != null) && (getArrs(BCC).length > 0)) {
        InternetAddress[] bccTo = new InternetAddress[getArrs(BCC).length];
        for (int i = 0; i < getArrs(BCC).length; i++) {
            bccTo[i] = new InternetAddress(getArrs(BCC)[i]);
            if (bccTo[i].getPersonal() != null) {
                bccTo[i].setPersonal(bccTo[i].getPersonal(), "UTF-8");
            }
        }
        mimeMessage.setRecipients(javax.mail.Message.RecipientType.BCC, bccTo);
    }
    // set Subject to the message
    mimeMessage.setSubject(subject);
    mimeMessage.setSubject(message.getSubject(), "UTF-8");
    mimeMessage.setSentDate(new Date());
    MimeMultipart multipPartRoot = new MimeMultipart("mixed");
    MimeMultipart multipPartContent = new MimeMultipart("alternative");
    if (attachment != null && attachment.size() != 0) {
        MimeBodyPart contentPartRoot = new MimeBodyPart();
        if (mimeType != null && mimeType.indexOf("text/plain") > -1)
            contentPartRoot.setContent(body, "text/plain; charset=utf-8");
        else
            contentPartRoot.setContent(body, "text/html; charset=utf-8");
        MimeBodyPart mimeBodyPart1 = new MimeBodyPart();
        mimeBodyPart1.setContent(body, mimeType);
        multipPartContent.addBodyPart(mimeBodyPart1);
        multipPartRoot.addBodyPart(contentPartRoot);
        for (Attachment att : attachment) {
            InputStream is = att.getInputStream();
            MimeBodyPart mimeBodyPart = new MimeBodyPart();
            ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(is, att.getMimeType());
            mimeBodyPart.setDataHandler(new DataHandler(byteArrayDataSource));
            mimeBodyPart.setDisposition(Part.ATTACHMENT);
            if (att.getName() != null)
                mimeBodyPart.setFileName(MimeUtility.encodeText(att.getName(), "utf-8", null));
            multipPartRoot.addBodyPart(mimeBodyPart);
        }
        mimeMessage.setContent(multipPartRoot);
    } else {
        if (mimeType != null && mimeType.indexOf("text/plain") > -1)
            mimeMessage.setContent(body, "text/plain; charset=utf-8");
        else
            mimeMessage.setContent(body, "text/html; charset=utf-8");
    }
    sendMessage(mimeMessage);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) InputStream(java.io.InputStream) Attachment(org.exoplatform.services.mail.Attachment) DataHandler(javax.activation.DataHandler) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 69 with ByteArrayDataSource

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

the class ReadHeaderInterceptorTest method testBadSOAPEnvelopeNamespace.

@Test
public void testBadSOAPEnvelopeNamespace() throws Exception {
    soapMessage = TestUtil.createEmptySoapMessage(Soap12.getInstance(), chain);
    InputStream in = getClass().getResourceAsStream("test-bad-env.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().getVersionMismatch(), 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 70 with ByteArrayDataSource

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

the class TestUtil method createSoapMessage.

public static SoapMessage createSoapMessage(SoapVersion soapVersion, InterceptorChain chain, Class<?> clazz) throws IOException {
    SoapMessage soapMessage = createEmptySoapMessage(soapVersion, chain);
    // setup the message result with attachment.class
    ByteArrayDataSource bads = new ByteArrayDataSource(clazz.getResourceAsStream("primarySoapPart.xml"), "Application/xop+xml");
    String cid = AttachmentUtil.createContentID("http://cxf.apache.org");
    soapMessage.setContent(Attachment.class, new AttachmentImpl(cid, new DataHandler(bads)));
    // setup the message attachments
    Collection<Attachment> attachments = new ArrayList<>();
    soapMessage.setAttachments(attachments);
    // String cidAtt1 = "cid:http://cxf.apache.org/me.bmp";
    // bads = new ByteArrayDataSource(clazz.getResourceAsStream("me.bmp"), "image/bmp");
    // AttachmentImpl att1 = new AttachmentImpl(cidAtt1, new DataHandler(bads));
    // att1.setXOP(true);
    // attachments.add(att1);
    String cidAtt2 = "cid:http://cxf.apache.org/my.wav";
    bads = new ByteArrayDataSource(clazz.getResourceAsStream("my.wav"), "Application/octet-stream");
    AttachmentImpl att2 = new AttachmentImpl(cidAtt2, new DataHandler(bads));
    att2.setXOP(true);
    attachments.add(att2);
    return soapMessage;
}
Also used : ArrayList(java.util.ArrayList) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

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