You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/05/17 10:38:57 UTC

[2/6] camel git commit: adding swagger custom paramater definition

adding swagger custom paramater definition


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

Branch: refs/heads/master
Commit: 96ce5691b954b5596a075fcb6625e6c9995d1a9b
Parents: bb4d11d
Author: sebi <se...@softvision.ro>
Authored: Wed May 13 19:30:57 2015 +0300
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun May 17 10:04:53 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/model/rest/RestDefinition.java |  22 ++--
 .../camel/model/rest/RestOperationParam.java    | 120 +++++++++++++++++++
 .../camel/model/rest/RestParamDefinition.java   |  76 ++++++++++++
 .../apache/camel/model/rest/VerbDefinition.java |  56 +++++++--
 .../component/swagger/RestSwaggerReader.scala   |  53 +++-----
 5 files changed, 272 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/96ce5691/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 82633ca..e03b2e8 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
@@ -5,9 +5,9 @@
  * 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
- *
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
  * 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.
@@ -52,7 +52,8 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
     @XmlAttribute
     private String produces;
 
-    @XmlAttribute @Metadata(defaultValue = "auto")
+    @XmlAttribute
+    @Metadata(defaultValue = "auto")
     private RestBindingMode bindingMode;
 
     @XmlAttribute
@@ -128,7 +129,7 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
     public void setVerbs(List<VerbDefinition> verbs) {
         this.verbs = verbs;
     }
-   
+
     public Boolean getSkipBindingOnErrorCode() {
         return skipBindingOnErrorCode;
     }
@@ -267,6 +268,14 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
         return this;
     }
 
