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:06:29 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 8adaa1d2b -> e530194ee


[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/e530194e
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/e530194e
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/e530194e

Branch: refs/heads/3.0.x-fixes
Commit: e530194ee96bf43c1db4b4215a4e2cc80c7d987c
Parents: 8adaa1d
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri Sep 19 13:04:15 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri Sep 19 13:06:06 2014 +0100

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


http://git-wip-us.apache.org/repos/asf/cxf/blob/e530194e/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
index b376bbe..4c15480 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
+++ b/rt/rs/description/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.container.ContainerRequestContext;
 import javax.ws.rs.container.ContainerRequestFilter;
 import javax.ws.rs.core.HttpHeaders;
@@ -125,6 +132,15 @@ public class WadlGenerator implements ContainerRequestFilter {
     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;
@@ -520,6 +536,8 @@ public class WadlGenerator implements ContainerRequestFilter {
             boolean isForm = isFormRequest(ori);
 
             doHandleClassParams(ori, sb, classParams, isJson, ParameterType.QUERY, ParameterType.HEADER);
+            doHandleJaxrsBeanParamClassParams(ori, sb, classParams, isJson, 
+                                              ParameterType.QUERY, ParameterType.HEADER);
             for (Parameter p : ori.getParameters()) {
                 if (isForm && p.getType() == ParameterType.REQUEST_BODY) {
                     continue;
@@ -614,6 +632,7 @@ public class WadlGenerator implements ContainerRequestFilter {
                                                   boolean isJson) {
         doHandleClassParams(ori, sb, params, isJson, ParameterType.PATH);
         doHandleClassParams(ori, sb, params, isJson, ParameterType.MATRIX);
+        doHandleJaxrsBeanParamClassParams(ori, sb, params, isJson, ParameterType.PATH, ParameterType.MATRIX);
     }
 
     protected void doHandleClassParams(OperationResourceInfo ori,
@@ -636,10 +655,26 @@ public class WadlGenerator implements ContainerRequestFilter {
             }
         }
     }
+    protected void doHandleJaxrsBeanParamClassParams(OperationResourceInfo ori,
+                                       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, ori, 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,
@@ -655,6 +690,11 @@ public class WadlGenerator implements ContainerRequestFilter {
         }
         if (pm.getType() == ParameterType.HEADER || pm.getType() == ParameterType.QUERY)
{
             writeParam(sb, pm, ori, isJson);
+            return;
+            
+        }
+        if (pm.getType() == ParameterType.BEAN) {
+            doWriteJaxrsBeanParams(sb, ori, isJson, ParameterType.HEADER, ParameterType.QUERY);
         }
 
     }
@@ -698,8 +738,56 @@ public class WadlGenerator implements ContainerRequestFilter {
             List<Class<?>> parentBeanClasses = new LinkedList<Class<?>>();
             parentBeanClasses.add(type);
             doWriteBeanParam(ori, sb, type, pm, null, parentBeanClasses, isJson);
+        } 
+    }
+    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, ori, type, isJson, parameterTypes);
+            }
+        }
+    }
+    
+    private void doWriteJaxrsBeanParam(StringBuilder sb, 
+                                       OperationResourceInfo ori, 
+                                       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(ori,
+                                     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(OperationResourceInfo ori,
                                   StringBuilder sb, 
@@ -927,7 +1015,7 @@ public class WadlGenerator implements ContainerRequestFilter {
                 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/e530194e/rt/rs/description/src/test/java/org/apache/cxf/jaxrs/model/wadl/BookStore.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/test/java/org/apache/cxf/jaxrs/model/wadl/BookStore.java
b/rt/rs/description/src/test/java/org/apache/cxf/jaxrs/model/wadl/BookStore.java
index 17bb003..86aab24 100644
--- a/rt/rs/description/src/test/java/org/apache/cxf/jaxrs/model/wadl/BookStore.java
+++ b/rt/rs/description/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;
@@ -82,7 +83,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";
     }
     
@@ -173,6 +175,25 @@ public class BookStore implements BookDescription {
         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;
         private int b;

http://git-wip-us.apache.org/repos/asf/cxf/blob/e530194e/rt/rs/description/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
b/rt/rs/description/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
index b087da0..2708cce 100644
--- a/rt/rs/description/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
+++ b/rt/rs/description/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
@@ -472,7 +472,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);
@@ -585,8 +587,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