CAMEL-9263: camel-servlet - Allow to configure the context path without leading slash
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3f84e4a2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3f84e4a2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3f84e4a2 Branch: refs/heads/camel-2.16.x Commit: 3f84e4a209087bf910b1c0e130cdf7564ee63ef8 Parents: 0661fdc Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Oct 28 11:21:45 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Oct 28 11:24:43 2015 +0100 ---------------------------------------------------------------------- .../component/servlet/ServletComponent.java | 12 +++++ .../component/servlet/ServletEndpoint.java | 18 +++++++- .../ExposedServletEndpointURIToJMXTest.java | 13 +++--- .../component/servlet/HttpClientRouteTest.java | 10 ++--- ...ponentConfigurationAndDocumentationTest.java | 2 +- .../servlet/ServletNoSlashNeededTest.java | 47 ++++++++++++++++++++ .../component/servlet/ServletSetBodyTest.java | 2 +- .../servlet/ServletTransferExceptionTest.java | 2 +- .../component/servlet/example-camelContext.xml | 2 +- .../src/main/resources/camel-config.xml | 2 +- 10 files changed, 92 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3f84e4a2/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java index 5aca6a6..7da4497 100644 --- a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java +++ b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java @@ -33,6 +33,7 @@ import org.apache.camel.spi.RestApiConsumerFactory; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RestConsumerFactory; import org.apache.camel.util.FileUtil; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; import org.apache.camel.util.UnsafeUriCharactersEncoder; @@ -66,6 +67,17 @@ public class ServletComponent extends HttpCommonComponent implements RestConsume String httpMethodRestrict = getAndRemoveParameter(parameters, "httpMethodRestrict", String.class); HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class); + // the uri must have a leading slash for the context-path matching to work with servlet, and it can be something people + // forget to add and then the servlet consumer cannot match the context-path as would have been expected + String scheme = ObjectHelper.before(uri, ":"); + String after = ObjectHelper.after(uri, ":"); + // rebuild uri to have exactly one leading slash + while (after.startsWith("/")) { + after = after.substring(1); + } + after = "/" + after; + uri = scheme + ":" + after; + // restructure uri to be based on the parameters left as we dont want to include the Camel internal options URI httpUri = URISupport.createRemainingURI(new URI(UnsafeUriCharactersEncoder.encodeHttpURI(uri)), parameters); http://git-wip-us.apache.org/repos/asf/camel/blob/3f84e4a2/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java index 1e870da..e2d4695 100644 --- a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java +++ b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletEndpoint.java @@ -25,13 +25,17 @@ import org.apache.camel.Producer; import org.apache.camel.http.common.HttpCommonEndpoint; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriPath; @UriEndpoint(scheme = "servlet", extendsScheme = "http", title = "Servlet", - syntax = "servlet:servletName", consumerOnly = true, consumerClass = ServletConsumer.class, label = "http") + syntax = "servlet:contextPath", consumerOnly = true, consumerClass = ServletConsumer.class, label = "http") public class ServletEndpoint extends HttpCommonEndpoint { @UriPath(label = "consumer") @Metadata(required = "true") + private String contextPath; + + @UriParam(label = "consumer", defaultValue = "CamelServlet") private String servletName; public ServletEndpoint() { @@ -39,6 +43,18 @@ public class ServletEndpoint extends HttpCommonEndpoint { public ServletEndpoint(String endPointURI, ServletComponent component, URI httpUri) throws URISyntaxException { super(endPointURI, component, httpUri); + this.contextPath = httpUri.getPath(); + } + + public String getContextPath() { + return contextPath; + } + + /** + * The context-path to use + */ + public void setContextPath(String contextPath) { + this.contextPath = contextPath; } /** http://git-wip-us.apache.org/repos/asf/camel/blob/3f84e4a2/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ExposedServletEndpointURIToJMXTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ExposedServletEndpointURIToJMXTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ExposedServletEndpointURIToJMXTest.java index 383b2ea..2935ff4 100644 --- a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ExposedServletEndpointURIToJMXTest.java +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ExposedServletEndpointURIToJMXTest.java @@ -34,9 +34,9 @@ public class ExposedServletEndpointURIToJMXTest extends CamelTestSupport { @Test public void exposedEndpointURIShouldContainContextAndOptions() throws Exception { - checkServletEndpointURI("\"servlet:///test1\\?matchOnUriPrefix=true\""); - checkServletEndpointURI("\"servlet:///test2\\?servletName=test2\""); - checkServletEndpointURI("\"servlet:///test3\\?matchOnUriPrefix=true&servletName=test3\""); + checkServletEndpointURI("\"servlet:/test1\\?matchOnUriPrefix=true\""); + checkServletEndpointURI("\"servlet:/test2\\?servletName=test2\""); + checkServletEndpointURI("\"servlet:/test3\\?matchOnUriPrefix=true&servletName=test3\""); } private void checkServletEndpointURI(String servletEndpointURI) throws Exception { @@ -48,14 +48,13 @@ public class ExposedServletEndpointURIToJMXTest extends CamelTestSupport { } protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { @Override public void configure() throws Exception { - from("servlet:///test1?matchOnUriPrefix=true").to("mock:jmx"); - from("servlet:///test2?servletName=test2").to("mock:jmx"); - from("servlet:///test3?matchOnUriPrefix=true&servletName=test3").to("mock:jmx"); + from("servlet:test1?matchOnUriPrefix=true").to("mock:jmx"); + from("servlet:test2?servletName=test2").to("mock:jmx"); + from("servlet:test3?matchOnUriPrefix=true&servletName=test3").to("mock:jmx"); } }; http://git-wip-us.apache.org/repos/asf/camel/blob/3f84e4a2/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java index b0453c2..aff55c1 100644 --- a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java @@ -133,7 +133,7 @@ public class HttpClientRouteTest extends ServletCamelRouterTestSupport { public void configure() throws Exception { errorHandler(noErrorHandler()); // START SNIPPET: route - from("servlet:///hello?matchOnUriPrefix=true").process(new Processor() { + from("servlet:hello?matchOnUriPrefix=true").process(new Processor() { public void process(Exchange exchange) throws Exception { String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class); String path = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class); @@ -152,14 +152,14 @@ public class HttpClientRouteTest extends ServletCamelRouterTestSupport { }); // END SNIPPET: route - from("servlet:///testHttpMethodRestrict?httpMethodRestrict=POST").process(new Processor() { + from("servlet:testHttpMethodRestrict?httpMethodRestrict=POST").process(new Processor() { public void process(Exchange exchange) throws Exception { String request = exchange.getIn().getBody(String.class); exchange.getOut().setBody(request); } }); - from("servlet:///testConverter?matchOnUriPrefix=true") + from("servlet:testConverter?matchOnUriPrefix=true") .convertBodyTo(String.class) .process(new Processor() { public void process(Exchange exchange) throws Exception { @@ -172,7 +172,7 @@ public class HttpClientRouteTest extends ServletCamelRouterTestSupport { } }).transform(constant("Bye World")); - from("servlet:///testUnicodeWithStringResponse?matchOnUriPrefix=true") + from("servlet:testUnicodeWithStringResponse?matchOnUriPrefix=true") .process(new Processor() { public void process(Exchange exchange) throws Exception { String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class); @@ -181,7 +181,7 @@ public class HttpClientRouteTest extends ServletCamelRouterTestSupport { }) .transform(constant(UNICODE_TEXT)); - from("servlet:///testUnicodeWithObjectResponse?matchOnUriPrefix=true") + from("servlet:testUnicodeWithObjectResponse?matchOnUriPrefix=true") .process(new Processor() { public void process(Exchange exchange) throws Exception { String contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class); http://git-wip-us.apache.org/repos/asf/camel/blob/3f84e4a2/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletComponentConfigurationAndDocumentationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletComponentConfigurationAndDocumentationTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletComponentConfigurationAndDocumentationTest.java index 4843c9f..5c3f020 100644 --- a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletComponentConfigurationAndDocumentationTest.java +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletComponentConfigurationAndDocumentationTest.java @@ -31,7 +31,7 @@ public class ServletComponentConfigurationAndDocumentationTest extends CamelTest @Test public void testComponentConfiguration() throws Exception { ServletComponent comp = context.getComponent("servlet", ServletComponent.class); - EndpointConfiguration conf = comp.createConfiguration("servlet://foo?servletName=MyServlet&proxyHost=myotherproxy&proxyPort=2345"); + EndpointConfiguration conf = comp.createConfiguration("servlet:foo?servletName=MyServlet&proxyHost=myotherproxy&proxyPort=2345"); assertEquals("myotherproxy", conf.getParameter("proxyHost")); assertEquals("2345", conf.getParameter("proxyPort")); http://git-wip-us.apache.org/repos/asf/camel/blob/3f84e4a2/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletNoSlashNeededTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletNoSlashNeededTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletNoSlashNeededTest.java new file mode 100644 index 0000000..246e6bb --- /dev/null +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletNoSlashNeededTest.java @@ -0,0 +1,47 @@ +/** + * 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.servlet; + +import com.meterware.httpunit.GetMethodWebRequest; +import com.meterware.httpunit.WebRequest; +import com.meterware.httpunit.WebResponse; +import com.meterware.servletunit.ServletUnitClient; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class ServletNoSlashNeededTest extends ServletCamelRouterTestSupport { + + @Test + public void testNoSlashNeeded() throws Exception { + WebRequest req = new GetMethodWebRequest(CONTEXT_URL + "/services/hello"); + ServletUnitClient client = newClient(); + WebResponse response = client.getResponse(req); + + assertEquals("The response message is wrong ", "Bye World", response.getText()); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("servlet:hello") + .setBody().constant("Bye World"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/3f84e4a2/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletSetBodyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletSetBodyTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletSetBodyTest.java index d0a7023..b28360f 100644 --- a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletSetBodyTest.java +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletSetBodyTest.java @@ -38,7 +38,7 @@ public class ServletSetBodyTest extends ServletCamelRouterTestSupport { return new RouteBuilder() { @Override public void configure() throws Exception { - from("servlet:///hello") + from("servlet:/hello") .setBody().constant("Bye World"); } }; http://git-wip-us.apache.org/repos/asf/camel/blob/3f84e4a2/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletTransferExceptionTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletTransferExceptionTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletTransferExceptionTest.java index 62a0f06..db91f6c 100644 --- a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletTransferExceptionTest.java +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/ServletTransferExceptionTest.java @@ -53,7 +53,7 @@ public class ServletTransferExceptionTest extends ServletCamelRouterTestSupport return new RouteBuilder() { @Override public void configure() throws Exception { - from("servlet:///hello?transferException=true") + from("servlet:hello?transferException=true") .throwException(new IllegalArgumentException("Damn")); } }; http://git-wip-us.apache.org/repos/asf/camel/blob/3f84e4a2/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/example-camelContext.xml ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/example-camelContext.xml b/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/example-camelContext.xml index 13637de..127f213 100644 --- a/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/example-camelContext.xml +++ b/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/example-camelContext.xml @@ -26,7 +26,7 @@ <camelContext id="camel" streamCache="true" xmlns="http://camel.apache.org/schema/spring" > <route id="helloRoute"> <!-- incoming requests from the servlet is routed --> - <from uri="servlet:///hello"/> + <from uri="servlet:hello"/> <choice> <when> <!-- is there a header with the key name? --> http://git-wip-us.apache.org/repos/asf/camel/blob/3f84e4a2/examples/camel-example-servlet-tomcat/src/main/resources/camel-config.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-servlet-tomcat/src/main/resources/camel-config.xml b/examples/camel-example-servlet-tomcat/src/main/resources/camel-config.xml index cb07609..7e9e9c0 100755 --- a/examples/camel-example-servlet-tomcat/src/main/resources/camel-config.xml +++ b/examples/camel-example-servlet-tomcat/src/main/resources/camel-config.xml @@ -28,7 +28,7 @@ <route id="helloRoute"> <!-- incoming requests from the servlet is routed --> - <from uri="servlet:///hello"/> + <from uri="servlet:hello"/> <choice> <when> <!-- is there a header with the key name? -->