Search in sources :

Example 21 with DataHandler

use of javax.activation.DataHandler in project camel by apache.

the class MimeMultipartDataFormatTest method roundtripWithTextAttachmentsAndBinaryContent.

@Test
public void roundtripWithTextAttachmentsAndBinaryContent() throws IOException {
    String attContentType = "text/plain";
    String attText = "Attachment Text";
    String attFileName = "Attachment File Name";
    in.setBody("Body text");
    in.setHeader(Exchange.CONTENT_TYPE, "text/plain;charset=iso8859-1;other-parameter=true");
    addAttachment(attContentType, attText, attFileName);
    Exchange result = template.send("direct:roundtripbinarycontent", exchange);
    Message out = result.getOut();
    assertEquals("Body text", out.getBody(String.class));
    assertThat(out.getHeader(Exchange.CONTENT_TYPE, String.class), startsWith("text/plain"));
    assertEquals("iso8859-1", out.getHeader(Exchange.CONTENT_ENCODING));
    assertTrue(out.hasAttachments());
    assertEquals(1, out.getAttachmentNames().size());
    assertThat(out.getAttachmentNames(), hasItem(attFileName));
    DataHandler dh = out.getAttachment(attFileName);
    assertNotNull(dh);
    assertEquals(attContentType, dh.getContentType());
    InputStream is = dh.getInputStream();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    IOHelper.copyAndCloseInput(is, os);
    assertEquals(attText, new String(os.toByteArray()));
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.camel.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringContains.containsString(org.hamcrest.core.StringContains.containsString) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 22 with DataHandler

use of javax.activation.DataHandler in project camel by apache.

the class MimeMultipartDataFormatTest method roundtripWithTextAttachmentsHeadersInline.

@Test
public void roundtripWithTextAttachmentsHeadersInline() throws IOException {
    String attContentType = "text/plain";
    String attText = "Attachment Text";
    String attFileName = "Attachment File Name";
    in.setBody("Body text");
    in.setHeader(Exchange.CONTENT_TYPE, "text/plain;charset=iso8859-1;other-parameter=true");
    in.setHeader(Exchange.CONTENT_ENCODING, "UTF8");
    addAttachment(attContentType, attText, attFileName);
    Exchange result = template.send("direct:roundtripinlineheaders", exchange);
    Message out = result.getOut();
    assertEquals("Body text", out.getBody(String.class));
    assertThat(out.getHeader(Exchange.CONTENT_TYPE, String.class), startsWith("text/plain"));
    assertEquals("UTF8", out.getHeader(Exchange.CONTENT_ENCODING));
    assertTrue(out.hasAttachments());
    assertEquals(1, out.getAttachmentNames().size());
    assertThat(out.getAttachmentNames(), hasItem(attFileName));
    DataHandler dh = out.getAttachment(attFileName);
    assertNotNull(dh);
    assertEquals(attContentType, dh.getContentType());
    InputStream is = dh.getInputStream();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    IOHelper.copyAndCloseInput(is, os);
    assertEquals(attText, new String(os.toByteArray()));
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.camel.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StringContains.containsString(org.hamcrest.core.StringContains.containsString) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 23 with DataHandler

use of javax.activation.DataHandler in project Openfire by igniterealtime.

the class EmailSenderUtility method sendEmail.

public void sendEmail() {
    ByteArrayOutputStream outputStream = null;
    try {
        String host = JiveGlobals.getProperty("mail.smtp.host", "localhost");
        String port = JiveGlobals.getProperty("mail.smtp.port", "25");
        String username = JiveGlobals.getProperty("mail.smtp.username");
        String password = JiveGlobals.getProperty("mail.smtp.password");
        String debugEnabled = JiveGlobals.getProperty("mail.debug");
        boolean sslEnabled = JiveGlobals.getBooleanProperty("mail.smtp.ssl", true);
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", port);
        props.setProperty("mail.smtp.sendpartial", "true");
        props.setProperty("mail.debug", debugEnabled);
        if (sslEnabled) {
            // Register with security provider.
            Security.setProperty("ssl.SocketFactory.provider", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "true");
        }
        if (username != null) {
            props.put("mail.smtp.auth", "true");
        }
        Session session = Session.getInstance(props);
        outputStream = new ByteArrayOutputStream();
        createPdfAttachment(outputStream);
        byte[] bytes = outputStream.toByteArray();
        ByteArrayDataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf");
        MimeBodyPart pdfBodyPart = new MimeBodyPart();
        pdfBodyPart.setDataHandler(new DataHandler(dataSource));
        pdfBodyPart.setFileName("ResultSummary.pdf");
        MimeMultipart multipart = new MimeMultipart();
        multipart.addBodyPart(pdfBodyPart);
        MimeMessage msg = new MimeMessage(session);
        DefaultAdminProvider defaultAdminProvider = new DefaultAdminProvider();
        java.util.List<JID> adminList = defaultAdminProvider.getAdmins();
        java.util.List<String> adminListEmails = new ArrayList<String>();
        UserManager manager = UserManager.getInstance();
        Log.info("Number of Admins " + adminList.size());
        for (int i = 0; i < adminList.size(); i++) {
            User user;
            try {
                user = manager.getUser(adminList.get(i).getNode().toString());
                Log.info("Admin Emails: " + user.getEmail());
                adminListEmails.add(user.getEmail());
            } catch (Exception ex) {
                continue;
            }
        }
        // java.util.List<String> recipientsList=Arrays.asList("", "", "");
        InternetAddress[] recipients = new InternetAddress[adminListEmails.size()];
        for (int i = 0; i < adminListEmails.size(); i++) {
            recipients[i] = new InternetAddress(adminListEmails.get(i).toString());
        }
        msg.setFrom(new InternetAddress("no-reply@openfire.org", "Openfire Admin"));
        msg.setRecipients(javax.mail.Message.RecipientType.TO, recipients);
        msg.setSubject("MONITORING REPORT - " + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date()));
        msg.setContent(multipart);
        if (username != null) {
            URLName url = new URLName("smtp", host, Integer.parseInt(port), "", username, password);
            Transport transport = new com.sun.mail.smtp.SMTPTransport(session, url);
            transport.connect(host, Integer.parseInt(port), username, password);
            transport.sendMessage(msg, msg.getRecipients(MimeMessage.RecipientType.TO));
        } else
            Transport.send(msg);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Could not send email");
    }
}
Also used : DefaultAdminProvider(org.jivesoftware.openfire.admin.DefaultAdminProvider) User(org.jivesoftware.openfire.user.User) javax.mail(javax.mail) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) JID(org.xmpp.packet.JID) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) java.util(java.util) org.jivesoftware.util(org.jivesoftware.util) UserManager(org.jivesoftware.openfire.user.UserManager) SimpleDateFormat(java.text.SimpleDateFormat)

