Return-Path: Delivered-To: apmail-camel-commits-archive@www.apache.org Received: (qmail 37224 invoked from network); 8 May 2009 08:50:33 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 8 May 2009 08:50:33 -0000 Received: (qmail 49361 invoked by uid 500); 8 May 2009 08:50:32 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 49316 invoked by uid 500); 8 May 2009 08:50:32 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 49307 invoked by uid 99); 8 May 2009 08:50:32 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 08 May 2009 08:50:32 +0000 X-ASF-Spam-Status: No, hits=-1998.5 required=10.0 tests=ALL_TRUSTED,WEIRD_PORT X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 08 May 2009 08:50:30 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 6A9282388A6E; Fri, 8 May 2009 08:50:10 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r772891 - in /camel/trunk/components/camel-jetty/src: main/java/org/apache/camel/component/jetty/ test/java/org/apache/camel/component/jetty/ Date: Fri, 08 May 2009 08:50:09 -0000 To: commits@camel.apache.org From: davsclaus@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090508085010.6A9282388A6E@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: davsclaus Date: Fri May 8 08:50:09 2009 New Revision: 772891 URL: http://svn.apache.org/viewvc?rev=772891&view=rev Log: CAMEL-1570: Added support for configuring handlers on Jetty, eg for security. Thanks to Christopher Hunt for the patch. Added: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java (with props) Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpEndpointDisconnectTest.java camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java?rev=772891&r1=772890&r2=772891&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java (original) +++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java Fri May 8 08:50:09 2009 @@ -17,7 +17,9 @@ package org.apache.camel.component.jetty; import java.net.URI; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.camel.Endpoint; @@ -25,6 +27,7 @@ import org.apache.camel.component.http.HttpComponent; import org.apache.camel.component.http.HttpConsumer; import org.apache.camel.component.http.HttpEndpoint; +import org.apache.camel.util.CamelContextHelper; import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.URISupport; import org.apache.camel.util.UnsafeUriCharactersEncoder; @@ -32,6 +35,7 @@ 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; @@ -84,8 +88,21 @@ uri = uri.startsWith("jetty:") ? remaining : uri; HttpClientParams params = new HttpClientParams(); - IntrospectionSupport.setProperties(params, parameters, "httpClient."); + IntrospectionSupport.setProperties(params, parameters, "httpClient."); + // handlers + List handlerList = new ArrayList(); + String handlers = getAndRemoveParameter(parameters, "handlers", String.class); + if (handlers != null) { + // remove any leading # for reference lookup as we know its a reference lookup + handlers = handlers.replaceAll("#", ""); + // lookup each individual handler and add it to the list + for (String key : handlers.split(",")) { + handlerList.add(CamelContextHelper.mandatoryLookup(getCamelContext(), key, Handler.class)); + } + } + + // configure regular parameters configureParameters(parameters); // restructure uri to be based on the parameters left as we dont want to include the Camel internal options @@ -96,14 +113,15 @@ if (httpBinding != null) { result.setBinding(httpBinding); } + if (handlerList.size() > 0) { + result.setHandlers(handlerList); + } setProperties(result, parameters); return result; } /** * Connects the URL specified on the endpoint to the specified processor. - * - * @throws Exception */ @Override public void connect(HttpConsumer consumer) throws Exception { @@ -127,7 +145,7 @@ } getServer().addConnector(connector); - connectorRef = new ConnectorRef(connector, createServletForConnector(connector)); + connectorRef = new ConnectorRef(connector, createServletForConnector(connector, endpoint.getHandlers())); connector.start(); connectors.put(connectorKey, connectorRef); @@ -248,18 +266,23 @@ sslSocketConnector = connector; } - protected CamelServlet createServletForConnector(Connector connector) throws Exception { + protected CamelServlet createServletForConnector(Connector connector, List handlers) throws Exception { CamelServlet camelServlet = new CamelServlet(isMatchOnUriPrefix()); Context context = new Context(server, "/", Context.NO_SECURITY | Context.NO_SESSIONS); context.setConnectorNames(new String[] {connector.getName()}); + if (handlers != null) { + for (Handler handler : handlers) { + context.addHandler(handler); + } + } + ServletHolder holder = new ServletHolder(); holder.setServlet(camelServlet); context.addServlet(holder, "/*"); connector.start(); context.start(); - return camelServlet; } Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java?rev=772891&r1=772890&r2=772891&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java (original) +++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java Fri May 8 08:50:09 2009 @@ -18,6 +18,7 @@ import java.net.URI; import java.net.URISyntaxException; +import java.util.List; import org.apache.camel.Component; import org.apache.camel.Consumer; @@ -30,6 +31,7 @@ import org.apache.camel.component.http.HttpPollingConsumer; import org.apache.commons.httpclient.HttpConnectionManager; import org.apache.commons.httpclient.params.HttpClientParams; +import org.mortbay.jetty.Handler; /** * @version $Revision$ @@ -37,6 +39,7 @@ public class JettyHttpEndpoint extends HttpEndpoint { private JettyHttpComponent component; private boolean sessionSupport; + private List handlers; public JettyHttpEndpoint(JettyHttpComponent component, String uri, URI httpURL, HttpClientParams clientParams, HttpConnectionManager httpConnectionManager, HttpClientConfigurer clientConfigurer) throws URISyntaxException { @@ -71,4 +74,12 @@ public boolean isSessionSupport() { return sessionSupport; } + + public List getHandlers() { + return handlers; + } + + public void setHandlers(List handlers) { + this.handlers = handlers; + } } Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java?rev=772891&r1=772890&r2=772891&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java (original) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/ExplicitHttpsRouteTest.java Fri May 8 08:50:09 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/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java?rev=772891&view=auto ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java (added) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java Fri May 8 08:50:09 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 = "Book 123 is Camel in Action"; + + 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/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HandlerTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java?rev=772891&r1=772890&r2=772891&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java (original) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpsRouteTest.java Fri May 8 08:50:09 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/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java?rev=772891&r1=772890&r2=772891&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java (original) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java Fri May 8 08:50:09 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/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpEndpointDisconnectTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpEndpointDisconnectTest.java?rev=772891&r1=772890&r2=772891&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpEndpointDisconnectTest.java (original) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpEndpointDisconnectTest.java Fri May 8 08:50:09 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/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java?rev=772891&r1=772890&r2=772891&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java (original) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java Fri May 8 08:50:09 2009 @@ -26,7 +26,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"); @@ -68,4 +68,4 @@ }; } -} \ No newline at end of file +} Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java?rev=772891&r1=772890&r2=772891&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java (original) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java Fri May 8 08:50:09 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/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java?rev=772891&r1=772890&r2=772891&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java (original) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java Fri May 8 08:50:09 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");