On Thu, Mar 11, 2010 at 9:21 AM, Kevin Jackson <[email protected]> wrote:
> Hi,
>
> I have a requirement to take an email, consume it extract the
> attachment, then process the contents of the attachment.
>
> I have the processing completed, but I cannot find an elegant method
> of testing the mail attachment handling.  The documentation seems to
> require that my processing is coupled to Camel, I would prefer to use
> a simple bean rather than a Processor implementation.
>
> The canonical example of handling mail attachments is similar to :
>
> 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();
>            }
>        }
>    }
>
> Unfortunately this means that my mail processing is camel specific -
> I'd rather be able to handle MimeMessage instead of Exchange and be
> completely free of a camel dependency so that I can test the handling
> of mail independently.  However other code for handling mail
> attachments seems to be needlessly complex.  Right now i have a choice
> between short relatively simple code that cannot be tested to my
> satisfaction, or writing much more complex code that I can test
> without tight coupling to the camel code.  Neither solution smells
> right and neither is particularly nice.
>
> Has anyone else come across this problem?
>
> Thanks,
> Kev
>

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?




-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Reply via email to