cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6064] Making it simpler to customize WADL resource ids
Date Fri, 14 Nov 2014 17:14:20 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 0e410548c -> ff407cd32


[CXF-6064] Making it simpler to customize WADL resource ids


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

Branch: refs/heads/3.0.x-fixes
Commit: ff407cd321af92fa1cb7d852690a1fa640742cc6
Parents: 0e41054
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri Nov 14 17:12:34 2014 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri Nov 14 17:14:04 2014 +0000

----------------------------------------------------------------------
 .../cxf/jaxrs/model/wadl/WadlGenerator.java     | 61 +++++++++++++-------
 1 file changed, 41 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/ff407cd3/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 4c15480..fa9e295 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
@@ -147,7 +147,6 @@ public class WadlGenerator implements ContainerRequestFilter {
     private boolean singleResourceMultipleMethods = true;
     private boolean useSingleSlashResource;
     private boolean ignoreForwardSlash;
-    private boolean addResourceAndMethodIds;
     private boolean ignoreRequests;
     private boolean linkAnyMediaTypeToXmlSchema;
     private boolean useJaxbContextForQnames = true;
@@ -170,7 +169,8 @@ public class WadlGenerator implements ContainerRequestFilter {
     private MediaType defaultRepMediaType = MediaType.WILDCARD_TYPE;
     private Bus bus;
     private DocumentationProvider docProvider;
-        
+    private ResourceIdGenerator idGenerator;     
+    
     public WadlGenerator() {
     }
     
@@ -266,7 +266,7 @@ public class WadlGenerator implements ContainerRequestFilter {
         Map<Class<?>, QName> clsMap = new IdentityHashMap<Class<?>,
QName>();
         Set<ClassResourceInfo> visitedResources = new LinkedHashSet<ClassResourceInfo>();
         for (ClassResourceInfo cri : cris) {
-            startResourceTag(sbResources, cri.getServiceClass(), cri.getURITemplate().getValue());
+            startResourceTag(sbResources, cri, cri.getURITemplate().getValue());
             
             Annotation description = AnnotationUtils.getClassAnnotation(cri.getServiceClass(),
Description.class);
             if (description == null) {
@@ -350,7 +350,7 @@ public class WadlGenerator implements ContainerRequestFilter {
                 Class<?> cls = getMethod(ori).getReturnType();
                 ClassResourceInfo subcri = cri.findResource(cls, cls);
                 if (subcri != null && !visitedResources.contains(subcri)) {
-                    startResourceTag(sb, subcri.getServiceClass(), ori.getURITemplate().getValue());
+                    startResourceTag(sb, subcri, ori.getURITemplate().getValue());
                     handleDocs(subcri.getServiceClass().getAnnotations(), sb, DocTarget.RESOURCE,
true,
                                isJson);
                     handlePathAndMatrixParams(sb, ori, isJson);
@@ -380,18 +380,11 @@ public class WadlGenerator implements ContainerRequestFilter {
         return classParams;
     }
 
-    private void startResourceTag(StringBuilder sb, Class<?> serviceClass, String path)
{
+    protected void startResourceTag(StringBuilder sb, ClassResourceInfo cri, String path)
{
         sb.append("<resource path=\"").append(getPath(path)).append("\"");
-        if (addResourceAndMethodIds) {
-            QName jaxbQname = null;
-            if (useJaxbContextForQnames) {
-                jaxbQname = getJaxbQName(null, serviceClass, new HashMap<Class<?>,
QName>(0));
-            }
-            String pName = jaxbQname == null ? PackageUtils.getPackageName(serviceClass)
: jaxbQname
-                .getNamespaceURI();
-            String localName = jaxbQname == null ? serviceClass.getSimpleName() : jaxbQname.getLocalPart();
-            String finalName = jaxbQname == null ? pName + "." : "{" + pName + "}";
-            sb.append(" id=\"").append(finalName + localName).append("\"");
+        if (idGenerator != null) {
+            String id = idGenerator.getClassResourceId(cri);
+            sb.append(" id=\"").append(id).append("\"");
         }
         sb.append(">");
     }
@@ -469,8 +462,9 @@ public class WadlGenerator implements ContainerRequestFilter {
     
     protected void startMethodTag(StringBuilder sb, OperationResourceInfo ori) {
         sb.append("<method name=\"").append(ori.getHttpMethod()).append("\"");
-        if (addResourceAndMethodIds) {
-            sb.append(" id=\"").append(getMethod(ori).getName()).append("\"");
+        if (idGenerator != null) {
+            String id = idGenerator.getMethodResourceId(ori);
+            sb.append(" id=\"").append(id).append("\"");
         }
         sb.append(">");
     }
@@ -620,8 +614,7 @@ public class WadlGenerator implements ContainerRequestFilter {
                 sb.append("<!-- Dynamic subresource -->");
             }
         }
-        startResourceTag(sb, subcri != null ? subcri.getServiceClass() : Object.class, ori.getURITemplate()
-            .getValue());
+        startResourceTag(sb, subcri, ori.getURITemplate().getValue());
         handlePathAndMatrixParams(sb, ori, isJson);
         sb.append("</resource>");
     }
@@ -1949,8 +1942,13 @@ public class WadlGenerator implements ContainerRequestFilter {
         return privateAddresses;
     }
 
+    
     public void setAddResourceAndMethodIds(boolean addResourceAndMethodIds) {
-        this.addResourceAndMethodIds = addResourceAndMethodIds;
+        ResourceIdGenerator idGen = addResourceAndMethodIds ? new ResourceIdGeneratorImpl()
: null; 
+        setResourceIdGenerator(idGen);
+    }
+    public void setResourceIdGenerator(ResourceIdGenerator idGen) {
+        this.idGenerator = idGen;
     }
 
     private Method getMethod(OperationResourceInfo ori) {
@@ -2037,4 +2035,27 @@ public class WadlGenerator implements ContainerRequestFilter {
     }
 
     
+    private class ResourceIdGeneratorImpl implements ResourceIdGenerator {
+
+        @Override
+        public String getClassResourceId(ClassResourceInfo cri) {
+            Class<?> serviceClass = cri != null ? cri.getServiceClass() : Object.class;
+            QName jaxbQname = null;
+            if (useJaxbContextForQnames) {
+                jaxbQname = getJaxbQName(null, serviceClass, new HashMap<Class<?>,
QName>(0));
+            }
+            String pName = jaxbQname == null ? PackageUtils.getPackageName(serviceClass)
: jaxbQname
+                .getNamespaceURI();
+            String localName = jaxbQname == null ? serviceClass.getSimpleName() : jaxbQname.getLocalPart();
+            String nsName = jaxbQname == null ? pName + "." : "{" + pName + "}";
+            return nsName + localName;
+        }
+
+        @Override
+        public String getMethodResourceId(OperationResourceInfo ori) {
+            return getMethod(ori).getName();
+        }
+        
+    }
+    
 }


Mime
View raw message