Author: davsclaus
Date: Tue Aug 12 05:28:15 2008
New Revision: 685129
URL: http://svn.apache.org/viewvc?rev=685129&view=rev
Log:
CAMEL-812: camel mail headers take precedence over pre configuration. Also now
its possible to have multiple recipients in a single string with comma or semi
colon as separator char.
Added:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailHeaderOverrulePreConfigurationRecipientsTest.java
(with props)
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailMultipleRecipientsTest.java
(with props)
Modified:
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSubjectTest.java
Modified:
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java?rev=685129&r1=685128&r2=685129&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
(original)
+++
activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
Tue Aug 12 05:28:15 2008
@@ -63,24 +63,12 @@
public void populateMailMessage(MailEndpoint endpoint, MimeMessage
mimeMessage, Exchange exchange)
throws MessagingException, IOException {
- // append the headers from the in message at first
- appendHeadersFromCamel(mimeMessage, exchange, exchange.getIn());
-
- // than override any from the fixed endpoint configuraiton
- Map<Message.RecipientType, String> recipients =
endpoint.getConfiguration().getRecipients();
- if (recipients.containsKey(Message.RecipientType.TO)) {
- mimeMessage.setRecipients(Message.RecipientType.TO,
recipients.get(Message.RecipientType.TO));
- }
- if (recipients.containsKey(Message.RecipientType.CC)) {
- mimeMessage.setRecipients(Message.RecipientType.CC,
recipients.get(Message.RecipientType.CC));
- }
- if (recipients.containsKey(Message.RecipientType.BCC)) {
- mimeMessage.setRecipients(Message.RecipientType.BCC,
recipients.get(Message.RecipientType.BCC));
- }
-
- // fallback to use destination if no TO provided at all
- if (mimeMessage.getRecipients(Message.RecipientType.TO) == null) {
- mimeMessage.setRecipients(Message.RecipientType.TO,
endpoint.getConfiguration().getDestination());
+ // camel message headers takes presedence over endpoint configuration
+ if (hasRecipientHeaders(exchange.getIn())) {
+ setRecipientFromCamelMessage(mimeMessage, exchange,
exchange.getIn());
+ } else {
+ // fallback to endpoint configuration
+ setRecipientFromEndpointConfiguration(mimeMessage, endpoint);
}
// must have at least one recipients otherwise we do not know where to
send the mail
@@ -88,6 +76,9 @@
throw new IllegalArgumentException("The mail message does not have
any recipients set.");
}
+ // append the rest of the headers (no recipients) that could be
subject, reply-to etc.
+ appendHeadersFromCamelMessage(mimeMessage, exchange, exchange.getIn());
+
if (empty(mimeMessage.getFrom())) {
// lets default the address to the endpoint destination
String from = endpoint.getConfiguration().getFrom();
@@ -122,8 +113,8 @@
/**
* Appends the Mail headers from the Camel [EMAIL PROTECTED] MailMessage}
*/
- protected void appendHeadersFromCamel(MimeMessage mimeMessage, Exchange
exchange,
- org.apache.camel.Message
camelMessage)
+ protected void appendHeadersFromCamelMessage(MimeMessage mimeMessage,
Exchange exchange,
+ org.apache.camel.Message
camelMessage)
throws MessagingException {
for (Map.Entry<String, Object> entry :
camelMessage.getHeaders().entrySet()) {
@@ -132,6 +123,12 @@
if (headerValue != null) {
if (headerFilterStrategy != null
&&
!headerFilterStrategy.applyFilterToCamelHeaders(headerName, headerValue)) {
+
+ if (isRecipientHeader(headerName)) {
+ // skip any recipients as they are handled specially
+ continue;
+ }
+
// Mail messages can repeat the same header...
if (ObjectConverter.isCollection(headerValue)) {
Iterator iter = ObjectConverter.iterator(headerValue);
@@ -147,20 +144,50 @@
}
}
+ private void setRecipientFromCamelMessage(MimeMessage mimeMessage,
Exchange exchange,
+ org.apache.camel.Message
camelMessage)
+ throws MessagingException {
+
+ for (Map.Entry<String, Object> entry :
camelMessage.getHeaders().entrySet()) {
+ String headerName = entry.getKey();
+ Object headerValue = entry.getValue();
+ if (headerValue != null && isRecipientHeader(headerName)) {
+ // special handling of recipients
+ if (ObjectConverter.isCollection(headerValue)) {
+ Iterator iter = ObjectConverter.iterator(headerValue);
+ while (iter.hasNext()) {
+ Object recipient = iter.next();
+ appendRecipientToMimeMessage(mimeMessage, headerName,
asString(exchange, recipient));
+ }
+ } else {
+ appendRecipientToMimeMessage(mimeMessage, headerName,
asString(exchange, headerValue));
+ }
+ }
+ }
+ }
+
/**
- * Does the given camel message contain any To, CC or BCC header names?
+ * Appends the Mail headers from the endpoint configuraiton.
*/
- private static boolean hasRecipientHeaders(org.apache.camel.Message
camelMessage) {
- for (String key : camelMessage.getHeaders().keySet()) {
- if (Message.RecipientType.TO.toString().equals(key)) {
- return true;
- } else if (Message.RecipientType.CC.toString().equals(key)) {
- return true;
- } else if (Message.RecipientType.BCC.toString().equals(key)) {
- return true;
- }
+ protected void setRecipientFromEndpointConfiguration(MimeMessage
mimeMessage, MailEndpoint endpoint)
+ throws MessagingException {
+
+ Map<Message.RecipientType, String> recipients =
endpoint.getConfiguration().getRecipients();
+ if (recipients.containsKey(Message.RecipientType.TO)) {
+ appendRecipientToMimeMessage(mimeMessage,
Message.RecipientType.TO.toString(), recipients.get(Message.RecipientType.TO));
+ }
+ if (recipients.containsKey(Message.RecipientType.CC)) {
+ appendRecipientToMimeMessage(mimeMessage,
Message.RecipientType.CC.toString(), recipients.get(Message.RecipientType.CC));
+ }
+ if (recipients.containsKey(Message.RecipientType.BCC)) {
+ appendRecipientToMimeMessage(mimeMessage,
Message.RecipientType.BCC.toString(),
recipients.get(Message.RecipientType.BCC));
+ }
+
+ // fallback to use destination if no TO provided at all
+ String destination = endpoint.getConfiguration().getDestination();
+ if (destination != null &&
mimeMessage.getRecipients(Message.RecipientType.TO) == null) {
+ appendRecipientToMimeMessage(mimeMessage,
Message.RecipientType.TO.toString(), destination);
}
- return false;
}
/**
@@ -209,15 +236,7 @@
return true;
}
- private static boolean empty(Address[] addresses) {
- return addresses == null || addresses.length == 0;
- }
-
- private static String asString(Exchange exchange, Object value) {
- return
exchange.getContext().getTypeConverter().convertTo(String.class, value);
- }
-
- public Map<String, Object> extractHeadersFromMail(Message mailMessage)
throws MessagingException {
+ protected Map<String, Object> extractHeadersFromMail(Message mailMessage)
throws MessagingException {
Map<String, Object> answer = new HashMap<String, Object>();
Enumeration names = mailMessage.getAllHeaders();
@@ -238,4 +257,64 @@
return answer;
}
+ private static void appendRecipientToMimeMessage(MimeMessage mimeMessage,
String type, String recipient)
+ throws MessagingException {
+
+ // we support that multi recipient can be given as a string seperated
by comma or semi colon
+ String[] lines = recipient.split("[,|;]");
+ for (String line : lines) {
+ line = line.trim();
+ mimeMessage.addRecipients(asRecipientType(type), line);
+ }
+ }
+
+ /**
+ * Does the given camel message contain any To, CC or BCC header names?
+ */
+ private static boolean hasRecipientHeaders(org.apache.camel.Message
camelMessage) {
+ for (String key : camelMessage.getHeaders().keySet()) {
+ if (isRecipientHeader(key)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Is the given key a mime message recipient header (To, CC or BCC)
+ */
+ private static boolean isRecipientHeader(String key) {
+ if (Message.RecipientType.TO.toString().equalsIgnoreCase(key)) {
+ return true;
+ } else if (Message.RecipientType.CC.toString().equalsIgnoreCase(key)) {
+ return true;
+ } else if (Message.RecipientType.BCC.toString().equalsIgnoreCase(key))
{
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Returns the RecipientType object.
+ */
+ private static Message.RecipientType asRecipientType(String type) {
+ if (Message.RecipientType.TO.toString().equalsIgnoreCase(type)) {
+ return Message.RecipientType.TO;
+ } else if (Message.RecipientType.CC.toString().equalsIgnoreCase(type))
{
+ return Message.RecipientType.CC;
+ } else if
(Message.RecipientType.BCC.toString().equalsIgnoreCase(type)) {
+ return Message.RecipientType.BCC;
+ }
+ throw new IllegalArgumentException("Unknown recipient type: " + type);
+ }
+
+
+ private static boolean empty(Address[] addresses) {
+ return addresses == null || addresses.length == 0;
+ }
+
+ private static String asString(Exchange exchange, Object value) {
+ return
exchange.getContext().getTypeConverter().convertTo(String.class, value);
+ }
+
}
Added:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailHeaderOverrulePreConfigurationRecipientsTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailHeaderOverrulePreConfigurationRecipientsTest.java?rev=685129&view=auto
==============================================================================
---
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailHeaderOverrulePreConfigurationRecipientsTest.java
(added)
+++
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailHeaderOverrulePreConfigurationRecipientsTest.java
Tue Aug 12 05:28:15 2008
@@ -0,0 +1,60 @@
+/**
+ * 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 java.util.HashMap;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.jvnet.mock_javamail.Mailbox;
+
+/**
+ * Unit test to verify that message headers override pre configuration.
+ */
+public class MailHeaderOverrulePreConfigurationRecipientsTest extends
ContextTestSupport {
+
+ public void testSendWithRecipientsInHeaders() throws Exception {
+ Mailbox.clearAll();
+
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Hello World");
+ mock.expectedHeaderReceived("to", "[EMAIL PROTECTED]");
+ mock.expectedHeaderReceived("cc", "[EMAIL PROTECTED]");
+ mock.expectedHeaderReceived("bcc", "[EMAIL PROTECTED]");
+
+ Map<String, Object> headers = new HashMap<String, Object>();
+ headers.put("to", "[EMAIL PROTECTED]");
+ headers.put("cc", "[EMAIL PROTECTED]");
+ headers.put("bcc", "[EMAIL PROTECTED]");
+
+ template.sendBodyAndHeaders("smtp://[EMAIL PROTECTED]", "Hello World",
headers);
+
+ mock.assertIsSatisfied();
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from("pop3://[EMAIL PROTECTED]@outhere.com&[EMAIL
PROTECTED]&consumer.delay=1000").to("mock:result");
+ }
+ };
+ }
+
+}
Propchange:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailHeaderOverrulePreConfigurationRecipientsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailHeaderOverrulePreConfigurationRecipientsTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailMultipleRecipientsTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailMultipleRecipientsTest.java?rev=685129&view=auto
==============================================================================
---
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailMultipleRecipientsTest.java
(added)
+++
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailMultipleRecipientsTest.java
Tue Aug 12 05:28:15 2008
@@ -0,0 +1,85 @@
+/**
+ * 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 java.util.HashMap;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.jvnet.mock_javamail.Mailbox;
+
+/**
+ * Unit test to verift that we can have multiple recipients in To, CC and BCC
+ */
+public class MailMultipleRecipientsTest extends ContextTestSupport {
+
+ public void testSendWithMultipleRecipientsInHeader() throws Exception {
+ Mailbox.clearAll();
+
+ // START SNIPPET: e1
+ Map<String, Object> headers = new HashMap<String, Object>();
+ // test with both comma and semi colon as Camel supports both kind of
separators
+ headers.put("to", "[EMAIL PROTECTED], [EMAIL PROTECTED] ; [EMAIL
PROTECTED]");
+ headers.put("cc", "[EMAIL PROTECTED]");
+
+ assertMailbox("claus");
+ assertMailbox("willem");
+ assertMailbox("hadrian");
+
+ template.sendBodyAndHeaders("smtp://localhost", "Hello World",
headers);
+ // END SNIPPET: e1
+
+ assertMockEndpointsSatisifed();
+ }
+
+ public void testSendWithMultipleRecipientsPreConfigured() throws Exception
{
+ Mailbox.clearAll();
+
+ assertMailbox("claus");
+ assertMailbox("willem");
+
+ // START SNIPPET: e2
+ // here we have preconfigued the to recievs to claus and willem.
Notice we use comma to seperate
+ // the two recipeients. Camel also support using colon as seperator
char
+ template.sendBody("smtp://[EMAIL PROTECTED],[EMAIL PROTECTED]&[EMAIL
PROTECTED]", "Hello World");
+ // END SNIPPET: e2
+
+ assertMockEndpointsSatisifed();
+ }
+
+ private void assertMailbox(String name) throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:" + name);
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Hello World");
+ mock.expectedHeaderReceived("cc", "[EMAIL PROTECTED]");
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+ from("pop3://[EMAIL PROTECTED]").to("mock:claus");
+
+ from("pop3://[EMAIL PROTECTED]").to("mock:willem");
+
+ from("pop3://[EMAIL PROTECTED]").to("mock:hadrian");
+ }
+ };
+ }
+
+}
Propchange:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailMultipleRecipientsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailMultipleRecipientsTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java?rev=685129&r1=685128&r2=685129&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
(original)
+++
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRouteTest.java
Tue Aug 12 05:28:15 2008
@@ -21,6 +21,7 @@
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
+import javax.mail.Address;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
@@ -68,7 +69,13 @@
assertNotNull(name + " should have received at least one mail!",
message);
assertEquals("hello world!", message.getContent());
assertEquals("[EMAIL PROTECTED]", message.getFrom()[0].toString());
- assertEquals(name,
message.getRecipients(RecipientType.TO)[0].toString());
+ boolean found = false;
+ for (Address adr : message.getRecipients(RecipientType.TO)) {
+ if (name.equals(adr.toString())) {
+ found = true;
+ }
+ }
+ assertTrue("Should have found the recpient to in the mail: " + name,
found);
}
@Override
@@ -80,9 +87,11 @@
// must use fixed to option to send the mail to the given
reciever, as we have polled
// a mail from a mailbox where it already has the 'old' To as
header value
+ // here we send the mail to 2 recievers. notice we can use a
plain string with semi colon
+ // to seperate the mail addresses
from("direct:a")
- .to("smtp://[EMAIL PROTECTED]",
- "smtp://[EMAIL PROTECTED]");
+ .setHeader("to", constant("[EMAIL PROTECTED]; [EMAIL
PROTECTED]"))
+ .to("smtp://localhost");
from("pop3://[EMAIL PROTECTED]")
.convertBodyTo(String.class).to("mock:result");
Modified:
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSubjectTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSubjectTest.java?rev=685129&r1=685128&r2=685129&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSubjectTest.java
(original)
+++
activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSubjectTest.java
Tue Aug 12 05:28:15 2008
@@ -31,12 +31,14 @@
Mailbox.clearAll();
String body = "Hello Claus.\nYes it does.\n\nRegards James.";
- template.sendBody("direct:a", body);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.expectedHeaderReceived("subject", subject);
mock.expectedBodiesReceived(body);
+
+ template.sendBody("direct:a", body);
+
mock.assertIsSatisfied();
}