Author: davsclaus Date: Wed May 1 09:02:37 2013 New Revision: 1477935 URL: http://svn.apache.org/r1477935 Log: CAMEL-6327: More work on new camel-netty-http component.
Added: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSSLTest.java camel/trunk/components/camel-netty-http/src/test/resources/jsse/ camel/trunk/components/camel-netty-http/src/test/resources/jsse/localhost.ks (with props) Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java Modified: camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java?rev=1477935&r1=1477934&r2=1477935&view=diff ============================================================================== --- camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java (original) +++ camel/trunk/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpServerPipelineFactory.java Wed May 1 09:02:37 2013 @@ -21,6 +21,7 @@ import javax.net.ssl.SSLEngine; import org.apache.camel.component.netty.NettyConsumer; import org.apache.camel.component.netty.ServerPipelineFactory; +import org.apache.camel.component.netty.ssl.SSLEngineFactory; import org.apache.camel.util.ObjectHelper; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.Channels; @@ -48,7 +49,7 @@ public class HttpServerPipelineFactory e public HttpServerPipelineFactory(NettyHttpConsumer nettyConsumer) { this.consumer = nettyConsumer; try { - this.sslContext = createSSLContext(); + this.sslContext = createSSLContext(consumer); } catch (Exception e) { throw ObjectHelper.wrapRuntimeCamelException(e); } @@ -66,11 +67,10 @@ public class HttpServerPipelineFactory e // Create a default pipeline implementation. ChannelPipeline pipeline = Channels.pipeline(); - if (sslContext != null) { - SSLEngine engine = sslContext.createSSLEngine(); - engine.setUseClientMode(false); - engine.setNeedClientAuth(consumer.getConfiguration().isNeedClientAuth()); - pipeline.addLast("ssl", new SslHandler(engine)); + SslHandler sslHandler = configureServerSSLOnDemand(); + if (sslHandler != null) { + LOG.debug("Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler); + pipeline.addLast("ssl", sslHandler); } pipeline.addLast("decoder", new HttpRequestDecoder()); @@ -89,18 +89,46 @@ public class HttpServerPipelineFactory e return pipeline; } - private SSLContext createSSLContext() throws Exception { + private SSLContext createSSLContext(NettyConsumer consumer) throws Exception { if (!consumer.getConfiguration().isSsl()) { return null; } + // create ssl context once if (consumer.getConfiguration().getSslContextParameters() != null) { - return consumer.getConfiguration().getSslContextParameters().createSSLContext(); + SSLContext context = consumer.getConfiguration().getSslContextParameters().createSSLContext(); + return context; } return null; } + private SslHandler configureServerSSLOnDemand() throws Exception { + if (!consumer.getConfiguration().isSsl()) { + return null; + } + + if (consumer.getConfiguration().getSslHandler() != null) { + return consumer.getConfiguration().getSslHandler(); + } else if (sslContext != null) { + SSLEngine engine = sslContext.createSSLEngine(); + engine.setUseClientMode(false); + engine.setNeedClientAuth(consumer.getConfiguration().isNeedClientAuth()); + return new SslHandler(engine); + } else { + SSLEngineFactory sslEngineFactory = new SSLEngineFactory( + consumer.getConfiguration().getKeyStoreFormat(), + consumer.getConfiguration().getSecurityProvider(), + consumer.getConfiguration().getKeyStoreFile(), + consumer.getConfiguration().getTrustStoreFile(), + consumer.getConfiguration().getPassphrase().toCharArray()); + SSLEngine sslEngine = sslEngineFactory.createServerSSLEngine(); + sslEngine.setUseClientMode(false); + sslEngine.setNeedClientAuth(consumer.getConfiguration().isNeedClientAuth()); + return new SslHandler(sslEngine); + } + } + private boolean supportChunked() { return consumer.getEndpoint().getConfiguration().isChunked(); } Added: camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSSLTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSSLTest.java?rev=1477935&view=auto ============================================================================== --- camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSSLTest.java (added) +++ camel/trunk/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSSLTest.java Wed May 1 09:02:37 2013 @@ -0,0 +1,107 @@ +/** + * 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.netty.http; + +import java.io.File; +import java.net.URL; +import java.util.Properties; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.After; +import org.junit.Test; + +public class NettyHttpSSLTest extends BaseNettyTest { + + private static final String NULL_VALUE_MARKER = CamelTestSupport.class.getCanonicalName(); + + protected Properties originalValues = new Properties(); + + @Override + public void setUp() throws Exception { + // 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.toURI().getPath()); + + super.setUp(); + } + + @Override + @After + public void tearDown() throws Exception { + restoreSystemProperties(); + super.tearDown(); + } + + protected void setSystemProp(String key, String value) { + String originalValue = System.setProperty(key, value); + originalValues.put(key, originalValue != null ? originalValue : NULL_VALUE_MARKER); + } + + protected void restoreSystemProperties() { + for (Object key : originalValues.keySet()) { + Object value = originalValues.get(key); + if (NULL_VALUE_MARKER.equals(value)) { + System.getProperties().remove(key); + } else { + System.setProperty((String)key, (String)value); + } + } + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry registry = super.createRegistry(); + registry.bind("password", "changeit"); + registry.bind("ksf", new File("src/test/resources/jsse/localhost.ks")); + registry.bind("tsf", new File("src/test/resources/jsse/localhost.ks")); + return registry; + } + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testSSLInOutWithNettyConsumer() throws Exception { + // ibm jdks dont have sun security algorithms + if (isJavaVendor("ibm")) { + return; + } + + getMockEndpoint("mock:input").expectedBodiesReceived("Hello World"); + + context.addRoutes(new RouteBuilder() { + public void configure() { + from("netty-http:http://localhost:{{port}}?ssl=true&passphrase=#password&keyStoreFile=#ksf&trustStoreFile=#tsf") + .to("mock:input") + .transform().constant("Bye World"); + } + }); + context.start(); + + String out = template.requestBody("https://localhost:{{port}}/foo", "Hello World", String.class); + assertEquals("Bye World", out); + + assertMockEndpointsSatisfied(); + } + +} + Added: camel/trunk/components/camel-netty-http/src/test/resources/jsse/localhost.ks URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-netty-http/src/test/resources/jsse/localhost.ks?rev=1477935&view=auto ============================================================================== Binary file - no diff available. Propchange: camel/trunk/components/camel-netty-http/src/test/resources/jsse/localhost.ks ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream