camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [04/33] git commit: CAMEL-7354: Migrating rest DSL to be separated from regular Camel routes.
Date Sun, 20 Jul 2014 06:53:10 GMT
CAMEL-7354: Migrating rest DSL to be separated from regular Camel routes.


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

Branch: refs/heads/master
Commit: a9856bb6bcfb2f324a4682d165d7448a6456f718
Parents: fe0f3f7
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Jul 2 11:52:24 2014 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Jul 19 09:38:47 2014 +0200

----------------------------------------------------------------------
 .../org/apache/camel/builder/RouteBuilder.java  | 27 ++++++++++
 .../apache/camel/model/rest/PathDefinition.java | 54 ++++++++++++++++++--
 .../apache/camel/model/rest/RestDefinition.java | 31 ++++++++++-
 .../camel/model/rest/RestsDefinition.java       | 17 ++++++
 .../component/restbinding/FromRestGetTest.java  | 14 +++--
 .../restbinding/SpringFromRestGetTest.xml       | 10 ++--
 6 files changed, 138 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a9856bb6/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
index e17dace..db96fea 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
@@ -31,6 +31,8 @@ import org.apache.camel.model.OnExceptionDefinition;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.RoutesDefinition;
 
+import org.apache.camel.model.rest.RestDefinition;
+import org.apache.camel.model.rest.RestsDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -43,6 +45,7 @@ import org.slf4j.LoggerFactory;
 public abstract class RouteBuilder extends BuilderSupport implements RoutesBuilder {
     protected Logger log = LoggerFactory.getLogger(getClass());
     private AtomicBoolean initialized = new AtomicBoolean(false);
+    private RestsDefinition restCollection = new RestsDefinition();
     private RoutesDefinition routeCollection = new RoutesDefinition();
 
     public RouteBuilder() {
@@ -69,6 +72,18 @@ public abstract class RouteBuilder extends BuilderSupport implements RoutesBuild
     public abstract void configure() throws Exception;
 
     /**
+     * Creates a new REST service
+     *
+     * @return the builder
+     */
+    public RestDefinition rest() {
+        getRestCollection().setCamelContext(getContext());
+        RestDefinition answer = getRestCollection().rest();
+        configureRest(answer);
+        return answer;
+    }
+
+    /**
      * Creates a new route from the given URI input
      *
      * @param uri  the from uri
@@ -337,6 +352,14 @@ public abstract class RouteBuilder extends BuilderSupport implements
RoutesBuild
         camelContext.addRouteDefinitions(getRouteCollection().getRoutes());
     }
 
+    public RestsDefinition getRestCollection() {
+        return restCollection;
+    }
+
+    public void setRestCollection(RestsDefinition restCollection) {
+        this.restCollection = restCollection;
+    }
+
     public void setRouteCollection(RoutesDefinition routeCollection) {
         this.routeCollection = routeCollection;
     }
@@ -354,6 +377,10 @@ public abstract class RouteBuilder extends BuilderSupport implements
RoutesBuild
         return new DefaultCamelContext();
     }
 
+    protected void configureRest(RestDefinition rest) {
+        // noop
+    }
+
     protected void configureRoute(RouteDefinition route) {
         route.setGroup(getClass().getName());
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/a9856bb6/camel-core/src/main/java/org/apache/camel/model/rest/PathDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/PathDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/PathDefinition.java
index e9c526b..39741cb 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/PathDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/PathDefinition.java
@@ -24,22 +24,24 @@ import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElementRef;
 import javax.xml.bind.annotation.XmlRootElement;
 
+import org.apache.camel.model.ToDefinition;
+
 @XmlRootElement(name = "path")
 @XmlAccessorType(XmlAccessType.FIELD)
 public class PathDefinition {
 
     @XmlAttribute
-    private String uri;
+    private String url;
 
     @XmlElementRef
     private List<VerbDefinition> verbs = new ArrayList<VerbDefinition>();
 
-    public String getUri() {
-        return uri;
+    public String getUrl() {
+        return url;
     }
 
-    public void setUri(String uri) {
-        this.uri = uri;
+    public void setUrl(String url) {
+        this.url = url;
     }
 
     public List<VerbDefinition> getVerbs() {
@@ -49,4 +51,46 @@ public class PathDefinition {
     public void setVerbs(List<VerbDefinition> verbs) {
         this.verbs = verbs;
     }
+
+    // Fluent API
+    //-------------------------------------------------------------------------
+
+    public PathDefinition get() {
+        return get(null);
+    }
+
+    public PathDefinition get(String url) {
+        GetVerbDefinition answer = new GetVerbDefinition();
+        getVerbs().add(answer);
+        if (url != null) {
+            answer.setUri(url);
+        }
+        return this;
+    }
+
+    public PathDefinition post() {
+        return post(null);
+    }
+
+    public PathDefinition post(String url) {
+        PostVerbDefinition answer = new PostVerbDefinition();
+        getVerbs().add(answer);
+        if (url != null) {
+            answer.setUri(url);
+        }
+        return this;
+    }
+
+    public PathDefinition to(String url) {
+        // add to last verb
+        if (getVerbs().isEmpty()) {
+            throw new IllegalArgumentException("Must add verb first, such as get/post/delete");
+        }
+
+        VerbDefinition verb = getVerbs().get(getVerbs().size() - 1);
+        verb.setTo(new ToDefinition(url));
+
+        return this;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a9856bb6/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 a44b181..3cbb67a 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
@@ -31,11 +31,28 @@ import javax.xml.bind.annotation.XmlRootElement;
 @XmlAccessorType(XmlAccessType.PROPERTY)
 public class RestDefinition {
 
-    @XmlAttribute
     private String component;
-
+    private String routeId;
     private List<PathDefinition> paths = new ArrayList<PathDefinition>();
 
+    public String getComponent() {
+        return component;
+    }
+
+    @XmlAttribute
+    public void setComponent(String component) {
+        this.component = component;
+    }
+
+    public String getRouteId() {
+        return routeId;
+    }
+
+    @XmlAttribute
+    public void setRouteId(String routeId) {
+        this.routeId = routeId;
+    }
+
     public List<PathDefinition> getPaths() {
         return paths;
     }
@@ -45,4 +62,14 @@ public class RestDefinition {
         this.paths = paths;
     }
 
+    // Fluent API
+    //-------------------------------------------------------------------------
+
+    public PathDefinition path(String url) {
+        PathDefinition answer = new PathDefinition();
+        getPaths().add(answer);
+        answer.setUrl(url);
+        return answer;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a9856bb6/camel-core/src/main/java/org/apache/camel/model/rest/RestsDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestsDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestsDefinition.java
index 8361c17..11aad02 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestsDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestsDefinition.java
@@ -77,5 +77,22 @@ public class RestsDefinition extends OptionalIdentifiedDefinition<RestsDefinitio
     // Fluent API
     //-------------------------------------------------------------------------
 
+    public RestDefinition rest() {
+        RestDefinition rest = createRest();
+        return rest(rest);
+
+    }
+
+    public RestDefinition rest(RestDefinition rest) {
+        getRests().add(rest);
+        return rest;
+    }
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+    protected RestDefinition createRest() {
+        RestDefinition rest = new RestDefinition();
+        return rest;
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a9856bb6/camel-core/src/test/java/org/apache/camel/component/restbinding/FromRestGetTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/restbinding/FromRestGetTest.java
b/camel-core/src/test/java/org/apache/camel/component/restbinding/FromRestGetTest.java
index 5b11cb5..b9baeb8 100644
--- a/camel-core/src/test/java/org/apache/camel/component/restbinding/FromRestGetTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/restbinding/FromRestGetTest.java
@@ -59,9 +59,17 @@ public class FromRestGetTest extends ContextTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-//                rest()
-//                    .get("/hello").to("mock:foo")
-//                    .get("/bye").to("mock:bar");
+                rest()
+                    .path("/say")
+                        .get("/hello").to("direct:hello")
+                        .get("/bye").to("direct:bye")
+                        .post().to("seda:update");
+
+                from("direct:hello")
+                    .transform().constant("Hello World");
+
+                from("direct:bye")
+                    .transform().constant("Bye World");
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/a9856bb6/components/camel-spring/src/test/resources/org/apache/camel/component/restbinding/SpringFromRestGetTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/restbinding/SpringFromRestGetTest.xml
b/components/camel-spring/src/test/resources/org/apache/camel/component/restbinding/SpringFromRestGetTest.xml
index 7a9242d..d238f43 100644
--- a/components/camel-spring/src/test/resources/org/apache/camel/component/restbinding/SpringFromRestGetTest.xml
+++ b/components/camel-spring/src/test/resources/org/apache/camel/component/restbinding/SpringFromRestGetTest.xml
@@ -27,7 +27,7 @@
 
   <camelContext xmlns="http://camel.apache.org/schema/spring">
 
-    <rest component="dummy-rest">
+    <rest component="dummy-rest" routeId="myRestRoute">
       <path uri="/say">
         <get uri="/hello">
           <to uri="direct:hello"/>
@@ -41,14 +41,14 @@
       </path>
     </rest>
 
-    <route>
-      <to uri="direct:hello"/>
+    <route id="helloRoute">
+      <from uri="direct:hello"/>
       <transform>
         <constant>Hello World</constant>
       </transform>
     </route>
-    <route>
-      <to uri="direct:bye"/>
+    <route id="byeRoute">
+      <from uri="direct:bye"/>
       <transform>
         <constant>Bye World</constant>
       </transform>


Mime
View raw message