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-7032] Adding a -beanValidation option, test to be enhanced
Date Mon, 29 Aug 2016 20:28:13 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 35eb4eb1e -> b977123ac


[CXF-7032] Adding a -beanValidation option, test to be enhanced


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

Branch: refs/heads/master
Commit: b977123acec6412e2df41df54cf0d33af78d6f7a
Parents: 35eb4eb
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Mon Aug 29 21:27:48 2016 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Mon Aug 29 21:27:48 2016 +0100

----------------------------------------------------------------------
 .../cxf/tools/wadlto/WadlToolConstants.java     |  1 +
 .../cxf/tools/wadlto/jaxrs/JAXRSContainer.java  |  1 +
 .../cxf/tools/wadlto/jaxrs/SourceGenerator.java | 50 +++++++++++++++++---
 .../cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml   |  8 ++++
 .../tools/wadlto/jaxrs/JAXRSContainerTest.java  | 22 +++++++++
 .../wadl/resourceSameTargetNsSchemas.xml        |  1 +
 6 files changed, 77 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/b977123a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
index 673f7a5..50611a4 100644
--- a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
+++ b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
@@ -43,6 +43,7 @@ public final class WadlToolConstants {
     public static final String CFG_SCHEMA_TYPE_MAP = "schemaTypeMap";
     public static final String CFG_MEDIA_TYPE_MAP = "mediaTypeMap";
     public static final String CFG_MULTIPLE_XML_REPS = "supportMultipleXmlReps";
+    public static final String CFG_BEAN_VALIDATION = "beanValidation";
     
     public static final String CFG_CATALOG = ToolConstants.CFG_CATALOG;
     public static final String CFG_BINDING = ToolConstants.CFG_BINDING;

http://git-wip-us.apache.org/repos/asf/cxf/blob/b977123a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
index ec36bc0..8780898 100644
--- a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
+++ b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
@@ -134,6 +134,7 @@ public class JAXRSContainer extends AbstractCXFToolContainer {
         }
         
         sg.setSupportMultipleXmlReps(context.optionSet(WadlToolConstants.CFG_MULTIPLE_XML_REPS));
+        sg.setSupportBeanValidation(context.optionSet(WadlToolConstants.CFG_BEAN_VALIDATION));
         sg.setCreateJavaDocs(context.optionSet(WadlToolConstants.CFG_CREATE_JAVA_DOCS));
         // set the base path
         sg.setWadlPath(wadlURL);

http://git-wip-us.apache.org/repos/asf/cxf/blob/b977123a/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 f07df3b..1137f00 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
@@ -134,6 +134,8 @@ public class SourceGenerator {
     private static final Map<String, Class<?>> HTTP_METHOD_ANNOTATIONS;
     private static final Map<String, Class<?>> PARAM_ANNOTATIONS;
     private static final String PLAIN_PARAM_STYLE = "plain";
+    private static final String BEAN_VALID_SIMPLE_NAME = "Valid";
+    private static final String BEAN_VALID_FULL_NAME = "javax.validation." + BEAN_VALID_SIMPLE_NAME;
     private static final Set<String> RESOURCE_LEVEL_PARAMS;
     private static final Map<String, String> AUTOBOXED_PRIMITIVES_MAP;
     private static final Map<String, String> XSD_SPECIFIC_TYPE_MAP;
@@ -219,6 +221,7 @@ public class SourceGenerator {
     private Map<String, String> mediaTypesMap = Collections.emptyMap();
     private Bus bus;
     private boolean supportMultipleRepsWithElements;
+    private boolean supportBeanValidation;
     private boolean validateWadl;    
     private SchemaCollection schemaCollection = new SchemaCollection();
     private String encoding;
@@ -663,7 +666,7 @@ public class SourceGenerator {
             }
         }
     }
-    
+        
     private void addImport(Set<String> imports, String clsName) {
         if (imports == null || clsName.startsWith("java.lang") || !clsName.contains("."))
{
             return;
@@ -761,8 +764,14 @@ public class SourceGenerator {
                         writeCustomHttpMethod(info, classPackage, methodName, sbMethodCode,
imports);    
                     }
                     writeFormatAnnotations(requestReps, sbMethodCode, imports, true, null);
-                    writeFormatAnnotations(getWadlElements(getOKResponse(responseEls), "representation"),
+                    List<Element> responseReps = getWadlElements(getOKResponse(responseEls),
"representation");
+                    writeFormatAnnotations(responseReps,
                                            sbMethodCode, imports, false, requestRepWithElement);
+                    if (supportBeanValidation && !responseRequired 
+                        && isRepWithElementAvailable(responseReps, info.getGrammarInfo()))
{
+                        addImport(imports, BEAN_VALID_FULL_NAME);
+                        sbMethodCode.append("@").append(BEAN_VALID_SIMPLE_NAME).append(getLineSep()).append(TAB);
+                    }
                 }
                 if (!isRoot && !"/".equals(currentPath)) {
                     writeAnnotation(sbMethodCode, imports, Path.class, currentPath, true,
true);
@@ -1014,6 +1023,18 @@ public class SourceGenerator {
         return duplicatesCount > 0;
     }
     
+    private boolean isRepWithElementAvailable(List<Element> repElements,
+                                              GrammarInfo gInfo) {
+        for (Element el : repElements) {
+            String value = el.getAttribute("element");
+            if (value.length() > 0 
+                && (value.contains(":") || gInfo.isSchemaWithoutTargetNamespace()))
{
+                return true;
+            }
+        }
+        return false;
+    }
+    
     private List<Element> getParameters(Element resourceEl, List<Element> inheritedParams,

                                         boolean isSubresourceMethod) {
         List<Element> inParamElements = new LinkedList<Element>();
@@ -1233,6 +1254,7 @@ public class SourceGenerator {
             }
         }
                   
+        boolean writeAnnotations = writeAnnotations(info.isInterfaceGenerated());
         for (int i = 0; i < inParamEls.size(); i++) {
     
             Element paramEl = inParamEls.get(i);
@@ -1251,7 +1273,7 @@ public class SourceGenerator {
                     enumCreated = true;
                 }
             }
-            if (writeAnnotations(info.isInterfaceGenerated())) {
+            if (writeAnnotations) {
                 String required = paramEl.getAttribute("required");
                 if (Multipart.class.equals(paramAnn) && "false".equals(required))
{
                     writeAnnotation(sbCode, imports, paramAnn, null, false, false);
@@ -1295,10 +1317,15 @@ public class SourceGenerator {
         }
         String elementParamType = null;
         String elementParamName = null;
+        boolean writeBeanValidation = false;
         if (!form) {
             if (!jaxpRequired) {    
                 elementParamType = getElementRefName(repElement, info, imports, false);
                 if (elementParamType != null) {
+                    if (writeAnnotations && supportBeanValidation 
+                        && isRepWithElementAvailable(Collections.singletonList(repElement),
info.getGrammarInfo())) {
+                        writeBeanValidation = true; 
+                    }
                     int lastIndex = elementParamType.lastIndexOf('.');
                     if (lastIndex != -1) {
                         elementParamType = elementParamType.substring(lastIndex + 1);
@@ -1329,6 +1356,11 @@ public class SourceGenerator {
             if (inParamEls.size() > 0) {
                 sbCode.append(", ");
             }
+            if (writeBeanValidation) {
+                addImport(imports, BEAN_VALID_FULL_NAME);
+                sbCode.append("@").append(BEAN_VALID_SIMPLE_NAME).append(" ");
+            }
+            
             sbCode.append(elementParamType).append(" ").append(elementParamName);
         }
         if (sbMethodDocs != null && repElement != null) {
@@ -1338,10 +1370,12 @@ public class SourceGenerator {
             if (inParamEls.size() > 0 || elementParamType != null) {
                 sbCode.append(", ");
             }
-            addImport(imports, Suspended.class.getName());
+            if (writeAnnotations) {
+                addImport(imports, Suspended.class.getName());
+                sbCode.append("@").append(Suspended.class.getSimpleName()).append(" "); 
      
+            }
             addImport(imports, AsyncResponse.class.getName());
-            sbCode.append("@").append(Suspended.class.getSimpleName()).append(" ")
-                .append(AsyncResponse.class.getSimpleName()).append(" ").append("async");
+            sbCode.append(AsyncResponse.class.getSimpleName()).append(" ").append("async");
         }
     }
     
@@ -1994,6 +2028,10 @@ public class SourceGenerator {
         this.createJavaDocs = createJavaDocs;
     }
 
+    public void setSupportBeanValidation(boolean supportBeanValidation) {
+        this.supportBeanValidation = supportBeanValidation;
+    }
+
     private static class GrammarInfo {
         private Map<String, String> nsMap = new HashMap<String, String>();
         private Map<String, String> elementTypeMap = new HashMap<String, String>();

http://git-wip-us.apache.org/repos/asf/cxf/blob/b977123a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
index 5a27e42..6bec5ba 100644
--- a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
+++ b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
@@ -240,6 +240,14 @@ Examples:
                     <annotation>encoding</annotation>
                 </associatedArgument>
             </option>
+            <option id="beanValidation" maxOccurs="1">
+                <annotation>
+                    Specifies that javax.validation.Valid annotation needs to be added to
+                    methods which return data entities and method parameters which represent
+                    data entities    
+                </annotation>
+                <switch>beanValidation</switch>
+            </option>
             <option id="validate" maxOccurs="1">
                 <annotation>
                     Specifies that the WADL is validated before generating the code. Using


http://git-wip-us.apache.org/repos/asf/cxf/blob/b977123a/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 2be4efa..e4c9c15 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
@@ -456,6 +456,28 @@ public class JAXRSContainerTest extends ProcessorTestBase {
         assertEquals("id", methodPathParam1.value());
     }
     
+    @Test
+    public void testBeanValidation() {
+        try {
+            JAXRSContainer container = new JAXRSContainer(null);
+
+            ToolContext context = new ToolContext();
+            context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
+            context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/resourceSameTargetNsSchemas.xml"));
+            context.put(WadlToolConstants.CFG_BEAN_VALIDATION, "true");
+            //context.put(WadlToolConstants.CFG_COMPILE, "true");
+
+            container.setContext(context);
+            container.execute();
+
+            assertNotNull(output.list());
+            
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+    
     @Test    
     public void testCodeGenWithImportedSchemaAndResourceSet() {
         try {

http://git-wip-us.apache.org/repos/asf/cxf/blob/b977123a/tools/wadlto/jaxrs/src/test/resources/wadl/resourceSameTargetNsSchemas.xml
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/test/resources/wadl/resourceSameTargetNsSchemas.xml b/tools/wadlto/jaxrs/src/test/resources/wadl/resourceSameTargetNsSchemas.xml
index b1a9b49..5de1256 100644
--- a/tools/wadlto/jaxrs/src/test/resources/wadl/resourceSameTargetNsSchemas.xml
+++ b/tools/wadlto/jaxrs/src/test/resources/wadl/resourceSameTargetNsSchemas.xml
@@ -18,6 +18,7 @@
     <resources base="http://localhost:8080/baz">
         <resource path="/">
             <resource path="setTest3">
+                <param name="id" style="template"/>
                 <method name="PUT">
                     <request>
                         <representation mediaType="application/xml" element="ns1:test"/>


Mime
View raw message