use of javax.activation.DataSource in project camel by apache.
the class MimeMultipartDataFormat method writeBodyPart.
private void writeBodyPart(byte[] bodyContent, Part part, ContentType contentType) throws MessagingException {
DataSource ds = new ByteArrayDataSource(bodyContent, contentType.toString());
part.setDataHandler(new DataHandler(ds));
part.setHeader(CONTENT_TYPE, contentType.toString());
if (contentType.match("text/*")) {
part.setHeader(CONTENT_TRANSFER_ENCODING, "8bit");
} else if (binaryContent) {
part.setHeader(CONTENT_TRANSFER_ENCODING, "binary");
} else {
part.setHeader(CONTENT_TRANSFER_ENCODING, "base64");
}
}
use of javax.activation.DataSource in project camel by apache.
the class MailBinding method populateContentOnMimeMessage.
protected String populateContentOnMimeMessage(MimeMessage part, MailConfiguration configuration, Exchange exchange) throws MessagingException, IOException {
String contentType = determineContentType(configuration, exchange);
LOG.trace("Using Content-Type {} for MimeMessage: {}", contentType, part);
String body = exchange.getIn().getBody(String.class);
if (body == null) {
body = "";
}
// always store content in a byte array data store to avoid various content type and charset issues
DataSource ds = new ByteArrayDataSource(body, contentType);
part.setDataHandler(new DataHandler(ds));
// set the content type header afterwards
part.setHeader("Content-Type", contentType);
return contentType;
}
use of javax.activation.DataSource in project camel by apache.
the class MailBinding method populateContentOnBodyPart.
protected String populateContentOnBodyPart(BodyPart part, MailConfiguration configuration, Exchange exchange) throws MessagingException, IOException {
String contentType = determineContentType(configuration, exchange);
if (contentType != null) {
LOG.trace("Using Content-Type {} for BodyPart: {}", contentType, part);
// always store content in a byte array data store to avoid various content type and charset issues
String data = exchange.getContext().getTypeConverter().tryConvertTo(String.class, exchange.getIn().getBody());
// use empty data if the body was null for some reason (otherwise there is a NPE)
data = data != null ? data : "";
DataSource ds = new ByteArrayDataSource(data, contentType);
part.setDataHandler(new DataHandler(ds));
// set the content type header afterwards
part.setHeader("Content-Type", contentType);
}
return contentType;
}
use of javax.activation.DataSource in project camel by apache.
the class MimeMessageConsumeTest method populateMimeMessageBody.
/**
* Lets encode a multipart mime message
*/
protected void populateMimeMessageBody(MimeMessage message) throws MessagingException {
MimeBodyPart plainPart = new MimeBodyPart();
plainPart.setText(body);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText("<html><body>" + body + "</body></html>");
Multipart alt = new MimeMultipart("alternative");
alt.addBodyPart(plainPart);
alt.addBodyPart(htmlPart);
Multipart mixed = new MimeMultipart("mixed");
MimeBodyPart wrap = new MimeBodyPart();
wrap.setContent(alt);
mixed.addBodyPart(wrap);
mixed.addBodyPart(plainPart);
mixed.addBodyPart(htmlPart);
DataSource ds;
try {
File f = new File(getClass().getResource("/log4j2.properties").toURI());
ds = new FileDataSource(f);
} catch (URISyntaxException ex) {
ds = new URLDataSource(getClass().getResource("/log4j2.properties"));
}
DataHandler dh = new DataHandler(ds);
BodyPart attachmentBodyPart;
// Create another body part
attachmentBodyPart = new MimeBodyPart();
// Set the data handler to the attachment
attachmentBodyPart.setDataHandler(dh);
// Set the filename
attachmentBodyPart.setFileName(dh.getName());
// Set Disposition
attachmentBodyPart.setDisposition(Part.ATTACHMENT);
mixed.addBodyPart(plainPart);
mixed.addBodyPart(htmlPart);
// Add attachmentBodyPart to multipart
mixed.addBodyPart(attachmentBodyPart);
message.setContent(mixed);
}
use of javax.activation.DataSource in project camel by apache.
the class MimeMultipartDataFormatTest method addAttachment.
private void addAttachment(String attContentType, String attText, String attFileName, Map<String, String> headers) throws IOException {
DataSource ds = new ByteArrayDataSource(attText, attContentType);
DefaultAttachment attachment = new DefaultAttachment(ds);
if (headers != null) {
for (String headerName : headers.keySet()) {
attachment.addHeader(headerName, headers.get(headerName));
}
}
in.addAttachmentObject(attFileName, attachment);
}
Aggregations