Repository: camel Updated Branches: refs/heads/master 2774eda16 -> 625eb8d76
CAMEL-7620: Rest DSL. Enlist rest services in RestRegistry and JMX. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/625eb8d7 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/625eb8d7 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/625eb8d7 Branch: refs/heads/master Commit: 625eb8d76c4b74f1a69a1c82a4b7405610f7ef01 Parents: 2774eda Author: Claus Ibsen <davscl...@apache.org> Authored: Fri Aug 8 09:07:58 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Aug 8 09:07:58 2014 +0200 ---------------------------------------------------------------------- .../camel/component/rest/RestEndpoint.java | 14 +++++++++++ .../model/rest/RestConfigurationDefinition.java | 25 ++++++++++++++++++++ .../org/apache/camel/spi/RestConfiguration.java | 22 +++++++++++++++++ .../README.txt | 5 ++-- .../camel-example-servlet-rest-tomcat/pom.xml | 2 ++ .../camel/example/rest/UserRouteBuilder.java | 10 +++++++- .../src/main/resources/camel-config-xml.xml | 9 +++++-- .../src/main/webapp/index.html | 9 +++---- 8 files changed, 84 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/625eb8d7/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java index 7ce6564..1e2aac8 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestEndpoint.java @@ -193,11 +193,25 @@ public class RestEndpoint extends DefaultEndpoint { } } + // calculate the url to the rest service String path = getPath(); if (!path.startsWith("/")) { path = "/" + path; } + + // there may be an optional context path configured to help Camel calculate the correct urls for the REST services + // this may be needed when using camel-serlvet where we cannot get the actual context-path or port number of the servlet engine + // during init of the servlet + String contextPath = config.getContextPath(); + if (contextPath != null) { + if (!contextPath.startsWith("/")) { + path = "/" + contextPath + path; + } else { + path = contextPath + path; + } + } + String baseUrl = scheme + "://" + host + (port != 80 ? ":" + port : "") + path; String url = baseUrl; http://git-wip-us.apache.org/repos/asf/camel/blob/625eb8d7/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java index 54378f2..531fbc8 100644 --- a/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestConfigurationDefinition.java @@ -50,6 +50,9 @@ public class RestConfigurationDefinition { private String port; @XmlAttribute + private String contextPath; + + @XmlAttribute private RestHostNameResolver hostNameResolver; @XmlAttribute @@ -105,6 +108,14 @@ public class RestConfigurationDefinition { this.port = port; } + public String getContextPath() { + return contextPath; + } + + public void setContextPath(String contextPath) { + this.contextPath = contextPath; + } + public RestHostNameResolver getHostNameResolver() { return hostNameResolver; } @@ -213,6 +224,17 @@ public class RestConfigurationDefinition { } /** + * Sets a leading context-path the REST services will be using. + * <p/> + * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application + * is deployed using a context-path. + */ + public RestConfigurationDefinition contextPath(String contextPath) { + setContextPath(contextPath); + return this; + } + + /** * To specify the hostname resolver */ public RestConfigurationDefinition hostNameResolver(RestHostNameResolver hostNameResolver) { @@ -316,6 +338,9 @@ public class RestConfigurationDefinition { if (port != null) { answer.setPort(CamelContextHelper.parseInteger(context, port)); } + if (contextPath != null) { + answer.setContextPath(CamelContextHelper.parseText(context, contextPath)); + } if (hostNameResolver != null) { answer.setRestHostNameResolver(hostNameResolver.name()); } http://git-wip-us.apache.org/repos/asf/camel/blob/625eb8d7/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java b/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java index d3fcb2d..7767ee2 100644 --- a/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java +++ b/camel-core/src/main/java/org/apache/camel/spi/RestConfiguration.java @@ -36,6 +36,7 @@ public class RestConfiguration { private String scheme; private String host; private int port; + private String contextPath; private RestHostNameResolver restHostNameResolver = RestHostNameResolver.localHostName; private RestBindingMode bindingMode = RestBindingMode.off; private String jsonDataFormat; @@ -118,6 +119,27 @@ public class RestConfiguration { } /** + * Gets the configured context-path + * + * @return the context path, or <tt>null</tt> if none configured. + */ + public String getContextPath() { + return contextPath; + } + + /** + * Sets a leading context-path the REST services will be using. + * <p/> + * This can be used when using components such as <tt>camel-servlet</tt> where the deployed web application + * is deployed using a context-path. + * + * @param contextPath the context path + */ + public void setContextPath(String contextPath) { + this.contextPath = contextPath; + } + + /** * Gets the resolver to use for resolving hostname * * @return the resolver http://git-wip-us.apache.org/repos/asf/camel/blob/625eb8d7/examples/camel-example-servlet-rest-tomcat/README.txt ---------------------------------------------------------------------- diff --git a/examples/camel-example-servlet-rest-tomcat/README.txt b/examples/camel-example-servlet-rest-tomcat/README.txt index fc32e6e..81eac72 100644 --- a/examples/camel-example-servlet-rest-tomcat/README.txt +++ b/examples/camel-example-servlet-rest-tomcat/README.txt @@ -15,9 +15,8 @@ You will need to package this example first: To run the example deploy it in Apache Tomcat by copying the .war to the deploy folder of Apache Tomcat. -And then hit this url from a web browser which has further -instructions (use correct version number) - http://localhost:8080/camel-example-servlet-rest-tomcat-{version} +And then hit this url from a web browser which has further instructions + http://localhost:8080/camel-example-servlet-rest-tomcat You can also try the example from Maven using mvn jetty:run http://git-wip-us.apache.org/repos/asf/camel/blob/625eb8d7/examples/camel-example-servlet-rest-tomcat/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-servlet-rest-tomcat/pom.xml b/examples/camel-example-servlet-rest-tomcat/pom.xml index 3258e48..3009fc6 100755 --- a/examples/camel-example-servlet-rest-tomcat/pom.xml +++ b/examples/camel-example-servlet-rest-tomcat/pom.xml @@ -72,6 +72,8 @@ </dependencies> <build> + <finalName>${project.artifactId}</finalName> + <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> http://git-wip-us.apache.org/repos/asf/camel/blob/625eb8d7/examples/camel-example-servlet-rest-tomcat/src/main/java/org/apache/camel/example/rest/UserRouteBuilder.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-servlet-rest-tomcat/src/main/java/org/apache/camel/example/rest/UserRouteBuilder.java b/examples/camel-example-servlet-rest-tomcat/src/main/java/org/apache/camel/example/rest/UserRouteBuilder.java index 7b4729a..fe4c5ce 100644 --- a/examples/camel-example-servlet-rest-tomcat/src/main/java/org/apache/camel/example/rest/UserRouteBuilder.java +++ b/examples/camel-example-servlet-rest-tomcat/src/main/java/org/apache/camel/example/rest/UserRouteBuilder.java @@ -31,7 +31,15 @@ public class UserRouteBuilder extends RouteBuilder { // and we enable json binding mode restConfiguration().component("servlet").bindingMode(RestBindingMode.json) // and output using pretty print - .dataFormatProperty("prettyPrint", "true"); + .dataFormatProperty("prettyPrint", "true") + // setup context path and port number that Apache Tomcat will deploy + // this application with, as we use the servlet component, then we + // need to aid Camel to tell it these details so Camel knows the url + // to the REST services. + // Notice: This is optional, but needed if the RestRegistry should + // enlist accurate information. You can access the RestRegistry + // from JMX at runtime + .contextPath("camel-example-servlet-rest-tomcat/rest").port(8080); // this user REST service is json only rest("/user").consumes("application/json").produces("application/json") http://git-wip-us.apache.org/repos/asf/camel/blob/625eb8d7/examples/camel-example-servlet-rest-tomcat/src/main/resources/camel-config-xml.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-servlet-rest-tomcat/src/main/resources/camel-config-xml.xml b/examples/camel-example-servlet-rest-tomcat/src/main/resources/camel-config-xml.xml index 0ef2fcc..3138843 100755 --- a/examples/camel-example-servlet-rest-tomcat/src/main/resources/camel-config-xml.xml +++ b/examples/camel-example-servlet-rest-tomcat/src/main/resources/camel-config-xml.xml @@ -30,12 +30,17 @@ <!-- configure rest to use the camel-servlet component, and use json binding mode --> <!-- and tell to output json in pretty print mode --> - <restConfiguration component="servlet" bindingMode="json"> + <!-- setup context path and port number that Apache Tomcat will deploy this application with, + as we use the servlet component, then we need to aid Camel to tell it these details so Camel + knows the url to the REST services. + Notice: This is optional, but needed if the RestRegistry should enlist accurate information. + You can access the RestRegistry from JMX at runtime --> + <restConfiguration component="servlet" bindingMode="json" contextPath="camel-example-servlet-rest-tomcat/rest" port="8080"> <dataFormatProperty key="prettyPrint" value="true"/> </restConfiguration> <!-- defines the rest services using the context-path /user --> - <rest uri="/user" consumes="application/json" produces="application/json"> + <rest path="/user" consumes="application/json" produces="application/json"> <!-- this is a rest GET to view an user by the given id --> <get uri="/{id}" outType="org.apache.camel.example.rest.User"> http://git-wip-us.apache.org/repos/asf/camel/blob/625eb8d7/examples/camel-example-servlet-rest-tomcat/src/main/webapp/index.html ---------------------------------------------------------------------- diff --git a/examples/camel-example-servlet-rest-tomcat/src/main/webapp/index.html b/examples/camel-example-servlet-rest-tomcat/src/main/webapp/index.html index e48aed4..ac6b54b 100644 --- a/examples/camel-example-servlet-rest-tomcat/src/main/webapp/index.html +++ b/examples/camel-example-servlet-rest-tomcat/src/main/webapp/index.html @@ -49,14 +49,11 @@ From a web browser you can access the first two services using the following lin From the command shell you can use curl to access the service as shown below: <pre> - curl -X GET -H "Accept: application/json" http://localhost:8080/camel-example-servlet-rest-tomcat-{version}/rest/user/123 - curl -X GET -H "Accept: application/json" http://localhost:8080/camel-example-servlet-rest-tomcat-{version}/rest/user/findAll - curl -X PUT -d "{ \"id\": 666, \"name\": \"The devil\"}" -H "Accept: application/json" http://localhost:8080/camel-example-servlet-rest-tomcat-{version}/rest/user + curl -X GET -H "Accept: application/json" http://localhost:8080/camel-example-servlet-rest-tomcat/rest/user/123 + curl -X GET -H "Accept: application/json" http://localhost:8080/camel-example-servlet-rest-tomcat/rest/user/findAll + curl -X PUT -d "{ \"id\": 666, \"name\": \"The devil\"}" -H "Accept: application/json" http://localhost:8080/camel-example-servlet-rest-tomcat/rest/user </pre> -This assume you installed the example by copying the .war as <tt>camel-example-servlet-rest-tomcat-VERSION.war</tt> -, where VERSION is the Camel version such as 2.14.0, into the <b>webapps</b> directory of Apache Tomcat. - <p/> If you hit any problems please let us know on the <a href="http://camel.apache.org/discussion-forums.html">Camel Forums</a>