camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r800779 - in /camel/trunk/components/camel-mail/src: main/java/org/apache/camel/component/mail/ test/java/org/apache/camel/component/mail/
Date Tue, 04 Aug 2009 13:41:10 GMT
Author: davsclaus
Date: Tue Aug  4 13:41:10 2009
New Revision: 800779

URL: http://svn.apache.org/viewvc?rev=800779&view=rev
Log:
CAMEL-1869: added option ignoreUnsupportedCharset to allow sending mails even if a bad charset
has been specified that the local JVM does not support.

Added:
    camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConsumerUnsupportedCharsetTest.java
  (with props)
    camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java
  (with props)
Modified:
    camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java
    camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.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=800779&r1=800778&r2=800779&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
Tue Aug  4 13:41:10 2009
@@ -17,6 +17,8 @@
 package org.apache.camel.component.mail;
 
 import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -117,14 +119,54 @@
         if (contentType != null && contentType.contains(";")) {
             String before = ObjectHelper.before(contentType, ";");
             String after = ObjectHelper.after(contentType, ";");
-            if (before != null && after != null) {
-                contentType = before.trim() + "; " + after.trim();
+
+            // after is the charset lets see if its given and a valid charset
+            if (after != null) {
+                String charset = ObjectHelper.after(after, "=");
+                charset = determineCharSet(configuration, charset);
+                if (charset != null) {
+                    after = "charset=" + charset;
+                } else {
+                    after = null;
+                }
+            }
+
+            if (before != null && after == null) {
+                contentType = before.trim();
+            } else if (before != null && after != null) {
+                contentType = before.trim() + "; " + after;
             }
         }
 
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Determined Content-Type: " + contentType);
+        }
+
         return contentType;
     }
 
