Friday, April 27, 2012

Camel: Working with Email Attachments

If your using the Camel-Mail component to handle some business logic that involves receiving email that contains attachments, then you might be interested in how these email attachments can be split into separate messages so they can be processed individually. This post will demonstrate how this can be done using a Camel Expression and a JUnit test that demonstrates this behavior.

/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.mail;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.activation.DataHandler;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.support.ExpressionAdapter;
/**
* A {@link org.apache.camel.Expression} which can be used to split a {@link MailMessage}
* per attachment. For example if a mail message has 5 attachments, then this
* expression will return a List<Message> that contains 5 {@link Message}
* and each have a single attachment from the source {@link MailMessage}.
*/
public class SplitAttachmentsExpression extends ExpressionAdapter {
@Override
public Object evaluate(Exchange exchange) {
// must use getAttachments to ensure attachments is initial populated
if (exchange.getIn().getAttachments().isEmpty()) {
return null;
}
// we want to provide a list of messages with 1 attachment per mail
List<Message> answer = new ArrayList<Message>();
for (Map.Entry<String, DataHandler> entry : exchange.getIn().getAttachments().entrySet()) {
final Message copy = exchange.getIn().copy();
copy.getAttachments().clear();
copy.getAttachments().put(entry.getKey(), entry.getValue());
answer.add(copy);
}
return answer;
}
}

From the above code you can see the Expression splits the exchange into separate messages, each containing one attachment, stored in a List object which is then returned to the Camel runtime which can then be used to iterate through the messages. For more information on how the splitter EIP works see the Camel Splitter EIP documentation.

Now we can test this Expression by using the following JUnit test case and verify that the attachments are indeed split into separate messages for processing:

In the route you can see the spilt is using the AttachmentsExpression which was shown above. In addition, I am using a simple processor to set the header of the exchange which contains the name of the attachment. Then, using the CBR (content base router) the exchange will be routed to an endpoint based on the attached file. The test case uses two mock endpoints which are used to validate the body of the message, number of attachments, attachment name, and attachment type.

Setting a up new maven project to test this out for yourself is very easy. Simply create a new maven project using one of the available Camel maven archetypes, the camel-archetype-java should work just fine for this. Once you have the project created you just need to copy the above classes into the project.

In addition to setting up the code you will also need to ensure the following dependencies are included in the project's pom.xml:

Note for the above test case I had my own class that implemented the SplitAttachmentExpression, as I was doing this before the class was added into the code base. If you are using the latest 2.10 the SplitAttachmentExpression import should be changed to org.apache.camel.component.mail.SplitAttachmentExpression. If you are using an earlier version, simply create the class as I did using the code from above.