use of org.apache.camel.impl.DefaultAttachment in project camel by apache.
the class MailBinding method extractAttachmentsFromMultipart.
protected void extractAttachmentsFromMultipart(Multipart mp, Map<String, Attachment> map) throws MessagingException, IOException {
for (int i = 0; i < mp.getCount(); i++) {
Part part = mp.getBodyPart(i);
LOG.trace("Part #" + i + ": " + part);
if (part.isMimeType("multipart/*")) {
LOG.trace("Part #" + i + ": is mimetype: multipart/*");
extractAttachmentsFromMultipart((Multipart) part.getContent(), map);
} else {
String disposition = part.getDisposition();
String fileName = part.getFileName();
if (LOG.isTraceEnabled()) {
LOG.trace("Part #{}: Disposition: {}", i, disposition);
LOG.trace("Part #{}: Description: {}", i, part.getDescription());
LOG.trace("Part #{}: ContentType: {}", i, part.getContentType());
LOG.trace("Part #{}: FileName: {}", i, fileName);
LOG.trace("Part #{}: Size: {}", i, part.getSize());
LOG.trace("Part #{}: LineCount: {}", i, part.getLineCount());
}
if (validDisposition(disposition, fileName) || fileName != null) {
LOG.debug("Mail contains file attachment: {}", fileName);
if (!map.containsKey(fileName)) {
// Parts marked with a disposition of Part.ATTACHMENT are clearly attachments
DefaultAttachment camelAttachment = new DefaultAttachment(part.getDataHandler());
@SuppressWarnings("unchecked") Enumeration<Header> headers = part.getAllHeaders();
while (headers.hasMoreElements()) {
Header header = headers.nextElement();
camelAttachment.addHeader(header.getName(), header.getValue());
}
map.put(fileName, camelAttachment);
} else {
LOG.warn("Cannot extract duplicate file attachment: {}.", fileName);
}
}
}
}
}
use of org.apache.camel.impl.DefaultAttachment in project camel by apache.
the class MailAttachmentTest method testSendAndReceiveMailWithAttachments.
@Test
public void testSendAndReceiveMailWithAttachments() throws Exception {
// clear mailbox
Mailbox.clearAll();
// START SNIPPET: e1
// create an exchange with a normal body and attachment to be produced as email
Endpoint endpoint = context.getEndpoint("smtp://james@mymailserver.com?password=secret");
// create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setBody("Hello World");
DefaultAttachment att = new DefaultAttachment(new FileDataSource("src/test/data/logo.jpeg"));
att.addHeader("Content-Description", "some sample content");
in.addAttachmentObject("logo.jpeg", att);
// create a producer that can produce the exchange (= send the mail)
Producer producer = endpoint.createProducer();
// start the producer
producer.start();
// and let it go (processes the exchange by sending the email)
producer.process(exchange);
// END SNIPPET: e1
// need some time for the mail to arrive on the inbox (consumed and sent to the mock)
Thread.sleep(2000);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
Exchange out = mock.assertExchangeReceived(0);
mock.assertIsSatisfied();
// plain text
assertEquals("Hello World", out.getIn().getBody(String.class));
// attachment
Map<String, Attachment> attachments = out.getIn().getAttachmentObjects();
assertNotNull("Should have attachments", attachments);
assertEquals(1, attachments.size());
Attachment attachment = out.getIn().getAttachmentObject("logo.jpeg");
DataHandler handler = attachment.getDataHandler();
assertNotNull("The logo should be there", handler);
// content type should match
boolean match1 = "image/jpeg; name=logo.jpeg".equals(handler.getContentType());
boolean match2 = "application/octet-stream; name=logo.jpeg".equals(handler.getContentType());
assertTrue("Should match 1 or 2", match1 || match2);
assertEquals("Handler name should be the file name", "logo.jpeg", handler.getName());
assertEquals("some sample content", attachment.getHeader("content-description"));
producer.stop();
}
use of org.apache.camel.impl.DefaultAttachment in project camel by apache.
the class AttachmentHttpBinding method populateAttachments.
@Override
protected void populateAttachments(HttpServletRequest request, HttpMessage message) {
try {
Collection<Part> parts = request.getParts();
for (Part part : parts) {
DataSource ds = new PartDataSource(part);
Attachment attachment = new DefaultAttachment(ds);
for (String headerName : part.getHeaderNames()) {
for (String headerValue : part.getHeaders(headerName)) {
attachment.addHeader(headerName, headerValue);
}
}
message.addAttachmentObject(part.getName(), attachment);
}
} catch (Exception e) {
throw new RuntimeCamelException("Cannot populate attachments", e);
}
}
use of org.apache.camel.impl.DefaultAttachment in project camel by apache.
the class DefaultUndertowHttpBinding method toCamelMessage.
@Override
public Message toCamelMessage(HttpServerExchange httpExchange, Exchange exchange) throws Exception {
Message result = new DefaultMessage();
populateCamelHeaders(httpExchange, result.getHeaders(), exchange);
// Map form data which is parsed by undertow form parsers
FormData formData = httpExchange.getAttachment(FormDataParser.FORM_DATA);
if (formData != null) {
Map<String, Object> body = new HashMap<>();
formData.forEach(key -> {
formData.get(key).forEach(value -> {
if (value.isFile()) {
DefaultAttachment attachment = new DefaultAttachment(new FilePartDataSource(value));
result.addAttachmentObject(key, attachment);
body.put(key, attachment.getDataHandler());
} else if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(key, value.getValue(), exchange)) {
UndertowHelper.appendHeader(result.getHeaders(), key, value.getValue());
UndertowHelper.appendHeader(body, key, value.getValue());
}
});
});
result.setBody(body);
} else {
//body is extracted as byte[] then auto TypeConverter kicks in
if (Methods.POST.equals(httpExchange.getRequestMethod()) || Methods.PUT.equals(httpExchange.getRequestMethod())) {
result.setBody(readFromChannel(httpExchange.getRequestChannel()));
} else {
result.setBody(null);
}
}
return result;
}
use of org.apache.camel.impl.DefaultAttachment in project camel by apache.
the class MessageWithAttachmentRedeliveryIssueTest method testMessageWithAttachmentRedeliveryIssue.
public void testMessageWithAttachmentRedeliveryIssue() throws Exception {
getMockEndpoint("mock:result").expectedMessageCount(1);
template.send("direct:start", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody("Hello World");
exchange.getIn().addAttachment("message1.xml", new DataHandler(new FileDataSource(new File("src/test/data/message1.xml"))));
exchange.getIn().addAttachmentObject("message2.xml", new DefaultAttachment(new FileDataSource(new File("src/test/data/message2.xml"))));
}
});
assertMockEndpointsSatisfied();
Message msg = getMockEndpoint("mock:result").getReceivedExchanges().get(0).getIn();
assertNotNull(msg);
assertEquals("Hello World", msg.getBody());
assertTrue(msg.hasAttachments());
}
Aggregations