camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin Jackson <foamd...@gmail.com>
Subject Testing consuming mail with attachments
Date Thu, 11 Mar 2010 08:21:50 GMT
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

Mime
View raw message