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()));
}
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()));
}
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");
}
}
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);
}
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);
}
Aggregations