Hi,
>> public void process(Exchange ex) throws Exception {
>> Map<String, DataHandler> attachments = ex.getIn().getAttachments();
>> if(attachments.size() > 0) {
>> //loop here but really only expect a single attachment
>> for(String name : attachments.keySet()) {
>> DataHandler dh = attachments.get(name);
>>
>> String filename = dh.getName();
>> byte[] data =
>> ex.getContext().getTypeConverter().convertTo(byte[].class,
>> dh.getInputStream());
>> FileOutputStream fos = new
>> FileOutputStream(getAttachmentPath()+filename);
>> fos.write(data);
>> fos.flush();
>> fos.close();
>> }
>> }
>> }
>>
> The bean parameter binding is what you would normally use to decouple
> a POJO from Camel Exchange.
> http://camel.apache.org/bean-binding.html
> http://camel.apache.org/parameter-binding-annotations.html
>
>
> But I can see it lacks a feature to binding attachements, so you can
> do something like
>
> public void foo(@Attachments Map<String, DataHandler> myAttachments) {
> ..
> }
>
> But it could probably also be done without the @Attachments annotation
> as it can detect the types in the Map and see if its <String,
> DataHandler> and then map that automatic to the attachments of the
> Exchange. Anyone see a problem with this?
I'm not sure I follow you here.
I can use bean-binding annotations to mark a method in my pojo as a
@Handler method, does this negate the need to use the marker interface
Processor?
The method signature suggests that the POJO would process Map<String
DataHandler>, this would mean that some pre-processing would be
required upstream to take the mime message and convert it into this
format?
If this is the case, then my route would become something like :
from("direct:start").bean(MailProc.class).to("mock:result"), where MailProc is :
@Handler
public void proc(Map<String, DataHandler> attachments) throws Exception {
}
But when I kick this test off, I want to be using a programatically
created MimeMessage, so will that get converted from MimeMessage into
Map<String, DataHandler> attachments automatically due it being the
body of the Exchange?
In the camel mail unit tests:
@Test
public void testSendAndRecieveMailWithAttachments() throws Exception {
// START SNIPPET: e1
// create an exchange with a normal body and attachment to be
produced as email
Endpoint endpoint =
context.getEndpoint("smtp://[email protected]?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");
in.addAttachment("logo.jpeg", new DataHandler(new
FileDataSource("src/test/data/logo.jpeg")));
// 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, DataHandler> attachments = out.getIn().getAttachments();
assertNotNull("Should have attachments", attachments);
assertEquals(1, attachments.size());
DataHandler handler = out.getIn().getAttachment("logo.jpeg");
assertNotNull("The logo should be there", handler);
assertEquals("image/jpeg; name=logo.jpeg", handler.getContentType());
producer.stop();
}
This relies on Camel test classes and I would just like to be able to
create a new MimeMessage and use direct:start to
template.sendBody(attachmentEmail) rather than rely on manually
creating Exchanges etc
Sorry to bother you but this is an interesting investigation from my side
Kev