cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5989] Updating WadlGenerator to check BeanParams
Date Fri, 19 Sep 2014 12:39:55 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes ab17a75be -> a876a3a57


[CXF-5989] Updating WadlGenerator to check BeanParams


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

Branch: refs/heads/2.7.x-fixes
Commit: a876a3a576e15cbe5cff48fdc476559ff66e3c9a
Parents: ab17a75
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri Sep 19 13:39:20 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri Sep 19 13:39:20 2014 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/model/wadl/WadlGenerator.java     | 89 +++++++++++++++++++-
 .../apache/cxf/jaxrs/model/wadl/BookStore.java  | 23 ++++-
 .../cxf/jaxrs/model/wadl/WadlGeneratorTest.java |  9 +-
 3 files changed, 115 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/a876a3a5/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
index 2856519..67305b5 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
@@ -47,6 +47,13 @@ import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.logging.Logger;
 
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.Encoded;
+import javax.ws.rs.FormParam;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.MatrixParam;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
@@ -126,6 +133,15 @@ public class WadlGenerator implements RequestHandler {
     private static final String JAXB_DEFAULT_NAME = "##default";
     private static final String CLASSPATH_PREFIX = "classpath:";
     private static final String DEFAULT_NS_PREFIX = "prefix";
+    private static final Map<ParameterType, Class<? extends Annotation>> PARAMETER_TYPE_MAP;
+    static {
+        PARAMETER_TYPE_MAP = new HashMap<ParameterType, Class<? extends Annotation>>();
+        PARAMETER_TYPE_MAP.put(ParameterType.FORM, FormParam.class);
+        PARAMETER_TYPE_MAP.put(ParameterType.QUERY, QueryParam.class);
+        PARAMETER_TYPE_MAP.put(ParameterType.HEADER, HeaderParam.class);
+        PARAMETER_TYPE_MAP.put(ParameterType.PATH, PathParam.class);
+        PARAMETER_TYPE_MAP.put(ParameterType.MATRIX, MatrixParam.class);
+    }
     
     private String wadlNamespace;
     private boolean ignoreMessageWriters = true;
@@ -495,6 +511,8 @@ public class WadlGenerator implements RequestHandler {
             boolean isForm = isFormRequest(ori);
 
             doHandleClassParams(sb, classParams, isJson, ParameterType.QUERY, ParameterType.HEADER);
+            doHandleJaxrsBeanParamClassParams(sb, classParams, isJson, 
+                                              ParameterType.QUERY, ParameterType.HEADER);
             for (Parameter p : ori.getParameters()) {
                 if (isForm && p.getType() == ParameterType.REQUEST_BODY) {
                     continue;
@@ -587,6 +605,7 @@ public class WadlGenerator implements RequestHandler {
                                                   boolean isJson) {
         doHandleClassParams(sb, params, isJson, ParameterType.PATH);
         doHandleClassParams(sb, params, isJson, ParameterType.MATRIX);
+        doHandleJaxrsBeanParamClassParams(sb, params, isJson, ParameterType.PATH, ParameterType.MATRIX);
     }
 
     protected void doHandleClassParams(StringBuilder sb, Map<Parameter, Object> params,
boolean isJson,
@@ -606,10 +625,26 @@ public class WadlGenerator implements RequestHandler {
             }
         }
     }
+    
+    protected void doHandleJaxrsBeanParamClassParams(StringBuilder sb, 
+                                       Map<Parameter, Object> params, 
+                                       boolean isJson,
+                                       ParameterType... pType) {
+        for (Map.Entry<Parameter, Object> entry : params.entrySet()) {
+            Parameter pm = entry.getKey();
+            Object obj = entry.getValue();
+            if (pm.getType() == ParameterType.BEAN) {
+                Class<?> cls = obj instanceof Method ? ((Method)obj).getParameterTypes()[0]
: ((Field)obj)
+                    .getType();
+                doWriteJaxrsBeanParam(sb, cls, isJson, pType);
+            }
+        }
+    } 
 
     protected void handlePathAndMatrixParams(StringBuilder sb, OperationResourceInfo ori,
boolean isJson) {
         handleParams(sb, ori, ParameterType.PATH, isJson);
         handleParams(sb, ori, ParameterType.MATRIX, isJson);
+        doWriteJaxrsBeanParams(sb, ori, isJson, ParameterType.PATH, ParameterType.MATRIX);
     }
 
     protected void handleParameter(StringBuilder sb, Set<Class<?>> jaxbTypes,
@@ -626,7 +661,9 @@ public class WadlGenerator implements RequestHandler {
         if (pm.getType() == ParameterType.HEADER || pm.getType() == ParameterType.QUERY)
{
             writeParam(sb, pm, ori, isJson);
         }
-
+        if (pm.getType() == ParameterType.BEAN) {
+            doWriteJaxrsBeanParams(sb, ori, isJson, ParameterType.HEADER, ParameterType.QUERY);
+        }
     }
 
     protected void handleParams(StringBuilder sb, OperationResourceInfo ori, ParameterType
type,
@@ -665,6 +702,54 @@ public class WadlGenerator implements RequestHandler {
         }
     }
 
+    private void doWriteJaxrsBeanParams(StringBuilder sb, 
+                                       OperationResourceInfo ori, 
+                                       boolean isJson,
+                                       ParameterType ...parameterTypes) {
+        for (Parameter p : ori.getParameters()) {
+            if (p.getType() == ParameterType.BEAN) {
+                Method method = getMethod(ori);
+                Class<?> type = method.getParameterTypes()[p.getIndex()];
+                doWriteJaxrsBeanParam(sb, type, isJson, parameterTypes);
+            }
+        }
+    }
+    
+    private void doWriteJaxrsBeanParam(StringBuilder sb, 
+                                       Class<?> beanType, 
+                                       boolean isJson,
+                                       ParameterType ...parameterTypes) {
+        for (Method m : beanType.getMethods()) {
+            if (m.getName().startsWith("set")) {
+                for (ParameterType parameterType : parameterTypes) {
+                    Class<? extends Annotation> annClass = getAnnotationFromParamType(parameterType);
+                    Annotation annotation = m.getAnnotation(annClass);
+                    if (annotation != null) {
+                        String propertyName = StringUtils.uncapitalize(m.getName().substring(3));
+                        Parameter pm = new Parameter(parameterType, propertyName);
+                        pm.setEncoded(m.getAnnotation(Encoded.class) != null);
+                        DefaultValue dv = m.getAnnotation(DefaultValue.class);
+                        if (dv != null) {
+                            pm.setDefaultValue(dv.value());
+                        }
+                        doWriteParam(sb,
+                                     pm, 
+                                     m.getParameterTypes()[0], 
+                                     m.getGenericParameterTypes()[0], 
+                                     propertyName,
+                                     new Annotation[]{}, 
+                                     isJson);
+                        
+                    }
+                }
+            }
+        }
+    }
+
+    private Class<? extends Annotation> getAnnotationFromParamType(ParameterType pt)
{
+        return PARAMETER_TYPE_MAP.get(pt);
+    }
+
     private void doWriteBeanParam(StringBuilder sb, Class<?> type, Parameter pm, String
parentName,
                                   List<Class<?>> parentBeanClasses, boolean isJson)
{
         Map<Parameter, Class<?>> pms = InjectionUtils.getParametersFromBeanClass(type,
pm.getType(), true);
@@ -863,7 +948,7 @@ public class WadlGenerator implements RequestHandler {
                 for (int i = 0; i < params.size(); i++) {
                     if (isFormParameter(params.get(i), getMethod(ori).getParameterTypes()[i],
getMethod(ori)
                         .getParameterAnnotations()[i])) {
-                        writeParam(sb, params.get(i), ori, false);
+                        writeParam(sb, params.get(i), ori, isJson);
                     }
                 }
                 sb.append("</representation>");

http://git-wip-us.apache.org/repos/asf/cxf/blob/a876a3a5/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/BookStore.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/BookStore.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/BookStore.java
index acd365a..9ce8ae5 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/BookStore.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/BookStore.java
@@ -21,6 +21,7 @@ package org.apache.cxf.jaxrs.model.wadl;
 import java.util.Collections;
 import java.util.List;
 
+import javax.ws.rs.BeanParam;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.CookieParam;
 import javax.ws.rs.DefaultValue;
@@ -80,7 +81,8 @@ public class BookStore implements BookDescription {
     
     @GET 
     @Produces("text/plain")
-    public String getName(@PathParam("id") Long id, @QueryParam("") QueryBean query) {
+    public String getName(@PathParam("id") Long id, @BeanParam TheBeanParam beanParam,
+                          @QueryParam("") QueryBean query) {
         return "store";
     }
     
@@ -170,6 +172,25 @@ public class BookStore implements BookDescription {
     public Book2 getBook2() {
         return new Book2();
     }
+
+    public static class TheBeanParam {
+        private int a;
+        private int b;
+        public int getA() {
+            return a;
+        }
+        @PathParam("a")
+        public void setA(int a) {
+            this.a = a;
+        }
+        public int getB() {
+            return b;
+        }
+        @QueryParam("b")
+        public void setB(int b) {
+            this.b = b;
+        }
+    }
     
     public static class QueryBean {
         private int a;

http://git-wip-us.apache.org/repos/asf/cxf/blob/a876a3a5/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
index 8343494..74ebed3 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
@@ -471,7 +471,9 @@ public class WadlGeneratorTest extends Assert {
         
         // verify root resource starting with "/"
         // must have a single template parameter
-        verifyParameters(resource, 1, new Param("id", "template", "xs:long"));
+        verifyParameters(resource, 2, 
+                         new Param("id", "template", "xs:long"),
+                         new Param("a", "template", "xs:int"));
         
         // must have 4 methods, 2 GETs, POST and PUT
         List<Element> methodEls = getElements(resource, "method", 4);
@@ -584,8 +586,9 @@ public class WadlGeneratorTest extends Assert {
         // check request 
         List<Element> requestEls = getElements(methodEl, "request", 1);
         
-        // 4 parameters are expected
-        verifyParameters(requestEls.get(0), 5, 
+        // 6 parameters are expected
+        verifyParameters(requestEls.get(0), 6, 
+                         new Param("b", "query", "xs:int"),
                          new Param("aProp", "query", "xs:int"),
                          new Param("c.a", "query", "xs:int"),
                          new Param("c.b", "query", "xs:int"),


Mime
View raw message