Author: davsclaus Date: Fri May 8 08:29:04 2009 New Revision: 772883 URL: http://svn.apache.org/viewvc?rev=772883&view=rev Log: CAMEL-1570: Added support for configuring handlers on Jetty, eg for security. Thanks to Christopher Hunt for the patch.
Added: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java (with props) Modified: camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpEndpointDisconnectTest.java camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java Modified: camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java?rev=772883&r1=772882&r2=772883&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java (original) +++ camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java Fri May 8 08:29:04 2009 @@ -26,20 +26,19 @@ import org.apache.camel.component.http.HttpConsumer; import org.apache.camel.component.http.HttpEndpoint; import org.apache.camel.component.http.HttpExchange; -import org.apache.camel.util.CamelContextHelper; import org.apache.camel.util.IntrospectionSupport; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.mortbay.jetty.Connector; +import org.mortbay.jetty.Handler; import org.mortbay.jetty.Server; import org.mortbay.jetty.handler.ContextHandlerCollection; import org.mortbay.jetty.nio.SelectChannelConnector; import org.mortbay.jetty.security.SslSocketConnector; import org.mortbay.jetty.servlet.Context; -import org.mortbay.jetty.servlet.HashSessionIdManager; -import org.mortbay.jetty.servlet.HashSessionManager; import org.mortbay.jetty.servlet.ServletHolder; import org.mortbay.jetty.servlet.SessionHandler; @@ -128,7 +127,7 @@ } getServer().addConnector(connector); - connectorRef = new ConnectorRef(connector, createServletForConnector(connector)); + connectorRef = new ConnectorRef(connector, createServletForConnector(connector, endpoint.getHandlers())); connector.start(); connectors.put(connectorKey, connectorRef); @@ -242,12 +241,20 @@ sslSocketConnector = connector; } - protected CamelServlet createServletForConnector(Connector connector) throws Exception { + protected CamelServlet createServletForConnector(Connector connector, String handlerNames) throws Exception { CamelServlet camelServlet = new CamelContinuationServlet(); Context context = new Context(server, "/", Context.NO_SECURITY | Context.NO_SESSIONS); context.setConnectorNames(new String[] {connector.getName()}); + if (handlerNames != null) { + String[] handlerNameArray = handlerNames.split(","); + for (String handlerName : handlerNameArray) { + Handler handler = getHandler(handlerName); + context.addHandler(handler); + } + } + ServletHolder holder = new ServletHolder(); holder.setServlet(camelServlet); context.addServlet(holder, "/*"); @@ -285,4 +292,15 @@ super.doStop(); } + private Handler getHandler(String handlerName) { + Handler handler = null; + if (handlerName != null) { + handler = getCamelContext().getRegistry().lookup(handlerName, Handler.class); + ObjectHelper.notNull(handler, handlerName); + if (LOG.isDebugEnabled()) { + LOG.debug("Using context handler: " + handlerName); + } + } + return handler; + } } Modified: camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java?rev=772883&r1=772882&r2=772883&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java (original) +++ camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java Fri May 8 08:29:04 2009 @@ -37,7 +37,8 @@ public class JettyHttpEndpoint extends HttpEndpoint { private JettyHttpComponent component; private boolean sessionSupport; - + private String handlerNames; + public JettyHttpEndpoint(JettyHttpComponent component, String uri, URI httpURL, HttpClientParams clientParams, HttpConnectionManager httpConnectionManager, HttpClientConfigurer clientConfigurer) throws URISyntaxException { super(uri, component, httpURL, clientParams, httpConnectionManager, clientConfigurer); @@ -72,4 +73,12 @@ return sessionSupport; } + public String getHandlers() { + return handlerNames; + } + + public void setHandlers(String handlerNames) { + this.handlerNames = handlerNames; + } + } Modified: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java?rev=772883&r1=772882&r2=772883&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java (original) +++ camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java Fri May 8 08:29:04 2009 @@ -16,6 +16,7 @@ */ package org.apache.camel.component.jetty; +import java.net.URISyntaxException; import java.net.URL; import org.apache.camel.Exchange; @@ -28,12 +29,12 @@ @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { - public void configure() { + public void configure() throws URISyntaxException { SslSocketConnector sslSocketConnector = new SslSocketConnector(); sslSocketConnector.setKeyPassword(pwd); sslSocketConnector.setPassword(pwd); URL keyStoreUrl = this.getClass().getClassLoader().getResource("jsse/localhost.ks"); - sslSocketConnector.setKeystore(keyStoreUrl.getPath()); + sslSocketConnector.setKeystore(keyStoreUrl.toURI().getPath()); sslSocketConnector.setTruststoreType("JKS"); JettyHttpComponent componentJetty = (JettyHttpComponent) context.getComponent("jetty"); Added: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java?rev=772883&view=auto ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java (added) +++ camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java Fri May 8 08:29:04 2009 @@ -0,0 +1,94 @@ +/** + * 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.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.InputStreamReader; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; +import org.mortbay.jetty.handler.StatisticsHandler; + +public class HandlerTest extends ContextTestSupport { + private StatisticsHandler statisticsHandler1 = new StatisticsHandler(); + private StatisticsHandler statisticsHandler2 = new StatisticsHandler(); + private StatisticsHandler statisticsHandler3 = new StatisticsHandler(); + + private String htmlResponse = "<html><body>Book 123 is Camel in Action</body></html>"; + + public void testHandler() throws Exception { + // First test the situation where one should invoke the handler once + assertEquals(0, statisticsHandler1.getRequests()); + assertEquals(0, statisticsHandler2.getRequests()); + assertEquals(0, statisticsHandler3.getRequests()); + ByteArrayInputStream html = (ByteArrayInputStream) template + .requestBody("http://localhost:9080/", ""); + BufferedReader br = new BufferedReader(new InputStreamReader(html)); + assertEquals(htmlResponse, br.readLine()); + assertEquals(1, statisticsHandler1.getRequests()); + assertEquals(0, statisticsHandler2.getRequests()); + assertEquals(0, statisticsHandler3.getRequests()); + + // Now test the situation where one should invoke the handler twice + assertEquals(1, statisticsHandler1.getRequests()); + assertEquals(0, statisticsHandler2.getRequests()); + assertEquals(0, statisticsHandler3.getRequests()); + html = (ByteArrayInputStream) template.requestBody( + "http://localhost:9081/", ""); + br = new BufferedReader(new InputStreamReader(html)); + assertEquals(htmlResponse, br.readLine()); + assertEquals(1, statisticsHandler1.getRequests()); + assertEquals(1, statisticsHandler2.getRequests()); + assertEquals(1, statisticsHandler3.getRequests()); + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("statisticsHandler1", statisticsHandler1); + jndi.bind("statisticsHandler2", statisticsHandler2); + jndi.bind("statisticsHandler3", statisticsHandler3); + return jndi; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("jetty:http://localhost:9080/?handlers=statisticsHandler1") + .process(new Processor() { + public void process(Exchange exchange) + throws Exception { + exchange.getOut().setBody(htmlResponse); + } + }); + from( + "jetty:http://localhost:9081/?handlers=statisticsHandler2,statisticsHandler3") + .process(new Processor() { + public void process(Exchange exchange) + throws Exception { + exchange.getOut().setBody(htmlResponse); + } + }); + }; + }; + } +} Propchange: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java?rev=772883&r1=772882&r2=772883&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java (original) +++ camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java Fri May 8 08:29:04 2009 @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.SocketException; +import java.net.URISyntaxException; import java.net.URL; import java.util.List; import java.util.Map; @@ -46,7 +47,7 @@ // 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()); + setSystemProp("javax.net.ssl.trustStore", trustStoreUrl.toURI().getPath()); } @Override @@ -134,13 +135,13 @@ @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { - public void configure() { + public void configure() throws URISyntaxException { 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()); + componentJetty.setKeystore(keyStoreUrl.toURI().getPath()); from("jetty:https://localhost:9080/test").to("mock:a"); Modified: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java?rev=772883&r1=772882&r2=772883&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java (original) +++ camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java Fri May 8 08:29:04 2009 @@ -25,7 +25,7 @@ */ public class JettyContentBasedRouteTest extends ContextTestSupport { - private String serverUri = "http://localhost:5432/myservice"; + private String serverUri = "http://localhost:9080/myservice"; public void testSendOne() throws Exception { MockEndpoint mock = getMockEndpoint("mock:one"); @@ -61,4 +61,4 @@ }; } -} \ No newline at end of file +} Modified: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpEndpointDisconnectTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpEndpointDisconnectTest.java?rev=772883&r1=772882&r2=772883&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpEndpointDisconnectTest.java (original) +++ camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpEndpointDisconnectTest.java Fri May 8 08:29:04 2009 @@ -24,7 +24,7 @@ */ public class JettyHttpEndpointDisconnectTest extends ContextTestSupport { - private String serverUri = "http://localhost:5432/myservice"; + private String serverUri = "http://localhost:9080/myservice"; public void testContextShutdownRemovesHttpConnector() throws Exception { context.stop(); Modified: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java?rev=772883&r1=772882&r2=772883&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java (original) +++ camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java Fri May 8 08:29:04 2009 @@ -27,7 +27,7 @@ */ public class JettyHttpGetWithParamAsExchangeHeaderTest extends ContextTestSupport { - private String serverUri = "http://localhost:5432/myservice"; + private String serverUri = "http://localhost:9080/myservice"; public void testHttpGetWithParamsViaURI() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); @@ -69,4 +69,4 @@ }; } -} \ No newline at end of file +} Modified: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java?rev=772883&r1=772882&r2=772883&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java (original) +++ camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java Fri May 8 08:29:04 2009 @@ -30,7 +30,7 @@ */ public class JettyHttpGetWithParamTest extends ContextTestSupport { - private String serverUri = "http://localhost:5432/myservice"; + private String serverUri = "http://localhost:9080/myservice"; private MyParamsProcessor processor = new MyParamsProcessor(); public void testHttpGetWithParamsViaURI() throws Exception { Modified: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java?rev=772883&r1=772882&r2=772883&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java (original) +++ camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java Fri May 8 08:29:04 2009 @@ -25,7 +25,7 @@ */ public class JettySteveIssueTest extends ContextTestSupport { - private String serverUri = "http://localhost:5432/myservice"; + private String serverUri = "http://localhost:9080/myservice"; public void testSendX() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result");