Example 24 with DataHandler

use of javax.activation.DataHandler in project quickstarts by jboss-switchyard.

the class ExternalCustomProcessor method process.

@Override
public void process(Exchange exchange) throws Exception {
    String newFileName = "resized-switchyard.jpeg";
    Image input = exchange.getIn().getBody(Image.class);
    if (input == null) {
        throw new RuntimeException("Image for resize not found!");
    }
    Image img = ImageIO.read(Classes.getResourceAsStream("switchyard.jpeg"));
    exchange.getOut().addAttachment(newFileName, new DataHandler(img, "image/jpeg"));
    exchange.getOut().setBody(newFileName);
}
Also used : DataHandler(javax.activation.DataHandler) Image(java.awt.Image)

Example 25 with DataHandler

use of javax.activation.DataHandler in project quickstarts by jboss-switchyard.

the class InternalCustomProcessor method process.

@Override
public void process(Exchange exchange) throws Exception {
    String newFileName = "internal-switchyard.jpeg";
    Image input = exchange.getIn().getBody(Image.class);
    if (input == null) {
        throw new RuntimeException("Image for resize not found!");
    }
    ContentType type = new ContentType("image/jpeg");
    exchange.getOut().addAttachment(newFileName, new DataHandler(input, type.getBaseType()));
    exchange.getOut().setBody(newFileName);
}
Also used : ContentType(javax.mail.internet.ContentType) DataHandler(javax.activation.DataHandler) Image(java.awt.Image)

Aggregations

DataHandler (javax.activation.DataHandler)180 Exchange (org.apache.camel.Exchange)39 MimeBodyPart (javax.mail.internet.MimeBodyPart)38 FileDataSource (javax.activation.FileDataSource)33 Test (org.junit.Test)33 DataSource (javax.activation.DataSource)32 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)29 IOException (java.io.IOException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 InputStream (java.io.InputStream)25 MessagingException (javax.mail.MessagingException)25 MimeMultipart (javax.mail.internet.MimeMultipart)25 MimeMessage (javax.mail.internet.MimeMessage)23 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Message (org.apache.camel.Message)21 OMElement (org.apache.axiom.om.OMElement)17 Processor (org.apache.camel.Processor)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 File (java.io.File)13 PipedInputStream (java.io.PipedInputStream)13