Repository: camel Updated Branches: refs/heads/master 1c64ca33c -> ff842d3b0
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/e239c0f2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e239c0f2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e239c0f2 Branch: refs/heads/master Commit: e239c0f2d7a29024a31ea3d7e10e05856864e5fe Parents: 1c64ca3 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Aug 6 13:35:47 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Aug 6 13:35:47 2014 +0200 ---------------------------------------------------------------------- .../management/mbean/CamelOpenMBeanTypes.java | 7 ++--- .../camel/component/rest/RestComponent.java | 18 ++++++++++++- .../camel/component/rest/RestEndpoint.java | 26 +++++++++++++++--- .../apache/camel/impl/DefaultRestRegistry.java | 28 ++++++++++++++------ .../management/mbean/ManagedRestRegistry.java | 10 ++++--- .../apache/camel/model/rest/RestDefinition.java | 5 +--- .../apache/camel/spi/RestConsumerFactory.java | 5 ++-- .../java/org/apache/camel/spi/RestRegistry.java | 22 ++++++++++++--- .../rest/DummyRestConsumerFactory.java | 9 +++++-- .../component/jetty/JettyHttpComponent.java | 25 ++++++++++++++--- .../netty/http/NettyHttpComponent.java | 26 +++++++++++++++--- .../component/restlet/RestletComponent.java | 26 +++++++++++++++--- .../component/servlet/ServletComponent.java | 15 ++++++++--- .../component/sparkrest/SparkComponent.java | 20 +++++++++++--- .../rest/DummyRestConsumerFactory.java | 9 +++++-- 15 files changed, 202 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java b/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java index ec56266..3102713 100644 --- a/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java +++ b/camel-core/src/main/java/org/apache/camel/api/management/mbean/CamelOpenMBeanTypes.java @@ -47,9 +47,10 @@ public final class CamelOpenMBeanTypes { } public static CompositeType listRestServicesCompositeType() throws OpenDataException { - return new CompositeType("rests", "Rest Services", new String[]{"url", "method", "uriTemplate", "consumes", "produces", "inType", "outType", "state"}, - new String[]{"Url", "Method", "Uri Template", "Consumes", "Produces", "Input Type", "Output Type", "State"}, - new OpenType[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING}); + return new CompositeType("rests", "Rest Services", new String[]{"url", "baseUrl", "basePath", "uriTemplate", "method", "consumes", "produces", "inType", "outType", "state"}, + new String[]{"Url", "Base Url", "Base Path", "Uri Template", "Method", "Consumes", "Produces", "Input Type", "Output Type", "State"}, + new OpenType[]{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, + SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING}); } } http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java index d595de6..687fb72 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java @@ -20,6 +20,7 @@ import java.util.Map; import org.apache.camel.Endpoint; import org.apache.camel.impl.UriEndpointComponent; +import org.apache.camel.util.FileUtil; import org.apache.camel.util.ObjectHelper; public class RestComponent extends UriEndpointComponent { @@ -39,10 +40,25 @@ public class RestComponent extends UriEndpointComponent { } String verb = ObjectHelper.before(remaining, ":"); - String path = ObjectHelper.after(remaining, ":"); + String s = ObjectHelper.after(remaining, ":"); + + String path; + String uriTemplate; + if (s != null && s.contains(":")) { + path = ObjectHelper.before(s, ":"); + uriTemplate = ObjectHelper.after(s, ":"); + } else { + path = s; + uriTemplate = null; + } + + // remove trailing slashes + path = FileUtil.stripTrailingSeparator(path); + uriTemplate = FileUtil.stripTrailingSeparator(uriTemplate); answer.setVerb(verb); answer.setPath(path); + answer.setUriTemplate(uriTemplate); // if no explicit component name was given, then fallback and use default configured component name if (answer.getComponentName() == null && getCamelContext().getRestConfiguration() != null) { http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/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 12bf7b7..66001c5 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 @@ -40,6 +40,8 @@ public class RestEndpoint extends DefaultEndpoint { @UriParam private String path; @UriParam + private String uriTemplate; + @UriParam private String consumes; @UriParam private String produces; @@ -73,6 +75,14 @@ public class RestEndpoint extends DefaultEndpoint { this.path = path; } + public String getUriTemplate() { + return uriTemplate; + } + + public void setUriTemplate(String uriTemplate) { + this.uriTemplate = uriTemplate; + } + public String getConsumes() { return consumes; } @@ -154,7 +164,7 @@ public class RestEndpoint extends DefaultEndpoint { } if (factory != null) { - Consumer consumer = factory.createConsumer(getCamelContext(), processor, getVerb(), getPath(), getConsumes(), getProduces(), getParameters()); + Consumer consumer = factory.createConsumer(getCamelContext(), processor, getVerb(), getPath(), getUriTemplate(), getConsumes(), getProduces(), getParameters()); configureConsumer(consumer); // if no explicit port/host configured, then use port from rest configuration @@ -188,7 +198,17 @@ public class RestEndpoint extends DefaultEndpoint { if (!path.startsWith("/")) { path = "/" + path; } - String url = scheme + "://" + host + (port != 80 ? ":" + port : "") + path; + String baseUrl = scheme + "://" + host + (port != 80 ? ":" + port : "") + path; + + String url = baseUrl; + if (uriTemplate != null) { + // make sure to avoid double slashes + if (uriTemplate.startsWith("/")) { + url = url + uriTemplate; + } else { + url = url + "/" + uriTemplate; + } + } // optional binding type information String inType = (String) getParameters().get("inType"); @@ -197,7 +217,7 @@ public class RestEndpoint extends DefaultEndpoint { // add to rest registry so we can keep track of them, we will remove from the registry when the consumer is removed // the rest registry will automatic keep track when the consumer is removed, // and un-register the REST service from the registry - getCamelContext().getRestRegistry().addRestService(consumer, url, getVerb(), getPath(), getConsumes(), getProduces(), inType, outType); + getCamelContext().getRestRegistry().addRestService(consumer, url, baseUrl, getPath(), getUriTemplate(), getVerb(), getConsumes(), getProduces(), inType, outType); return consumer; } else { http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/camel-core/src/main/java/org/apache/camel/impl/DefaultRestRegistry.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultRestRegistry.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultRestRegistry.java index 1d6d8f6..62194ad 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultRestRegistry.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultRestRegistry.java @@ -38,9 +38,9 @@ public class DefaultRestRegistry extends ServiceSupport implements StaticService private CamelContext camelContext; private final Map<Consumer, RestService> registry = new LinkedHashMap<Consumer, RestService>(); - public void addRestService(Consumer consumer, String url, String method, String uriTemplate, String consumes, String produces, - String inType, String outType) { - RestServiceEntry entry = new RestServiceEntry(consumer, url, uriTemplate, method, consumes, produces, inType, outType); + public void addRestService(Consumer consumer, String url, String baseUrl, String basePath, String uriTemplate, String method, + String consumes, String produces, String inType, String outType) { + RestServiceEntry entry = new RestServiceEntry(consumer, url, baseUrl, basePath, uriTemplate, method, consumes, produces, inType, outType); registry.put(consumer, entry); } @@ -85,18 +85,22 @@ public class DefaultRestRegistry extends ServiceSupport implements StaticService private final Consumer consumer; private final String url; - private final String path; + private final String baseUrl; + private final String basePath; + private final String uriTemplate; private final String method; private final String consumes; private final String produces; private final String inType; private final String outType; - private RestServiceEntry(Consumer consumer, String url, String path, String method, String consumes, String produces, - String inType, String outType) { + private RestServiceEntry(Consumer consumer, String url, String baseUrl, String basePath, String uriTemplate, + String method, String consumes, String produces, String inType, String outType) { this.consumer = consumer; this.url = url; - this.path = path; + this.baseUrl = baseUrl; + this.basePath = basePath; + this.uriTemplate = uriTemplate; this.method = method; this.consumes = consumes; this.produces = produces; @@ -112,8 +116,16 @@ public class DefaultRestRegistry extends ServiceSupport implements StaticService return url; } + public String getBaseUrl() { + return baseUrl; + } + + public String getBasePath() { + return basePath; + } + public String getUriTemplate() { - return path; + return uriTemplate; } public String getMethod() { http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRestRegistry.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRestRegistry.java b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRestRegistry.java index 37214b0..dfc9c19 100644 --- a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRestRegistry.java +++ b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRestRegistry.java @@ -60,15 +60,19 @@ public class ManagedRestRegistry extends ManagedService implements ManagedRestRe for (RestRegistry.RestService entry : services) { CompositeType ct = CamelOpenMBeanTypes.listRestServicesCompositeType(); String url = entry.getUrl(); - String method = entry.getMethod(); + String baseUrl = entry.getBaseUrl(); + String basePath = entry.getBasePath(); String uriTemplate = entry.getUriTemplate(); + String method = entry.getMethod(); String consumes = entry.getConsumes(); String produces = entry.getProduces(); String state = entry.getState(); String inType = entry.getInType(); String outType = entry.getOutType(); - CompositeData data = new CompositeDataSupport(ct, new String[]{"url", "method", "uriTemplate", "consumes", "produces", "inType", "outType", "state"}, - new Object[]{url, method, uriTemplate, consumes, produces, inType, outType, state}); + + CompositeData data = new CompositeDataSupport(ct, new String[] + {"url", "baseUrl", "basePath", "uriTemplate", "method", "consumes", "produces", "inType", "outType", "state"}, + new Object[]{url, baseUrl, basePath, uriTemplate, method, consumes, produces, inType, outType, state}); answer.put(data); } return answer; http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java index a1c7bec..2039328 100644 --- a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java @@ -387,10 +387,7 @@ public class RestDefinition { private String buildUri(VerbDefinition verb) { if (uri != null && verb.getUri() != null) { - // make sure there is only one / slash separator between the two - String s = FileUtil.stripTrailingSeparator(uri); - String s2 = FileUtil.stripLeadingSeparator(verb.getUri()); - return s + "/" + s2; + return uri + ":" + verb.getUri(); } else if (uri != null) { return uri; } else if (verb.getUri() != null) { http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/camel-core/src/main/java/org/apache/camel/spi/RestConsumerFactory.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestConsumerFactory.java b/camel-core/src/main/java/org/apache/camel/spi/RestConsumerFactory.java index 8af67c7..ac07fb7 100644 --- a/camel-core/src/main/java/org/apache/camel/spi/RestConsumerFactory.java +++ b/camel-core/src/main/java/org/apache/camel/spi/RestConsumerFactory.java @@ -32,13 +32,14 @@ public interface RestConsumerFactory { * @param camelContext the camel context * @param processor the processor * @param verb HTTP verb such as GET, POST - * @param path HTTP context-path + * @param basePath base path + * @param uriTemplate uri template * @param consumes media-types for what this REST service consume as input (accept-type), is <tt>null</tt> or <tt>*/*</tt> for anything * @param produces media-types for what this REST service produces as output, can be <tt>null</tt> * @param parameters additional parameters * @return a newly created REST consumer * @throws Exception can be thrown */ - Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String path, + Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, Map<String, Object> parameters) throws Exception; } http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/camel-core/src/main/java/org/apache/camel/spi/RestRegistry.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/spi/RestRegistry.java b/camel-core/src/main/java/org/apache/camel/spi/RestRegistry.java index f8c894e..92ac05d 100644 --- a/camel-core/src/main/java/org/apache/camel/spi/RestRegistry.java +++ b/camel-core/src/main/java/org/apache/camel/spi/RestRegistry.java @@ -43,12 +43,22 @@ public interface RestRegistry extends Service { String getState(); /** - * Gets the absolute url to the REST service + * Gets the absolute url to the REST service (baseUrl + uriTemplate) */ String getUrl(); /** - * Gets the uri template (context path) + * Gets the base url to the REST service + */ + String getBaseUrl(); + + /** + * Gets the base path to the REST service + */ + String getBasePath(); + + /** + * Gets the uri template */ String getUriTemplate(); @@ -88,12 +98,16 @@ public interface RestRegistry extends Service { * * @param consumer the consumer * @param url the absolute url of the REST service + * @param baseUrl the base url of the REST service + * @param basePath the base path + * @param uriTemplate the uri template * @param method the HTTP method - * @param uriTemplate the uri template (context-path) * @param consumes optional details about what media-types the REST service accepts * @param produces optional details about what media-types the REST service returns + * @param inType optional detail input binding to a FQN class name + * @param outType optional detail output binding to a FQN class name */ - void addRestService(Consumer consumer, String url, String method, String uriTemplate, String consumes, String produces, + void addRestService(Consumer consumer, String url, String baseUrl, String basePath, String uriTemplate, String method, String consumes, String produces, String inType, String outType); /** http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestConsumerFactory.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestConsumerFactory.java b/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestConsumerFactory.java index 837933f..84819a4 100644 --- a/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestConsumerFactory.java +++ b/camel-core/src/test/java/org/apache/camel/component/rest/DummyRestConsumerFactory.java @@ -28,10 +28,15 @@ import org.apache.camel.spi.RestConsumerFactory; public class DummyRestConsumerFactory implements RestConsumerFactory { @Override - public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String path, + public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, Map<String, Object> parameters) throws Exception { // just use a seda endpoint for testing purpose - String id = ActiveMQUuidGenerator.generateSanitizedId(path); + String id; + if (uriTemplate != null) { + id = ActiveMQUuidGenerator.generateSanitizedId(basePath + uriTemplate); + } else { + id = ActiveMQUuidGenerator.generateSanitizedId(basePath); + } // remove leading dash as we add that ourselves if (id.startsWith("-")) { id = id.substring(1); http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java index 757264d..48f4441 100644 --- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java +++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java @@ -46,6 +46,7 @@ import org.apache.camel.spi.ManagementStrategy; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RestConsumerFactory; import org.apache.camel.util.FileUtil; +import org.apache.camel.util.HostUtils; import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; @@ -940,18 +941,27 @@ public class JettyHttpComponent extends HttpComponent implements RestConsumerFac @Override - public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String path, + public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, Map<String, Object> parameters) throws Exception { + String path = basePath; + if (uriTemplate != null) { + // make sure to avoid double slashes + if (uriTemplate.startsWith("/")) { + path = path + uriTemplate; + } else { + path = path + "/" + uriTemplate; + } + } path = FileUtil.stripLeadingSeparator(path); String scheme = "http"; - String host = "0.0.0.0"; + String host = ""; int port = 0; // if no explicit port/host configured, then use port from rest configuration RestConfiguration config = getCamelContext().getRestConfiguration(); - if (config != null && (config.getComponent() == null || config.getComponent().equals("jetty"))) { + if (config.getComponent() == null || config.getComponent().equals("jetty")) { if (config.getScheme() != null) { scheme = config.getScheme(); } @@ -964,6 +974,15 @@ public class JettyHttpComponent extends HttpComponent implements RestConsumerFac } } + // if no explicit hostname set then resolve the hostname + if (ObjectHelper.isEmpty(host)) { + if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) { + host = HostUtils.getLocalHostName(); + } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) { + host = HostUtils.getLocalIp(); + } + } + Map<String, Object> map = new HashMap<String, Object>(); // build query string, and append any endpoint configuration properties if (config != null && (config.getComponent() == null || config.getComponent().equals("jetty"))) { http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java index 90f09f1..cff2f1e 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java @@ -34,7 +34,9 @@ import org.apache.camel.spi.HeaderFilterStrategyAware; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RestConsumerFactory; import org.apache.camel.util.FileUtil; +import org.apache.camel.util.HostUtils; import org.apache.camel.util.IntrospectionSupport; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.ServiceHelper; import org.apache.camel.util.URISupport; import org.apache.camel.util.UnsafeUriCharactersEncoder; @@ -213,18 +215,27 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt } @Override - public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String path, + public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, Map<String, Object> parameters) throws Exception { + String path = basePath; + if (uriTemplate != null) { + // make sure to avoid double slashes + if (uriTemplate.startsWith("/")) { + path = path + uriTemplate; + } else { + path = path + "/" + uriTemplate; + } + } path = FileUtil.stripLeadingSeparator(path); String scheme = "http"; - String host = "0.0.0.0"; + String host = ""; int port = 0; // if no explicit port/host configured, then use port from rest configuration RestConfiguration config = getCamelContext().getRestConfiguration(); - if (config != null && (config.getComponent() == null || config.getComponent().equals("netty-http"))) { + if (config.getComponent() == null || config.getComponent().equals("netty-http")) { if (config.getScheme() != null) { scheme = config.getScheme(); } @@ -235,6 +246,15 @@ public class NettyHttpComponent extends NettyComponent implements HeaderFilterSt if (num > 0) { port = num; } + } + + // if no explicit hostname set then resolve the hostname + if (ObjectHelper.isEmpty(host)) { + if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) { + host = HostUtils.getLocalHostName(); + } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) { + host = HostUtils.getLocalIp(); + } } Map<String, Object> map = new HashMap<String, Object>(); http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java index ac90fde..6681f13 100644 --- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java +++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java @@ -30,6 +30,8 @@ import org.apache.camel.impl.HeaderFilterStrategyComponent; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RestConsumerFactory; import org.apache.camel.util.FileUtil; +import org.apache.camel.util.HostUtils; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.URISupport; import org.apache.camel.util.UnsafeUriCharactersEncoder; import org.restlet.Component; @@ -501,18 +503,27 @@ public class RestletComponent extends HeaderFilterStrategyComponent implements R } @Override - public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String path, + public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, Map<String, Object> parameters) throws Exception { + String path = basePath; + if (uriTemplate != null) { + // make sure to avoid double slashes + if (uriTemplate.startsWith("/")) { + path = path + uriTemplate; + } else { + path = path + "/" + uriTemplate; + } + } path = FileUtil.stripLeadingSeparator(path); String scheme = "http"; - String host = "0.0.0.0"; + String host = ""; int port = 0; // if no explicit port/host configured, then use port from rest configuration RestConfiguration config = getCamelContext().getRestConfiguration(); - if (config != null && (config.getComponent() == null || config.getComponent().equals("restlet"))) { + if (config.getComponent() == null || config.getComponent().equals("restlet")) { if (config.getScheme() != null) { scheme = config.getScheme(); } @@ -525,6 +536,15 @@ public class RestletComponent extends HeaderFilterStrategyComponent implements R } } + // if no explicit hostname set then resolve the hostname + if (ObjectHelper.isEmpty(host)) { + if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) { + host = HostUtils.getLocalHostName(); + } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) { + host = HostUtils.getLocalIp(); + } + } + Map<String, Object> map = new HashMap<String, Object>(); // build query string, and append any endpoint configuration properties if (config != null && (config.getComponent() == null || config.getComponent().equals("restlet"))) { http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/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 0fbe4d6..2dd57ac 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 @@ -163,10 +163,18 @@ public class ServletComponent extends HttpComponent implements RestConsumerFacto this.httpRegistry = httpRegistry; } - @Override - public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String path, + public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, Map<String, Object> parameters) throws Exception { + String path = basePath; + if (uriTemplate != null) { + // make sure to avoid double slashes + if (uriTemplate.startsWith("/")) { + path = path + uriTemplate; + } else { + path = path + "/" + uriTemplate; + } + } path = FileUtil.stripLeadingSeparator(path); // if no explicit port/host configured, then use port from rest configuration @@ -174,7 +182,7 @@ public class ServletComponent extends HttpComponent implements RestConsumerFacto Map<String, Object> map = new HashMap<String, Object>(); // build query string, and append any endpoint configuration properties - if (config != null && (config.getComponent() == null || config.getComponent().equals("servlet"))) { + if (config.getComponent() == null || config.getComponent().equals("servlet")) { // setup endpoint options if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) { map.putAll(config.getEndpointProperties()); @@ -183,7 +191,6 @@ public class ServletComponent extends HttpComponent implements RestConsumerFacto String query = URISupport.createQueryString(map); - // servlet:///hello String url = "servlet:///%s?httpMethodRestrict=%s"; if (!query.isEmpty()) { url = url + "?" + query; http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java b/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java index c3827ff..2dafa9d 100644 --- a/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java +++ b/components/camel-spark-rest/src/main/java/org/apache/camel/component/sparkrest/SparkComponent.java @@ -28,6 +28,7 @@ import org.apache.camel.Processor; import org.apache.camel.impl.UriEndpointComponent; 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 spark.Spark; @@ -98,7 +99,7 @@ public class SparkComponent extends UriEndpointComponent implements RestConsumer } else { // if no explicit port configured, then use port from rest configuration RestConfiguration config = getCamelContext().getRestConfiguration(); - if (config != null && (config.getComponent() == null || config.getComponent().equals("spark-rest"))) { + if (config.getComponent() == null || config.getComponent().equals("spark-rest")) { int port = config.getPort(); if (port > 0) { Spark.setPort(port); @@ -108,7 +109,7 @@ public class SparkComponent extends UriEndpointComponent implements RestConsumer // configure component options RestConfiguration config = getCamelContext().getRestConfiguration(); - if (config != null && (config.getComponent() == null || config.getComponent().equals("spark-rest"))) { + if (config.getComponent() == null || config.getComponent().equals("spark-rest")) { // configure additional options on spark configuration if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) { setProperties(sparkConfiguration, config.getComponentProperties()); @@ -123,9 +124,20 @@ public class SparkComponent extends UriEndpointComponent implements RestConsumer } @Override - public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String path, + public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, Map<String, Object> parameters) throws Exception { + String path = basePath; + if (uriTemplate != null) { + // make sure to avoid double slashes + if (uriTemplate.startsWith("/")) { + path = path + uriTemplate; + } else { + path = path + "/" + uriTemplate; + } + } + path = FileUtil.stripLeadingSeparator(path); + if (ObjectHelper.isNotEmpty(path)) { // spark-rest uses :name syntax instead of {name} so we need to replace those Matcher matcher = pattern.matcher(path); @@ -141,7 +153,7 @@ public class SparkComponent extends UriEndpointComponent implements RestConsumer // build query string, and append any endpoint configuration properties RestConfiguration config = getCamelContext().getRestConfiguration(); - if (config != null && (config.getComponent() == null || config.getComponent().equals("spark-rest"))) { + if (config.getComponent() == null || config.getComponent().equals("spark-rest")) { // setup endpoint options if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) { map.putAll(config.getEndpointProperties()); http://git-wip-us.apache.org/repos/asf/camel/blob/e239c0f2/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/component/rest/DummyRestConsumerFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/component/rest/DummyRestConsumerFactory.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/component/rest/DummyRestConsumerFactory.java index ad5c9e9..c87fae5 100644 --- a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/component/rest/DummyRestConsumerFactory.java +++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/component/rest/DummyRestConsumerFactory.java @@ -28,10 +28,15 @@ import org.apache.camel.spi.RestConsumerFactory; public class DummyRestConsumerFactory implements RestConsumerFactory { @Override - public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String path, + public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String path, String uriTemplate, String consumes, String produces, Map<String, Object> parameters) throws Exception { // just use a seda endpoint for testing purpose - String id = ActiveMQUuidGenerator.generateSanitizedId(path); + String id; + if (uriTemplate != null) { + id = ActiveMQUuidGenerator.generateSanitizedId(path + uriTemplate); + } else { + id = ActiveMQUuidGenerator.generateSanitizedId(path); + } // remove leading dash as we add that ourselves if (id.startsWith("-")) { id = id.substring(1);