Author: hadrian
Date: Thu Feb 24 03:37:59 2011
New Revision: 1074033
URL: http://svn.apache.org/viewvc?rev=1074033&view=rev
Log:
CAMEL-3713. Fix for duplicate mail attachments. Thanks to Paul for providing
the patch.
Added:
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentDuplicateNamesTest.java
Modified:
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
Modified:
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java?rev=1074033&r1=1074032&r2=1074033&view=diff
==============================================================================
---
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
(original)
+++
camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
Thu Feb 24 03:37:59 2011
@@ -314,8 +314,13 @@ public class MailBinding {
String fileName = part.getFileName();
if (fileName != null) {
LOG.debug("Mail contains file attachment: " +
fileName);
- // Parts marked with a disposition of Part.ATTACHMENT
are clearly attachments
- CollectionHelper.appendValue(map, fileName,
part.getDataHandler());
+ if (!map.containsKey(fileName)) {
+ // Parts marked with a disposition of
Part.ATTACHMENT are clearly attachments
+ map.put(fileName, part.getDataHandler());
+ }
+ else {
+ LOG.warn("Cannot extract duplicate attachment: " +
fileName);
+ }
}
}
}
Added:
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentDuplicateNamesTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentDuplicateNamesTest.java?rev=1074033&view=auto
==============================================================================
---
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentDuplicateNamesTest.java
(added)
+++
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentDuplicateNamesTest.java
Thu Feb 24 03:37:59 2011
@@ -0,0 +1,96 @@
+/**
+ * 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.Map;
+
+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;
+
+/**
+ * Unit test for Camel attachments and Mail attachments.
+ */
+public class MailAttachmentDuplicateNamesTest extends CamelTestSupport {
+
+ @Test
+ public void testSendAndRecieveMailWithAttachmentsWithDuplicateNames()
throws Exception {
+ // clear mailbox
+ Mailbox.clearAll();
+
+ // 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")));
+ 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();
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+
from("pop3://[email protected]?password=secret&consumer.delay=1000").to("mock:result");
+ }
+ };
+ }
+}