CAMEL-9263: camel-servlet - Allow to configure the context path without leading slash. Fixed issue with camel-gae extending servlet and works a bit differently than a plain servlet.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/76f6bc4c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/76f6bc4c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/76f6bc4c Branch: refs/heads/master Commit: 76f6bc4ca93f4cd07623f77047a2b784bff1e982 Parents: 17724ed Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Nov 19 08:11:16 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Nov 19 08:11:16 2015 +0100 ---------------------------------------------------------------------- .../component/gae/http/GHttpComponent.java | 6 +++++ .../component/servlet/ServletComponent.java | 27 +++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/76f6bc4c/components/camel-gae/src/main/java/org/apache/camel/component/gae/http/GHttpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-gae/src/main/java/org/apache/camel/component/gae/http/GHttpComponent.java b/components/camel-gae/src/main/java/org/apache/camel/component/gae/http/GHttpComponent.java index c5bb708..2fecd4d 100644 --- a/components/camel-gae/src/main/java/org/apache/camel/component/gae/http/GHttpComponent.java +++ b/components/camel-gae/src/main/java/org/apache/camel/component/gae/http/GHttpComponent.java @@ -63,4 +63,10 @@ public class GHttpComponent extends ServletComponent { protected ServletEndpoint createServletEndpoint(String endpointUri, ServletComponent component, URI httpUri) throws Exception { return new GHttpEndpoint(endpointUri, component, httpUri); } + + @Override + protected boolean lenientContextPath() { + // must use the path as-is + return false; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/76f6bc4c/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 7da4497..25a1b1b 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 @@ -67,16 +67,18 @@ 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); + if (lenientContextPath()) { + // 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; } - 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); @@ -120,6 +122,13 @@ public class ServletComponent extends HttpCommonComponent implements RestConsume } /** + * Whether defining the context-path is lenient and do not require an exact leading slash. + */ + protected boolean lenientContextPath() { + return true; + } + + /** * Strategy to create the servlet endpoint. */ protected ServletEndpoint createServletEndpoint(String endpointUri, ServletComponent component, URI httpUri) throws Exception {