Repository: camel
Updated Branches:
  refs/heads/camel-2.15.x 14ec99f52 -> b03a51c95


CAMEL-8163 - socketFactory must also be set in MailConfiguration when STARTTLS 
is used


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/79c6e28f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/79c6e28f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/79c6e28f

Branch: refs/heads/camel-2.15.x
Commit: 79c6e28f47d6e457fc54d7f168115304c56f93c7
Parents: 14ec99f
Author: lburgazzoli <lburgazz...@gmail.com>
Authored: Wed Feb 24 13:15:35 2016 +0100
Committer: Andrea Cosentino <anco...@gmail.com>
Committed: Wed Feb 24 14:19:32 2016 +0100

----------------------------------------------------------------------
 .../camel/component/mail/MailConfiguration.java |  16 ++-
 .../component/mail/MailEndpointTlsTest.java     | 140 +++++++++++++++++++
 .../camel/component/mail/MailTestHelper.java    |  48 +++++++
 .../SslContextParametersMailRouteTest.java      |  27 +---
 4 files changed, 205 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/79c6e28f/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
 
b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
index 638e603..da49b8e 100644
--- 
a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
+++ 
b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
@@ -30,6 +30,7 @@ import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 import org.apache.camel.spi.UriPath;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.jsse.SSLContextParameters;
 
 /**
@@ -227,7 +228,7 @@ public class MailConfiguration implements Cloneable {
             properties.put("javax.net.debug", "all");
         }
 
-        if (sslContextParameters != null && isSecureProtocol()) {
+        if (sslContextParameters != null && (isSecureProtocol() || 
isStartTlsEnabled())) {
             SSLContext sslContext;
             try {
                 sslContext = sslContextParameters.createSSLContext();
@@ -238,7 +239,7 @@ public class MailConfiguration implements Cloneable {
             properties.put("mail." + protocol + ".socketFactory.fallback", 
"false");
             properties.put("mail." + protocol + ".socketFactory.port", "" + 
port);
         }
-        if (dummyTrustManager && isSecureProtocol()) {
+        if (dummyTrustManager && (isSecureProtocol() || isStartTlsEnabled())) {
             // set the custom SSL properties
             properties.put("mail." + protocol + ".socketFactory.class", 
"org.apache.camel.component.mail.security.DummySSLSocketFactory");
             properties.put("mail." + protocol + ".socketFactory.fallback", 
"false");
@@ -256,6 +257,17 @@ public class MailConfiguration implements Cloneable {
                || this.protocol.equalsIgnoreCase("imaps");
     }
 
+    public boolean isStartTlsEnabled() {
+        if (additionalJavaMailProperties != null) {
+            return ObjectHelper.equal(
+                additionalJavaMailProperties.getProperty("mail." + protocol + 
".starttls.enable"),
+                "true",
+                true);
+        }
+
+        return false;
+    }
+
     public String getMailStoreLogInformation() {
         String ssl = "";
         if (isSecureProtocol()) {

http://git-wip-us.apache.org/repos/asf/camel/blob/79c6e28f/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailEndpointTlsTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailEndpointTlsTest.java
 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailEndpointTlsTest.java
new file mode 100644
index 0000000..fcd70e1
--- /dev/null
+++ 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailEndpointTlsTest.java
@@ -0,0 +1,140 @@
+/**
+ * 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.Arrays;
+import java.util.Collection;
+import java.util.Properties;
+
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class MailEndpointTlsTest extends CamelTestSupport {
+
+    private final String protocol;
+
+    public MailEndpointTlsTest(String protocol) {
+        this.protocol = protocol;
+    }
+
+    @Parameterized.Parameters
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+            {"smtp"},
+            {"smtps"},
+            {"pop3"},
+            {"pop3s"},
+            {"imap"},
+            {"imaps"}
+        });
+    }
+
+    @Test
+    public void testMailEndpointTslConfig() throws Exception {
+        Properties properties = new Properties();
+        properties.setProperty("mail." + protocol + ".starttls.enable", 
"true");
+
+        MailConfiguration cfg = new MailConfiguration();
+        cfg.setPort(21);
+        cfg.setProtocol(protocol);
+        cfg.setHost("myhost");
+        cfg.setUsername("james");
+        cfg.setPassword("secret");
+        cfg.setAdditionalJavaMailProperties(properties);
+
+        assertTrue(cfg.isStartTlsEnabled());
+
+        Properties javaMailProperties = 
cfg.createJavaMailSender().getJavaMailProperties();
+        assertNull(javaMailProperties.get("mail." + protocol + 
".socketFactory"));
+        assertNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.fallback"));
+        assertNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.port"));
+    }
+
+    @Test
+    public void testMailEndpointNoTslConfig() throws Exception {
+        MailConfiguration cfg = new MailConfiguration();
+        cfg.setPort(21);
+        cfg.setProtocol(protocol);
+        cfg.setHost("myhost");
+        cfg.setUsername("james");
+        cfg.setPassword("secret");
+        
cfg.setSslContextParameters(MailTestHelper.createSslContextParameters());
+
+        Properties javaMailProperties = 
cfg.createJavaMailSender().getJavaMailProperties();
+
+        assertFalse(cfg.isStartTlsEnabled());
+
+        if (protocol.endsWith("s")) {
+            assertTrue(cfg.isSecureProtocol());
+            assertNotNull(javaMailProperties.get("mail." + protocol + 
".socketFactory"));
+            assertNotNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.fallback"));
+            assertNotNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.port"));
+        } else {
+            assertFalse(cfg.isSecureProtocol());
+            assertNull(javaMailProperties.get("mail." + protocol + 
".socketFactory"));
+            assertNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.fallback"));
+            assertNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.port"));
+        }
+    }
+
+    @Test
+    public void testMailEndpointTslSslContextParametersConfig() throws 
Exception {
+        Properties properties = new Properties();
+        properties.setProperty("mail." + protocol + ".starttls.enable", 
"true");
+
+        MailConfiguration cfg = new MailConfiguration();
+        cfg.setPort(21);
+        cfg.setProtocol(protocol);
+        cfg.setHost("myhost");
+        cfg.setUsername("james");
+        cfg.setPassword("secret");
+        
cfg.setSslContextParameters(MailTestHelper.createSslContextParameters());
+        cfg.setAdditionalJavaMailProperties(properties);
+
+        assertTrue(cfg.isStartTlsEnabled());
+
+        Properties javaMailProperties = 
cfg.createJavaMailSender().getJavaMailProperties();
+        assertNotNull(javaMailProperties.get("mail." + protocol + 
".socketFactory"));
+        assertNotNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.fallback"));
+        assertNotNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.port"));
+    }
+
+    @Test
+    public void testMailEndpointTslDummyTrustManagerConfig() throws Exception {
+        Properties properties = new Properties();
+        properties.setProperty("mail." + protocol + ".starttls.enable", 
"true");
+
+        MailConfiguration cfg = new MailConfiguration();
+        cfg.setPort(21);
+        cfg.setProtocol(protocol);
+        cfg.setHost("myhost");
+        cfg.setUsername("james");
+        cfg.setPassword("secret");
+        cfg.setDummyTrustManager(true);
+        cfg.setAdditionalJavaMailProperties(properties);
+
+        assertTrue(cfg.isStartTlsEnabled());
+
+        Properties javaMailProperties = 
cfg.createJavaMailSender().getJavaMailProperties();
+        assertNotNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.class"));
+        assertNotNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.fallback"));
+        assertNotNull(javaMailProperties.get("mail." + protocol + 
".socketFactory.port"));
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/79c6e28f/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailTestHelper.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailTestHelper.java
 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailTestHelper.java
new file mode 100644
index 0000000..e454b0c
--- /dev/null
+++ 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailTestHelper.java
@@ -0,0 +1,48 @@
+/**
+ * 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 org.apache.camel.util.jsse.KeyManagersParameters;
+import org.apache.camel.util.jsse.KeyStoreParameters;
+import org.apache.camel.util.jsse.SSLContextParameters;
+import org.apache.camel.util.jsse.TrustManagersParameters;
+
+public final class MailTestHelper {
+    private static final String KEY_STORE_PASSWORD = "changeit";
+
+    private MailTestHelper() {
+    }
+
+    public static SSLContextParameters createSslContextParameters() {
+        KeyStoreParameters ksp = new KeyStoreParameters();
+        
ksp.setResource(MailTestHelper.class.getClassLoader().getResource("jsse/localhost.ks").toString());
+        ksp.setPassword(KEY_STORE_PASSWORD);
+
+        KeyManagersParameters kmp = new KeyManagersParameters();
+        kmp.setKeyPassword(KEY_STORE_PASSWORD);
+        kmp.setKeyStore(ksp);
+
+        TrustManagersParameters tmp = new TrustManagersParameters();
+        tmp.setKeyStore(ksp);
+
+        SSLContextParameters sslContextParameters = new SSLContextParameters();
+        sslContextParameters.setKeyManagers(kmp);
+        sslContextParameters.setTrustManagers(tmp);
+
+        return sslContextParameters;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/79c6e28f/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java
 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java
index 39633a2..e394bff 100644
--- 
a/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java
+++ 
b/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java
@@ -18,18 +18,14 @@ package org.apache.camel.component.mail.security;
 
 import java.util.HashMap;
 import java.util.Map;
-
 import javax.net.ssl.SSLHandshakeException;
 
 import org.apache.camel.CamelExecutionException;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mail.MailTestHelper;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.test.junit4.CamelTestSupport;
-import org.apache.camel.util.jsse.KeyManagersParameters;
-import org.apache.camel.util.jsse.KeyStoreParameters;
-import org.apache.camel.util.jsse.SSLContextParameters;
-import org.apache.camel.util.jsse.TrustManagersParameters;
 import org.junit.Ignore;
 import org.junit.Test;
 
@@ -40,9 +36,7 @@ import org.junit.Test;
  */
 @Ignore
 public class SslContextParametersMailRouteTest extends CamelTestSupport {
-    
-    protected static final String KEY_STORE_PASSWORD = "changeit";
-    
+
     private String email = "usern...@gmail.com";
     private String username = "usern...@gmail.com";
     private String imapHost = "imap.gmail.com";
@@ -120,22 +114,7 @@ public class SslContextParametersMailRouteTest extends 
CamelTestSupport {
     }
     
     protected void addSslContextParametersToRegistry(JndiRegistry registry) {
-        KeyStoreParameters ksp = new KeyStoreParameters();
-        
ksp.setResource(this.getClass().getClassLoader().getResource("jsse/localhost.ks").toString());
-        ksp.setPassword(KEY_STORE_PASSWORD);
-
-        KeyManagersParameters kmp = new KeyManagersParameters();
-        kmp.setKeyPassword(KEY_STORE_PASSWORD);
-        kmp.setKeyStore(ksp);
-
-        TrustManagersParameters tmp = new TrustManagersParameters();
-        tmp.setKeyStore(ksp);
-        
-        SSLContextParameters sslContextParameters = new SSLContextParameters();
-        sslContextParameters.setKeyManagers(kmp);
-        sslContextParameters.setTrustManagers(tmp);
-
-        registry.bind("sslContextParameters", sslContextParameters);
+        registry.bind("sslContextParameters", 
MailTestHelper.createSslContextParameters());
     }
     
     /**

Reply via email to