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-6625] Generating custom annotation classes for methods like PATCH, etc
Date Mon, 05 Oct 2015 14:25:39 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 712e96428 -> d35482c9e


[CXF-6625] Generating custom annotation classes for methods like PATCH, etc


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

Branch: refs/heads/master
Commit: d35482c9ede040e880b2baac858ced5103f68730
Parents: 712e964
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Mon Oct 5 15:25:15 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Mon Oct 5 15:25:15 2015 +0100

----------------------------------------------------------------------
 .../cxf/tools/wadlto/jaxrs/SourceGenerator.java | 44 +++++++++++++++++++-
 .../tools/wadlto/jaxrs/JAXRSContainerTest.java  |  3 +-
 .../wadl/bookstoreInlinedSchemaWithImport.xml   |  8 ++++
 .../resources/wadl/bookstoreMultipleSchemas.xml |  8 ++++
 4 files changed, 60 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/d35482c9/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
index a210726..b0bca1f 100644
--- a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
+++ b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
@@ -692,7 +692,8 @@ public class SourceGenerator {
         boolean duplicatesAvailable = 
             getRepsWithElements(allRequestReps, requestRepsWithElements, info.getGrammarInfo());
         
-        final String methodNameLowerCase = methodEl.getAttribute("name").toLowerCase();
+        String methodName = methodEl.getAttribute("name");
+        final String methodNameLowerCase = methodName.toLowerCase();
         String id = methodEl.getAttribute("id");
         if (id.length() == 0) {
             id = methodNameLowerCase;
@@ -737,7 +738,7 @@ public class SourceGenerator {
                         writeAnnotation(sbCode, imports, 
                                         HTTP_METHOD_ANNOTATIONS.get(methodNameLowerCase),
null, true, true);
                     } else {
-                        // TODO : write a custom annotation class name based on HttpMethod
   
+                        writeCustomHttpMethod(info, classPackage, methodName, sbCode, imports);
   
                     }
                     writeFormatAnnotations(requestReps, sbCode, imports, true, null);
                     writeFormatAnnotations(getWadlElements(getOKResponse(responseEls), "representation"),
@@ -800,6 +801,45 @@ public class SourceGenerator {
         }
     }
     
+    private void writeCustomHttpMethod(ContextInfo info, 
+                                       String classPackage,
+                                       String methodName, 
+                                       StringBuilder mainCode,
+                                       Set<String> mainImports) {
+        
+        mainCode.append("@").append(methodName);
+        mainCode.append(getLineSep());
+        mainCode.append(TAB);
+        
+        final String className = methodName;
+        if (info.getResourceClassNames().contains(className)) {
+            return;
+        }
+        info.getResourceClassNames().add(className);
+        
+        
+        StringBuilder sbMethodClassImports = new StringBuilder();
+        sbMethodClassImports.append(getClassComment()).append(getLineSep());
+        sbMethodClassImports.append("package " + classPackage)
+            .append(";").append(getLineSep()).append(getLineSep());
+        
+        sbMethodClassImports.append("import java.lang.annotation.ElementType;").append(getLineSep());
+        sbMethodClassImports.append("import java.lang.annotation.Retention;").append(getLineSep());
+        sbMethodClassImports.append("import java.lang.annotation.RetentionPolicy;").append(getLineSep());
+        sbMethodClassImports.append("import java.lang.annotation.Target;").append(getLineSep());
+        sbMethodClassImports.append("import javax.ws.rs.HttpMethod;").append(getLineSep());
+        
+        StringBuilder sbMethodClassCode = new StringBuilder();
+        sbMethodClassCode.append("@Target({ElementType.METHOD })").append(getLineSep());
+        sbMethodClassCode.append("@Retention(RetentionPolicy.RUNTIME)").append(getLineSep());
+        sbMethodClassCode.append("@HttpMethod(\"" + methodName + "\")").append(getLineSep());
+        sbMethodClassCode.append("public @interface " + methodName);    
+        sbMethodClassCode.append(" {" + getLineSep() + getLineSep());
+        sbMethodClassCode.append("}");
+        createJavaSourceFile(info.getSrcDir(), new QName(classPackage, className), 
+                             sbMethodClassCode, sbMethodClassImports, true);
+    }
+
     private void writeSubresourceMethod(Element resourceEl,
                                         Set<String> imports,
                                         StringBuilder sbCode,

http://git-wip-us.apache.org/repos/asf/cxf/blob/d35482c9/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
b/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
index f947bcf..e3d9104 100644
--- a/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
+++ b/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
@@ -240,8 +240,9 @@ public class JAXRSContainerTest extends ProcessorTestBase {
             assertNotNull(output.list());
             
             List<File> files = FileUtils.getFilesRecurse(output, ".+\\." + "class"
+ "$");
-            assertEquals(7, files.size());
+            assertEquals(8, files.size());
             assertTrue(checkContains(files, "org.apache.cxf.jaxrs.model.wadl" + ".BookStore.class"));
+            assertTrue(checkContains(files, "org.apache.cxf.jaxrs.model.wadl" + ".PATCH.class"));
             assertTrue(checkContains(files, "superbooks" + ".Book.class"));
             assertTrue(checkContains(files, "superbooks" + ".ObjectFactory.class"));
             assertTrue(checkContains(files, "superbooks" + ".package-info.class"));

http://git-wip-us.apache.org/repos/asf/cxf/blob/d35482c9/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreInlinedSchemaWithImport.xml
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreInlinedSchemaWithImport.xml
b/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreInlinedSchemaWithImport.xml
index 19081e0..130b619 100644
--- a/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreInlinedSchemaWithImport.xml
+++ b/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreInlinedSchemaWithImport.xml
@@ -46,6 +46,14 @@
                         <representation mediaType="application/xml" element="prefix1:bookchapter"/>
                     </request>
                 </method>
+                <method name="PATCH" id="patchName">
+                  <request>
+                    <representation mediaType="text/plain">
+                        <param name="request" style="plain" type="xs:string"/>
+                    </representation>
+                  </request>
+                  <response status="204"/>
+                </method>
             </resource>
         </resource>
     </resources>

http://git-wip-us.apache.org/repos/asf/cxf/blob/d35482c9/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreMultipleSchemas.xml
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreMultipleSchemas.xml b/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreMultipleSchemas.xml
index b2d773b..52b5ffc 100644
--- a/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreMultipleSchemas.xml
+++ b/tools/wadlto/jaxrs/src/test/resources/wadl/bookstoreMultipleSchemas.xml
@@ -48,6 +48,14 @@
                         <representation mediaType="application/xml" element="prefix1:thebook"/>
                     </request>
                 </method>
+                <method name="PATCH" id="patchName">
+                  <request>
+                    <representation mediaType="text/plain">
+                        <param name="request" style="plain" type="xs:string"/>
+                    </representation>
+                  </request>
+                  <response status="204"/>
+                </method>
             </resource>
         </resource>
     </resources>


Mime
View raw message