Repository: camel
Updated Branches:
  refs/heads/master 1f43d9cd7 -> 8dc8d59a6


CAMEL-9910: Rest DSL now automatic discover rest capable component and if there 
is only one on the classpath it uses that by default.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8dc8d59a
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8dc8d59a
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8dc8d59a

Branch: refs/heads/master
Commit: 8dc8d59a653175c76c30b25d701cbedd6ec8ca8a
Parents: 1f43d9c
Author: Claus Ibsen <davscl...@apache.org>
Authored: Thu Sep 15 17:02:42 2016 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Thu Sep 15 17:02:42 2016 +0200

----------------------------------------------------------------------
 .../camel/component/rest/RestEndpoint.java      | 46 ++++++++++++++
 ...RestRestletGetAutoDiscoverComponentTest.java | 65 ++++++++++++++++++++
 2 files changed, 111 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/8dc8d59a/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 29cde1d..22e3ece 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
@@ -46,6 +46,8 @@ import org.slf4j.LoggerFactory;
 @UriEndpoint(scheme = "rest", title = "REST", syntax = 
"rest:method:path:uriTemplate", label = "core,rest", lenientProperties = true)
 public class RestEndpoint extends DefaultEndpoint {
 
+    public static final String[] DEFAULT_REST_CONSUMER_COMPONENTS = new 
String[]{"coap", "netty-http", "netty4-http", "jetty", "restlet", "servlet", 
"spark-java", "undertow"};
+    public static final String[] DEFAULT_REST_PRODUCER_COMPONENTS = new 
String[]{"http", "http4", "netty4-http", "jetty", "restlet", "undertow"};
     public static final String DEFAULT_API_COMPONENT_NAME = "swagger";
     public static final String RESOURCE_PATH = 
"META-INF/services/org/apache/camel/rest/";
 
@@ -314,6 +316,28 @@ public class RestEndpoint extends DefaultEndpoint {
             }
         }
 
+        // no explicit factory found then try to see if we can find any of the 
default rest consumer components
+        // and there must only be exactly one so we safely can pick this one
+        if (factory == null) {
+            RestProducerFactory found = null;
+            String foundName = null;
+            for (String name : DEFAULT_REST_PRODUCER_COMPONENTS) {
+                Object comp = getCamelContext().getComponent(name, true);
+                if (comp != null && comp instanceof RestProducerFactory) {
+                    if (found == null) {
+                        found = (RestProducerFactory) comp;
+                        foundName = name;
+                    } else {
+                        throw new IllegalArgumentException("Multiple 
RestProducerFactory found on classpath. Configure explicit which component to 
use");
+                    }
+                }
+            }
+            if (found != null) {
+                LOG.debug("Auto discovered {} as RestProducerFactory", 
foundName);
+                factory = found;
+            }
+        }
+
         if (factory != null) {
             LOG.debug("Using RestProducerFactory: {}", factory);
 
@@ -381,6 +405,28 @@ public class RestEndpoint extends DefaultEndpoint {
             }
         }
 
+        // no explicit factory found then try to see if we can find any of the 
default rest consumer components
+        // and there must only be exactly one so we safely can pick this one
+        if (factory == null) {
+            RestConsumerFactory found = null;
+            String foundName = null;
+            for (String name : DEFAULT_REST_CONSUMER_COMPONENTS) {
+                Object comp = getCamelContext().getComponent(name, true);
+                if (comp != null && comp instanceof RestConsumerFactory) {
+                    if (found == null) {
+                        found = (RestConsumerFactory) comp;
+                        foundName = name;
+                    } else {
+                        throw new IllegalArgumentException("Multiple 
RestConsumerFactory found on classpath. Configure explicit which component to 
use");
+                    }
+                }
+            }
+            if (found != null) {
+                LOG.debug("Auto discovered {} as RestConsumerFactory", 
foundName);
+                factory = found;
+            }
+        }
+
         if (factory != null) {
             // if no explicit port/host configured, then use port from rest 
configuration
             String scheme = "http";

http://git-wip-us.apache.org/repos/asf/camel/blob/8dc8d59a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletGetAutoDiscoverComponentTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletGetAutoDiscoverComponentTest.java
 
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletGetAutoDiscoverComponentTest.java
new file mode 100644
index 0000000..05a3585
--- /dev/null
+++ 
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletGetAutoDiscoverComponentTest.java
@@ -0,0 +1,65 @@
+/**
+ * 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.restlet;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class RestRestletGetAutoDiscoverComponentTest extends 
RestletTestSupport {
+    
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myBinding", new DefaultRestletBinding());
+        return jndi;
+    }
+
+    @Test
+    public void testRestletProducerGet() throws Exception {
+        String out = template.requestBody("http://localhost:"; + portNum + 
"/users/123/basic", null, String.class);
+        assertEquals("123;Donald Duck", out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // configure to use restlet with the given port
+                
restConfiguration().port(portNum).endpointProperty("restletBinding", 
"#myBinding");
+
+                // use the rest DSL to define the rest services
+                rest("/users/")
+                    .get("{id}/basic")
+                        .route()
+                        .to("mock:input")
+                        .process(new Processor() {
+                            public void process(Exchange exchange) throws 
Exception {
+                                String id = exchange.getIn().getHeader("id", 
String.class);
+                                exchange.getOut().setBody(id + ";Donald Duck");
+                            }
+                        });
+            }
+        };
+    }
+}

Reply via email to