camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/3] git commit: CAMEL-7619: Rest DSL - adding support for xml/json binding using Camel's data formats. Work in progress.
Date Tue, 22 Jul 2014 18:55:51 GMT
CAMEL-7619: Rest DSL - adding support for xml/json binding using Camel's data formats. Work
in progress.


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

Branch: refs/heads/master
Commit: f41a7e4d5d3ac6445e7fefe380bf874fd7c9b2e8
Parents: 619b1b9
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Tue Jul 22 20:51:05 2014 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Tue Jul 22 20:55:34 2014 +0200

----------------------------------------------------------------------
 .../camel/model/rest/RestBindingDefinition.java | 44 +++++++++++---------
 .../apache/camel/model/rest/RestDefinition.java | 13 +++---
 .../apache/camel/model/rest/VerbDefinition.java | 20 ++++-----
 .../org/apache/camel/model/rest/jaxb.index      |  1 +
 .../RestRestletPostJsonJaxbPojoTest.java        |  2 +-
 .../restlet/RestRestletPostJsonPojoTest.java    |  2 +-
 .../restlet/RestRestletPostXmlJaxbPojoTest.java |  2 +-
 7 files changed, 46 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f41a7e4d/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
b/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
index f1ef7a8..fbb1d9c 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestBindingDefinition.java
@@ -30,6 +30,7 @@ import org.apache.camel.processor.binding.RestBindingProcessor;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.RouteContext;
 import org.apache.camel.util.IntrospectionSupport;
+import org.apache.camel.util.ObjectHelper;
 
 @XmlRootElement(name = "restBinding")
 @XmlAccessorType(XmlAccessType.FIELD)