+    protected String determineCharSet(MailConfiguration configuration, String charset) {
+        if (charset == null) {
+            return null;
+        }
+        
+        boolean supported;
+        try {
+            supported = Charset.isSupported(charset);
+        } catch (IllegalCharsetNameException e) {
+            supported = false;
+        }
+
+        if (supported) {
+            return charset;
+        } else if (configuration.isIgnoreUnsupportedCharset()) {
+            LOG.warn("Charset: " + charset + " is not supported, will fallback to use platform
default instead.");
+            return null;
+        }
+
+        return charset;
+    }
+
     protected String populateContentOnMimeMessage(MimeMessage part, MailConfiguration configuration,
Exchange exchange)
         throws MessagingException, IOException {
 

Modified: camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java?rev=800779&r1=800778&r2=800779&view=diff
==============================================================================
--- camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
(original)
+++ camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
Tue Aug  4 13:41:10 2009
@@ -59,6 +59,7 @@
     private String contentType = "text/plain";
     private String alternativeBodyHeader = MailConstants.MAIL_ALTERNATIVE_BODY;
     private boolean useInlineAttachments;
+    private boolean ignoreUnsupportedCharset;
 
     public MailConfiguration() {
     }
@@ -433,4 +434,12 @@
     public void setUseInlineAttachments(boolean useInlineAttachments) {
         this.useInlineAttachments = useInlineAttachments;
     }
+
+    public boolean isIgnoreUnsupportedCharset() {
+        return ignoreUnsupportedCharset;
+    }
+
+    public void setIgnoreUnsupportedCharset(boolean ignoreUnsupportedCharset) {
+        this.ignoreUnsupportedCharset = ignoreUnsupportedCharset;
+    }
 }

Added: camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConsumerUnsupportedCharsetTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConsumerUnsupportedCharsetTest.java?rev=800779&view=auto
==============================================================================
--- camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConsumerUnsupportedCharsetTest.java
(added)
+++ camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConsumerUnsupportedCharsetTest.java
Tue Aug  4 13:41:10 2009
@@ -0,0 +1,71 @@
+/**
+ * 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.mail.Folder;
+import javax.mail.Message;
+import javax.mail.Store;
+import javax.mail.internet.MimeMessage;
+
+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;
+import org.springframework.mail.javamail.JavaMailSenderImpl;
+
+/**
+ * @version $Revision$
+ */
+public class MailConsumerUnsupportedCharsetTest extends CamelTestSupport {
+
+    @Test
+    public void testConsumeUnsupportedCharset() throws Exception {
+        prepareMailbox();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Bye World");
+
+        mock.assertIsSatisfied();
+    }
+
+    private void prepareMailbox() throws Exception {
+        // connect to mailbox
+        Mailbox.clearAll();
+        JavaMailSenderImpl sender = new JavaMailSenderImpl();
+        Store store = sender.getSession().getStore("imap");
+        store.connect("localhost", 25, "jones", "secret");
+        Folder folder = store.getFolder("INBOX");
+        folder.open(Folder.READ_WRITE);
+        folder.expunge();
+
+        Message[] msg = new Message[1];
+        MimeMessage mime = new MimeMessage(sender.getSession());
+        mime.setContent("Bye World", "text/plain; charset=ThisIsNotAKnownCharset");
+        msg[0] = mime;
+        folder.appendMessages(msg);
+        folder.close(true);
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("pop3://jones@localhost?password=secret&consumer.delay=1000&ignoreUnsupportedCharset=true").to("mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConsumerUnsupportedCharsetTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailConsumerUnsupportedCharsetTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java?rev=800779&view=auto
==============================================================================
--- camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java
(added)
+++ camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java
Tue Aug  4 13:41:10 2009
@@ -0,0 +1,102 @@
+/**
+ * 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.io.UnsupportedEncodingException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.RuntimeCamelException;
+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.springframework.mail.MailPreparationException;
+
+/**
+ * @version $Revision$
+ */
+public class MailProducerUnsupportedCharsetTest extends CamelTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testSencUnsupportedCharset() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("pop3://jones@localhost?password=secret&delay=1000&ignoreUnsupportedCharset=true").to("mock:result");
+            }
+        });
+        context.start();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World", "Bye World");
+        mock.allMessages().header("Content-Type").isEqualTo("text/plain");
+
+        Map headers = new HashMap();
+        headers.put("To", "jones@localhost");
+        headers.put("Content-Type", "text/plain");
+        template.sendBodyAndHeaders("smtp://localhost?ignoreUnsupportedCharset=true", "Hello
World", headers);
+
+        headers.clear();
+        headers.put("To", "jones@localhost");
+        headers.put("Content-Type", "text/plain; charset=ansi_x3.110-1983");
+        template.sendBodyAndHeaders("smtp://localhost?ignoreUnsupportedCharset=true", "Bye
World", headers);
+
+        mock.assertIsSatisfied();
+    }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testSencUnsupportedCharsetDisabledOption() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("pop3://jones@localhost?password=secret&delay=1000&ignoreUnsupportedCharset=false").to("mock:result");
+            }
+        });
+        context.start();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        mock.allMessages().header("Content-Type").isEqualTo("text/plain");
+
+        Map headers = new HashMap();
+        headers.put("To", "jones@localhost");
+        headers.put("Content-Type", "text/plain");
+        template.sendBodyAndHeaders("smtp://localhost?ignoreUnsupportedCharset=false", "Hello
World", headers);
+
+        headers.clear();
+        headers.put("To", "jones@localhost");
+        headers.put("Content-Type", "text/plain; charset=XXX");
+        try {
+            template.sendBodyAndHeaders("smtp://localhost?ignoreUnsupportedCharset=false",
"Bye World", headers);
+            fail("Should have thrown an exception");
+        } catch (RuntimeCamelException e) {
+            assertIsInstanceOf(MailPreparationException.class, e.getCause());
+            assertIsInstanceOf(UnsupportedEncodingException.class, e.getCause().getCause());
+        }
+
+        mock.assertIsSatisfied();
+    }
+
+}

Propchange: camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message