You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2008/04/14 21:31:21 UTC

svn commit: r647957 - in /activemq/camel/trunk/components/camel-jetty/src: main/java/org/apache/camel/component/jetty/ test/java/org/apache/camel/component/jetty/ test/resources/jsse/

Author: davsclaus
Date: Mon Apr 14 12:31:19 2008
New Revision: 647957

URL: http://svn.apache.org/viewvc?rev=647957&view=rev
Log:
CAMEL-409 Added unit tests for Jetty doing https. With huge thanks to Gary Tully and Micky Santomax for the patches

Added:
    activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java
    activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java
    activemq/camel/trunk/components/camel-jetty/src/test/resources/jsse/
    activemq/camel/trunk/components/camel-jetty/src/test/resources/jsse/localhost.ks   (with props)
Modified:
    activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java

Modified: activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java?rev=647957&r1=647956&r2=647957&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java (original)
+++ activemq/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java Mon Apr 14 12:31:19 2008
@@ -65,8 +65,10 @@
     private Server server;
     private final HashMap<String, ConnectorRef> connectors = new HashMap<String, ConnectorRef>();
     private HttpClient httpClient;
-    private String sslKeyPassword = "";
-    private String sslPassword = "";
+    private String sslKeyPassword;
+    private String sslPassword;
+    private String sslKeystore;
+    private SslSocketConnector sslSocketConnector;
 
     @Override
     protected Endpoint<HttpExchange> createEndpoint(String uri, String remaining, Map parameters) throws Exception {
@@ -91,10 +93,7 @@
             if (connectorRef == null) {
                 Connector connector;
                 if ("https".equals(endpoint.getProtocol())) {
-                    SslSocketConnector socketConnector = new SslSocketConnector();
-                    socketConnector.setPassword(sslPassword);
-                    socketConnector.setKeyPassword(sslKeyPassword);
-                    connector = socketConnector;
+                    connector = getSslSocketConnector();
                 } else {
                     connector = new SelectChannelConnector();
                 }
@@ -167,7 +166,33 @@
     public void setSslPassword(String sslPassword) {
         this.sslPassword = sslPassword;
     }
-
+    
+    public void setKeystore(String sslKeystore) {
+        this.sslKeystore = sslKeystore;  
+    }
+    
+    public String getKeystore() {
+        return sslKeystore;  
+    }
+
+    public synchronized SslSocketConnector getSslSocketConnector() {
+        if (sslSocketConnector == null) {
+            sslSocketConnector = new SslSocketConnector();
+            // with default null values, jetty ssl system properties
+            // and console will be read by jetty implementation
+            sslSocketConnector.setPassword(sslPassword);
+            sslSocketConnector.setKeyPassword(sslKeyPassword);
+            if (sslKeystore != null) {
+                sslSocketConnector.setKeystore(sslKeystore);
+            }
+        }
+        return sslSocketConnector;
+    }
+    
+    public void setSslSocketConnector(SslSocketConnector connector) {
+        sslSocketConnector = connector;
+    }
+    
     // Implementation methods
     // -------------------------------------------------------------------------
 

Added: activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java?rev=647957&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java (added)
+++ activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java Mon Apr 14 12:31:19 2008
@@ -0,0 +1,55 @@
+/**
+ * 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.jetty;
+
+import java.net.URL;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.mortbay.jetty.security.SslSocketConnector;
+
+public class ExplicitHttpsRouteTest extends HttpsRouteTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                
+                SslSocketConnector sslSocketConnector = new SslSocketConnector();
+                sslSocketConnector.setKeyPassword(pwd);
+                sslSocketConnector.setPassword(pwd);
+                URL keyStoreUrl = this.getClass().getClassLoader().getResource("jsse/localhost.ks");
+                sslSocketConnector.setKeystore(keyStoreUrl.getPath());
+                sslSocketConnector.setTruststoreType("JKS");
+                
+                JettyHttpComponent componentJetty = (JettyHttpComponent) context.getComponent("jetty");
+                componentJetty.setSslSocketConnector(sslSocketConnector);
+                
+                from("jetty:https://localhost:8080/test").to("mock:a");
+
+                Processor proc = new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        exchange.getOut(true).setBody("<b>Hello World</b>");
+                    }
+                };
+                from("jetty:https://localhost:8080/hello").process(proc);
+            }
+        };
+    }
+}
+

Added: activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java?rev=647957&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java (added)
+++ activemq/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java Mon Apr 14 12:31:19 2008
@@ -0,0 +1,157 @@
+/**
+ * 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.jetty;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.SocketException;
+import java.net.URL;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+
+public class HttpsRouteTest extends ContextTestSupport {
+    private static final String NULL_VALUE_MARKER = ContextTestSupport.class.getCanonicalName();
+    protected String expectedBody = "<hello>world!</hello>";
+    protected String pwd = "changeit";
+    protected Properties originalValues = new Properties();
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();        
+        // ensure jsse clients can validate the self signed dummy localhost cert, 
+        // use the server keystore as the trust store for these tests
+        URL trustStoreUrl = this.getClass().getClassLoader().getResource("jsse/localhost.ks");
+        setSystemProp("javax.net.ssl.trustStore", trustStoreUrl.getPath());
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        restoreSystemProperties();
+        super.tearDown();
+    }
+
+    private void setSystemProp(String key, String value) {
+        String originalValue = System.setProperty(key, value);
+        originalValues.put(key, originalValue != null ? originalValue : NULL_VALUE_MARKER);
+    }
+
+    private void restoreSystemProperties() {
+        for (Object key : originalValues.keySet()) {
+            Object value = (String) originalValues.get(key);  
+            if (NULL_VALUE_MARKER.equals(value)) {
+                System.getProperties().remove(key);    
+            } else {
+                System.setProperty((String)key, (String)value);
+            }
+        }
+    }
+
+    public void testEndpoint() throws Exception {
+        MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:a", MockEndpoint.class);
+        mockEndpoint.expectedBodiesReceived(expectedBody);
+
+        invokeHttpEndpoint();
+
+        mockEndpoint.assertIsSatisfied();
+        List<Exchange> list = mockEndpoint.getReceivedExchanges();
+        Exchange exchange = list.get(0);
+        assertNotNull("exchange", exchange);
+
+        Message in = exchange.getIn();
+        assertNotNull("in", in);
+
+        Map<String, Object> headers = in.getHeaders();
+
+        log.info("Headers: " + headers);
+
+        assertTrue("Should be more than one header but was: " + headers, headers.size() > 0);
+    }
+    
+    
+    public void testEndpointWithoutHttps() {
+        MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:a", MockEndpoint.class);    
+        try {
+            template.sendBodyAndHeader("jetty:http://localhost:8080/test", expectedBody, "Content-Type", "application/xml");
+            fail("expect exception on access to https endpoint via http");
+        } catch (RuntimeCamelException expected) {
+        }
+        assertTrue("mock endpoint was not called", mockEndpoint.getExchanges().isEmpty());
+    }
+
+    public void testHelloEndpoint() throws Exception {
+
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        InputStream is = new URL("https://localhost:8080/hello").openStream();
+        int c;
+        while ((c = is.read()) >= 0) {
+            os.write(c);
+        }
+
+        String data = new String(os.toByteArray());
+        assertEquals("<b>Hello World</b>", data);
+        
+    }
+        
+    public void testHelloEndpointWithoutHttps() throws Exception {
+        try {
+            new URL("http://localhost:8080/hello").openStream();
+            fail("expected SocketException on use ot http");
+        } catch (SocketException expected) {
+        }
+        
+        
+    }
+    
+    protected void invokeHttpEndpoint() throws IOException {
+        template.sendBodyAndHeader("jetty:https://localhost:8080/test", expectedBody, "Content-Type", "application/xml");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                
+                JettyHttpComponent componentJetty = (JettyHttpComponent) context.getComponent("jetty");
+                componentJetty.setSslPassword(pwd);
+                componentJetty.setSslKeyPassword(pwd);
+                URL keyStoreUrl = this.getClass().getClassLoader().getResource("jsse/localhost.ks");
+                componentJetty.setKeystore(keyStoreUrl.getPath());
+                
+                from("jetty:https://localhost:8080/test").to("mock:a");
+
+                Processor proc = new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        exchange.getOut(true).setBody("<b>Hello World</b>");
+                    }
+                };
+                from("jetty:https://localhost:8080/hello").process(proc);
+            }
+        };
+    }
+}
+

Added: activemq/camel/trunk/components/camel-jetty/src/test/resources/jsse/localhost.ks
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jetty/src/test/resources/jsse/localhost.ks?rev=647957&view=auto
==============================================================================
Binary file - no diff available.

Propchange: activemq/camel/trunk/components/camel-jetty/src/test/resources/jsse/localhost.ks
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream