Author: davsclaus Date: Tue Apr 10 12:56:54 2012 New Revision: 1311700 URL: http://svn.apache.org/viewvc?rev=1311700&view=rev Log: Added example based on user forum issue
Added: camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SplitAttachmentsExpression.java camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java Added: camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SplitAttachmentsExpression.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SplitAttachmentsExpression.java?rev=1311700&view=auto ============================================================================== --- camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SplitAttachmentsExpression.java (added) +++ camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/SplitAttachmentsExpression.java Tue Apr 10 12:56:54 2012 @@ -0,0 +1,55 @@ +/** + * 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 <tt>List<Message></tt> 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; + } +} Added: camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java?rev=1311700&view=auto ============================================================================== --- camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java (added) +++ camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java Tue Apr 10 12:56:54 2012 @@ -0,0 +1,91 @@ +/** + * 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 javax.activation.DataHandler; +import javax.activation.FileDataSource; + +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Producer; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; +import org.jvnet.mock_javamail.Mailbox; + +/** + * + */ +public class MailSplitAttachmentsTest extends CamelTestSupport { + + @Test + public void testSplitAttachments() throws Exception { + + // clear mailbox + Mailbox.clearAll(); + + // create an exchange with a normal body and attachment to be produced as email + Endpoint endpoint = context.getEndpoint("smtp://ja...@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"); + in.addAttachment("logo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg"))); + in.addAttachment("license.txt", new DataHandler(new FileDataSource("src/main/resources/META-INF/LICENSE.txt"))); + + Producer producer = endpoint.createProducer(); + producer.start(); + producer.process(exchange); + + Thread.sleep(2000); + + MockEndpoint mock = getMockEndpoint("mock:split"); + mock.expectedMessageCount(2); + + mock.assertIsSatisfied(); + + Message first = mock.getReceivedExchanges().get(0).getIn(); + Message second = mock.getReceivedExchanges().get(1).getIn(); + + assertEquals(1, first.getAttachments().size()); + assertEquals("logo.jpeg", first.getAttachments().keySet().iterator().next()); + + assertEquals(1, second.getAttachments().size()); + assertEquals("license.txt", second.getAttachments().keySet().iterator().next()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // START SNIPPET: e1 + from("pop3://ja...@mymailserver.com?password=secret&consumer.delay=1000") + .to("log:email") + // use the SplitAttachmentsExpression which will split the message per attachment + .split(new SplitAttachmentsExpression()) + // each message going to this mock has a single attachment + .to("mock:split") + .end(); + // END SNIPPET: e1 + } + }; + } +}