@@ -42,10 +43,10 @@ public class RestBindingDefinition extends NoOutputDefinition {
     private String xmlDataFormat;
 
     @XmlAttribute
-    private String classType;
+    private String type;
 
     @XmlTransient
-    private Class<?> resolvedClassType;
+    private Class<?> classType;
 
     @Override
     public String toString() {
@@ -57,10 +58,15 @@ public class RestBindingDefinition extends NoOutputDefinition {
         return "rest";
     }
 
-    // TODO: allow to configure if json/jaxb is mandatory, or optional
+    // TODO: allow to configure if json/xml only or auto detect (now)
 
     @Override
     public Processor createProcessor(RouteContext routeContext) throws Exception {
+        // type must be set
+        if (ObjectHelper.isEmpty(type) && ObjectHelper.isEmpty(classType)) {
+            throw new IllegalArgumentException("Type must be configured on " + this);
+        }
+
         CamelContext context = routeContext.getCamelContext();
 
         // setup json data format
@@ -72,11 +78,11 @@ public class RestBindingDefinition extends NoOutputDefinition {
         if (json == null) {
             throw new IllegalArgumentException("DataFormat " + name + " not found.");
         }
-        if (resolvedClassType == null && classType != null) {
-            resolvedClassType = context.getClassResolver().resolveMandatoryClass(classType);
+        if (classType == null && type != null) {
+            classType = context.getClassResolver().resolveMandatoryClass(type);
         }
-        if (resolvedClassType != null) {
-            IntrospectionSupport.setProperty(context.getTypeConverter(), json, "unmarshalType",
resolvedClassType);
+        if (classType != null) {
+            IntrospectionSupport.setProperty(context.getTypeConverter(), json, "unmarshalType",
classType);
         }
         context.addService(json);
 
@@ -89,11 +95,11 @@ public class RestBindingDefinition extends NoOutputDefinition {
         if (jaxb == null) {
             throw new IllegalArgumentException("DataFormat " + name + " not found.");
         }
-        if (resolvedClassType == null && classType != null) {
-            resolvedClassType = context.getClassResolver().resolveMandatoryClass(classType);
+        if (classType == null && type != null) {
+            classType = context.getClassResolver().resolveMandatoryClass(type);
         }
-        if (resolvedClassType != null) {
-            JAXBContext jc = JAXBContext.newInstance(resolvedClassType);
+        if (classType != null) {
+            JAXBContext jc = JAXBContext.newInstance(classType);
             IntrospectionSupport.setProperty(context.getTypeConverter(), jaxb, "context",
jc);
         }
         context.addService(jaxb);
@@ -101,20 +107,20 @@ public class RestBindingDefinition extends NoOutputDefinition {
         return new RestBindingProcessor(json, jaxb);
     }
 
-    public String getClassType() {
-        return classType;
+    public String getType() {
+        return type;
     }
 
-    public void setClassType(String classType) {
-        this.classType = classType;
+    public void setType(String type) {
+        this.type = type;
     }
 
-    public Class<?> getResolvedClassType() {
-        return resolvedClassType;
+    public Class<?> getClassType() {
+        return classType;
     }
 
-    public void setResolvedClassType(Class<?> resolvedClassType) {
-        this.resolvedClassType = resolvedClassType;
+    public void setClassType(Class<?> classType) {
+        this.classType = classType;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/f41a7e4d/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 6dffdc7..ad2fcd2 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
@@ -131,25 +131,26 @@ public class RestDefinition {
         return this;
     }
 
-    public RestDefinition as(String classType) {
+    public RestDefinition type(String classType) {
         // 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.setClassType(classType);
+        verb.setType(classType);
         return this;
     }
 
-    public RestDefinition as(Class<?> classType) {
+    public RestDefinition type(Class<?> classType) {
         // 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.setResolvedClassType(classType);
+        verb.setClassType(classType);
+        verb.setType(classType.getCanonicalName());
         return this;
     }
 
@@ -245,10 +246,10 @@ public class RestDefinition {
             }
 
             // add the binding
-            if (verb.getClassType() != null || verb.getResolvedClassType() != null) {
+            if (verb.getType() != null || verb.getClassType() != null) {
                 RestBindingDefinition binding = new RestBindingDefinition();
+                binding.setType(verb.getType());
                 binding.setClassType(verb.getClassType());
-                binding.setResolvedClassType(verb.getResolvedClassType());
                 route.getOutputs().add(0, binding);
             }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/f41a7e4d/camel-core/src/main/java/org/apache/camel/model/rest/VerbDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/VerbDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/VerbDefinition.java
index cbcf3f0..a9d57e6 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/VerbDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/VerbDefinition.java
@@ -42,10 +42,10 @@ public class VerbDefinition {
     private String consumes;
 
     @XmlAttribute
-    private String classType;
+    private String type;
 
     @XmlTransient
-    private Class<?> resolvedClassType;
+    private Class<?> classType;
 
     // used by XML DSL to either select a <to> or <route>
     // so we need to use the common type OptionalIdentifiedDefinition
@@ -87,20 +87,20 @@ public class VerbDefinition {
         this.consumes = consumes;
     }
 
-    public String getClassType() {
-        return classType;
+    public String getType() {
+        return type;
     }
 
-    public void setClassType(String classType) {
-        this.classType = classType;
+    public void setType(String type) {
+        this.type = type;
     }
 
-    public Class<?> getResolvedClassType() {
-        return resolvedClassType;
+    public Class<?> getClassType() {
+        return classType;
     }
 
-    public void setResolvedClassType(Class<?> resolvedClassType) {
-        this.resolvedClassType = resolvedClassType;
+    public void setClassType(Class<?> classType) {
+        this.classType = classType;
     }
 
     public RestDefinition getRest() {

http://git-wip-us.apache.org/repos/asf/camel/blob/f41a7e4d/camel-core/src/main/resources/org/apache/camel/model/rest/jaxb.index
----------------------------------------------------------------------
diff --git a/camel-core/src/main/resources/org/apache/camel/model/rest/jaxb.index b/camel-core/src/main/resources/org/apache/camel/model/rest/jaxb.index
index 5a3e94b..66ead80 100644
--- a/camel-core/src/main/resources/org/apache/camel/model/rest/jaxb.index
+++ b/camel-core/src/main/resources/org/apache/camel/model/rest/jaxb.index
@@ -19,6 +19,7 @@ GetVerbDefinition
 HeadVerbDefinition
 PostVerbDefinition
 PutVerbDefinition
+RestBindingDefinition
 RestConfigurationDefinition
 RestDefinition
 RestPropertyDefinition

http://git-wip-us.apache.org/repos/asf/camel/blob/f41a7e4d/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonJaxbPojoTest.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonJaxbPojoTest.java
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonJaxbPojoTest.java
index ab60797..1135c78 100644
--- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonJaxbPojoTest.java
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonJaxbPojoTest.java
@@ -52,7 +52,7 @@ public class RestRestletPostJsonJaxbPojoTest extends RestletTestSupport
{
 
                 // use the rest DSL to define the rest services
                 rest("/users/")
-                    .post("new").as(UserJaxbPojo.class)
+                    .post("new").type(UserJaxbPojo.class)
                         .to("mock:input");
             }
         };

http://git-wip-us.apache.org/repos/asf/camel/blob/f41a7e4d/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoTest.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoTest.java
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoTest.java
index 846564b..2bcb379 100644
--- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoTest.java
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostJsonPojoTest.java
@@ -52,7 +52,7 @@ public class RestRestletPostJsonPojoTest extends RestletTestSupport {
 
                 // use the rest DSL to define the rest services
                 rest("/users/")
-                    .post("new").as(UserPojo.class)
+                    .post("new").type(UserPojo.class)
                         .to("mock:input");
             }
         };

http://git-wip-us.apache.org/repos/asf/camel/blob/f41a7e4d/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostXmlJaxbPojoTest.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostXmlJaxbPojoTest.java
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostXmlJaxbPojoTest.java
index 34b78e7..7a22443 100644
--- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostXmlJaxbPojoTest.java
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletPostXmlJaxbPojoTest.java
@@ -70,7 +70,7 @@ public class RestRestletPostXmlJaxbPojoTest extends RestletTestSupport {
 
                 // use the rest DSL to define the rest services
                 rest("/users/")
-                    .post("new").as(UserJaxbPojo.class)
+                    .post("new").type(UserJaxbPojo.class)
                         .to("mock:input");
             }
         };


Mime
View raw message