Author: dvaleri
Date: Mon Mar  5 15:49:58 2012
New Revision: 1297097

URL: http://svn.apache.org/viewvc?rev=1297097&view=rev
Log:
[CAMEL-4665] Added support for JSSE Configuration Utility to Camel Mail 
component.

Added:
    
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java
   (with props)
    camel/trunk/components/camel-mail/src/test/resources/jsse/
    camel/trunk/components/camel-mail/src/test/resources/jsse/localhost.ks
Modified:
    
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/MailConfiguration.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java?rev=1297097&r1=1297096&r2=1297097&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
 Mon Mar  5 15:49:58 2012
@@ -181,6 +181,8 @@ public class MailConfiguration implement
                 throw new RuntimeCamelException("Error initializing 
SSLContext.", e);
             }
             properties.put("mail." + protocol + ".socketFactory", 
sslContext.getSocketFactory());
+            properties.put("mail." + protocol + ".socketFactory.fallback", 
"false");
+            properties.put("mail." + protocol + ".socketFactory.port", "" + 
port);
         }
         if (dummyTrustManager && isSecureProtocol()) {
             // set the custom SSL properties

Added: 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java?rev=1297097&view=auto
==============================================================================
--- 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java
 (added)
+++ 
camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/security/SslContextParametersMailRouteTest.java
 Mon Mar  5 15:49:58 2012
@@ -0,0 +1,146 @@
+/**
+ * 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.security;
+
+import java.util.HashMap;
+
+import javax.net.ssl.SSLHandshakeException;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+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;
+
+/**
+ * Test of integration between the mail component and JSSE Configuration 
Utility.
+ * This test does not easily automate.  This test is therefore ignored and the
+ * source is maintained here for easier development in the future.
+ */
+@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";
+    private String smtpHost = "smtp.gmail.com";
+    private String password = "PASSWORD";
+    
+    @Test
+    public void testSendAndReceiveMails() throws Exception {
+        
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                
+                from("imaps://" + imapHost + "?username=" + username + 
"&password=" + password
+                        + 
"&delete=false&unseen=true&fetchSize=1&consumer.useFixedDelay=true&consumer.delay=1000")
+                     .to("mock:in");
+                
+                from("direct:in")
+                    .to("smtps://" + smtpHost + "?username=" + username + 
"&password=" + password);
+            }
+        });
+        
+        context.start();
+
+        MockEndpoint resultEndpoint = getMockEndpoint("mock:in");
+        resultEndpoint.expectedBodiesReceived("Test Email Body\r\n");
+
+        HashMap<String, Object> headers = new HashMap<String, Object>();
+        headers.put("To", email);
+        headers.put("From", email);
+        headers.put("Reply-to", email);
+        headers.put("Subject", "SSL/TLS Test");
+        
+        template.sendBodyAndHeaders("direct:in", "Test Email Body", headers);
+
+        resultEndpoint.assertIsSatisfied();
+    }
+    
+    @Test
+    public void testSendAndReceiveMailsWithCustomTrustStore() throws Exception 
{
+        
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                
+                from("direct:in")
+                    .to("smtps://" + smtpHost + "?username=" + username + 
"&password=" + password
+                        + "&sslContextParameters=#sslContextParameters");
+            }
+        });
+        
+        context.start();
+
+        HashMap<String, Object> headers = new HashMap<String, Object>();
+        headers.put("To", email);
+        headers.put("From", email);
+        headers.put("Reply-to", email);
+        headers.put("Subject", "SSL/TLS Test");
+        
+        try {
+            template.sendBodyAndHeaders("direct:in", "Test Email Body", 
headers);
+        } catch (CamelExecutionException e) {
+            assertTrue(e.getCause().getCause() instanceof 
SSLHandshakeException);
+            assertTrue(e.getCause().getCause().getMessage().contains(
+                    "unable to find valid certification path to requested 
target"));
+        }
+    }
+    
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry reg = super.createRegistry();
+        
+        addSslContextParametersToRegistry(reg);
+    
+        return reg;
+    }
+    
+    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);
+    }
+    
+    /**
+     * Stop Camel startup.
+     */
+    @Override
+    public boolean isUseAdviceWith() {
+        return true;
+    }
+}

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

Added: camel/trunk/components/camel-mail/src/test/resources/jsse/localhost.ks
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mail/src/test/resources/jsse/localhost.ks?rev=1297097&view=auto
==============================================================================
Files camel/trunk/components/camel-mail/src/test/resources/jsse/localhost.ks 
(added) and 
camel/trunk/components/camel-mail/src/test/resources/jsse/localhost.ks Mon Mar  
5 15:49:58 2012 differ


Reply via email to