+    public RestParamDefinition restParam() {
+        if (getVerbs().isEmpty()) {
+            throw new IllegalArgumentException("Must add verb first, such as get/post/delete");
+        }
+        VerbDefinition verb = getVerbs().get(getVerbs().size() - 1);
+        return new RestParamDefinition(verb);
+    }
+
     public RestDefinition produces(String mediaType) {
         if (getVerbs().isEmpty()) {
             this.produces = mediaType;
@@ -416,10 +425,9 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
             answer = new VerbDefinition();
             answer.setMethod(verb);
         }
-
+        getVerbs().add(answer);
         answer.setRest(this);
         answer.setUri(uri);
-        getVerbs().add(answer);
         return this;
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/96ce5691/camel-core/src/main/java/org/apache/camel/model/rest/RestOperationParam.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestOperationParam.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestOperationParam.java
new file mode 100644
index 0000000..258e26c
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestOperationParam.java
@@ -0,0 +1,120 @@
+package org.apache.camel.model.rest;
+
+import org.apache.camel.spi.Metadata;
+
+import javax.xml.bind.annotation.*;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by seb on 5/13/15.
+ */
+@Metadata(label = "rest")
+@XmlRootElement(name = "param")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class RestOperationParam {
+    @XmlAttribute
+    String paramType="query";
+
+    @XmlAttribute
+    String name;
+
+    @XmlAttribute
+    String description="";
+
+    @XmlAttribute
+    String defaultValue="";
+
+    @XmlAttribute
+    Boolean required=true;
+
+    @XmlAttribute
+    Boolean allowMultiple=false;
+
+    @XmlAttribute
+    String dataType="string";
+
+    @XmlElementWrapper( name="allowableValues" )
+    @XmlElement( name="value" )
+    List<String> allowableValues=new ArrayList<String>();
+
+    @XmlAttribute
+    String paramAccess=null;
+
+
+    public RestOperationParam() {
+    
+    }
+
+    public String getParamType() {
+        return paramType;
+    }
+
+    public void setParamType(String paramType) {
+        this.paramType = paramType;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public String getDefaultValue() {
+        return defaultValue;
+    }
+
+    public void setDefaultValue(String defaultValue) {
+        this.defaultValue = defaultValue;
+    }
+
+    public Boolean getRequired() {
+        return required;
+    }
+
+    public void setRequired(Boolean required) {
+        this.required = required;
+    }
+
+    public Boolean getAllowMultiple() {
+        return allowMultiple;
+    }
+
+    public void setAllowMultiple(Boolean allowMultiple) {
+        this.allowMultiple = allowMultiple;
+    }
+
+    public String getDataType() {
+        return dataType;
+    }
+
+    public void setDataType(String dataType) {
+        this.dataType = dataType;
+    }
+
+    public List<String> getAllowableValues() {
+        return allowableValues;
+    }
+
+    public void setAllowableValues(List<String> allowableValues) {
+        this.allowableValues = allowableValues;
+    }
+
+    public String getParamAccess() {
+        return paramAccess;
+    }
+
+    public void setParamAccess(String paramAccess) {
+        this.paramAccess = paramAccess;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/96ce5691/camel-core/src/main/java/org/apache/camel/model/rest/RestParamDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestParamDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestParamDefinition.java
new file mode 100644
index 0000000..b4a318a
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestParamDefinition.java
@@ -0,0 +1,76 @@
+package org.apache.camel.model.rest;
+
+import org.apache.camel.model.OptionalIdentifiedDefinition;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by seb on 5/14/15.
+ */
+public class RestParamDefinition extends OptionalIdentifiedDefinition<RestParamDefinition> {
+
+    private RestOperationParam parameter = new RestOperationParam();
+    private VerbDefinition verb;
+
+    public RestParamDefinition(VerbDefinition verb) {
+        this.verb = verb;
+    }
+
+    @Override
+    public String getLabel() {
+        return "param";
+    }
+
+
+    public RestParamDefinition name(String name) {
+        parameter.setName(name);
+        return this;
+    }
+
+    public RestParamDefinition description(String name) {
+        parameter.setDescription(name);
+        return this;
+    }
+
+    public RestParamDefinition defaultValue(String name) {
+        parameter.setDefaultValue(name);
+        return this;
+    }
+
+    public RestParamDefinition required(Boolean required) {
+        parameter.setRequired(required);
+        return this;
+    }
+
+    public RestParamDefinition allowMultiple(Boolean allowMultiple) {
+        parameter.setAllowMultiple(allowMultiple);
+        return this;
+    }
+
+    public RestParamDefinition dataType(String type) {
+        parameter.setDataType(type);
+        return this;
+    }
+
+    public RestParamDefinition allowableValues(List<String> allowableValues) {
+        parameter.setAllowableValues(allowableValues);
+        return this;
+    }
+
+    public RestParamDefinition type(String type) {
+        parameter.setParamType(type);
+        return this;
+    }
+
+    public RestParamDefinition paramAccess(String paramAccess) {
+        parameter.setParamAccess(paramAccess);
+        return this;
+    }
+
+    public RestDefinition endParam() {
+        verb.getParams().add(parameter);
+        return verb.getRest();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/96ce5691/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 df053fd..84a71f6 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
@@ -5,9 +5,9 @@
  * 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
- *
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
  * 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.
@@ -16,18 +16,16 @@
  */
 package org.apache.camel.model.rest;
 
-import javax.xml.bind.annotation.XmlAccessType;
-import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlAttribute;
-import javax.xml.bind.annotation.XmlElement;
-import javax.xml.bind.annotation.XmlElements;
-import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.annotation.XmlTransient;
+import javax.xml.bind.annotation.*;
 
 import org.apache.camel.model.OptionalIdentifiedDefinition;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.ToDefinition;
 import org.apache.camel.spi.Metadata;
+import org.apache.camel.util.FileUtil;
+
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Rest command
@@ -40,6 +38,9 @@ public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition>
     @XmlAttribute
     private String method;
 
+    @XmlElementRef
+    private List<RestOperationParam> params = new ArrayList<RestOperationParam>();
+
     @XmlAttribute
     private String uri;
 
@@ -49,7 +50,8 @@ public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition>
     @XmlAttribute
     private String produces;
 
-    @XmlAttribute @Metadata(defaultValue = "auto")
+    @XmlAttribute
+    @Metadata(defaultValue = "auto")
     private RestBindingMode bindingMode;
 
     @XmlAttribute
@@ -89,6 +91,10 @@ public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition>
         }
     }
 
+    public List<RestOperationParam> getParams() {
+        return params;
+    }
+
     public String getMethod() {
         return method;
     }
@@ -109,6 +115,29 @@ public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition>
      */
     public void setUri(String uri) {
         this.uri = uri;
+        String path = this.rest.getPath();
+
+        String s1 = FileUtil.stripTrailingSeparator(path);
+        String s2 = FileUtil.stripLeadingSeparator(uri);
+        String allPath;
+        if (s1 != null && s2 != null) {
+            allPath = s1 + "/" + s2;
+        } else if (path != null) {
+            allPath = path;
+        } else {
+            allPath = uri;
+        }
+
+        // each {} is a parameter
+        String[] arr = allPath.split("\\/");
+        for (String a: arr) {
+            if (a.startsWith("{") && a.endsWith("}")) {
+                String key = a.substring(1, a.length() - 1);
+                rest.restParam().name(key).type("path").endParam();
+            }
+        }
+
+
     }
 
     public String getConsumes() {
@@ -186,6 +215,11 @@ public class VerbDefinition extends OptionalIdentifiedDefinition<VerbDefinition>
      */
     public void setType(String type) {
         this.type = type;
+        String bodyType = type;
+        if (type.endsWith("[]")) {
+            bodyType = "List[" + bodyType.substring(0, type.length() - 2) + "]";
+        }
+        rest.restParam().name("body").type("body").dataType(bodyType).endParam();
     }
 
     public String getOutType() {

http://git-wip-us.apache.org/repos/asf/camel/blob/96ce5691/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala
----------------------------------------------------------------------
diff --git a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala
index ca5bff2..6e73d7c 100644
--- a/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala
+++ b/components/camel-swagger/src/main/scala/org/apache/camel/component/swagger/RestSwaggerReader.scala
@@ -23,7 +23,7 @@ import com.wordnik.swagger.model._
 import com.wordnik.swagger.core.util.ModelUtil
 import com.wordnik.swagger.core.SwaggerSpec
 
-import org.apache.camel.model.rest.{VerbDefinition, RestDefinition}
+import org.apache.camel.model.rest.{RestOperationParam, VerbDefinition, RestDefinition}
 import org.apache.camel.util.FileUtil
 import org.slf4j.LoggerFactory
 
@@ -120,7 +120,7 @@ class RestSwaggerReader {
         consumes,
         List(),
         List(),
-        createParameters(verb, buildUrl(resourcePath, path)),
+        createParameters(verb),
         List(),
         None)
     }
@@ -173,46 +173,25 @@ class RestSwaggerReader {
     else None
   }
 
-  def createParameters(verb: VerbDefinition, absPath : String): List[Parameter] = {
+  def createParameters(verb: VerbDefinition): List[Parameter] = {
     val parameters = new ListBuffer[Parameter]
 
-    // each {} is a parameter
-    val arr = absPath.split("\\/")
-    for (a <- arr) {
-      if (a.startsWith("{") && a.endsWith("}")) {
-        var key = a.substring(1, a.length - 1)
-
-        parameters += Parameter(
-          key,
-          None,
-          None,
-          true,
-          false,
-          "string",
-          AnyAllowableValues,
-          "path",
-          None
-        )
-      }
-    }
+    for (param:RestOperationParam <- verb.getParams.asScala) {
+      var allowValues=AnyAllowableValues
 
-    // if we have input type then its a body parameter
-    if (verb.getType != null) {
-      var bodyType = verb.getType
-      if (bodyType.endsWith("[]")) {
-        bodyType = "List[" + bodyType.substring(0, bodyType.length - 2) + "]"
+      if(!param.getAllowableValues.isEmpty){
+        AllowableListValues(param.getAllowableValues.asScala.toList)
       }
-
       parameters += Parameter(
-        "body",
-        None,
-        None,
-        true,
-        false,
-        bodyType,
-        AnyAllowableValues,
-        "body",
-        None
+        param.getName,
+        Some( param.getDescription ),
+        Some( param.getDefaultValue),
+        param.getRequired.booleanValue(),
+        param.getAllowMultiple.booleanValue(),
+        param.getDataType,
+        allowValues,
+        param.getParamType,
+        Some(param.getParamAccess)
       )
     }