cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5701,CXF-5706] More flexible support for Response
Date Fri, 25 Apr 2014 20:37:01 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes e24fef563 -> 3594f3313


[CXF-5701,CXF-5706] More flexible support for Response


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

Branch: refs/heads/2.7.x-fixes
Commit: 3594f331395f556b266d6b3bc248eedd5da4e5e7
Parents: e24fef5
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Fri Apr 25 17:49:56 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Fri Apr 25 21:36:31 2014 +0100

----------------------------------------------------------------------
 .../cxf/tools/wadlto/WadlToolConstants.java     |  8 +--
 .../cxf/tools/wadlto/jaxrs/JAXRSContainer.java  | 13 ++++-
 .../cxf/tools/wadlto/jaxrs/SourceGenerator.java | 58 ++++++++++++++++----
 .../cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml   | 21 ++++++-
 .../tools/wadlto/jaxrs/JAXRSContainerTest.java  |  2 +
 5 files changed, 82 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/3594f331/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 9ba9fa2..3eaa831 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
@@ -54,14 +54,12 @@ public final class WadlToolConstants {
     public static final String CFG_WADL_NAMESPACE = "wadlNamespace";
     public static final String CFG_GENERATE_ENUMS = "generateEnums";
     public static final String CFG_INHERIT_PARAMS = "inheritResourceParams";
+    public static final String CFG_GENERATE_RESPONSE_IF_HEADERS_SET = "generateResponseIfHeadersSet";
+    public static final String CFG_GENERATE_RESPONSE_FOR_METHODS = "generateResponseForMethods";
     
     // JAX-RS 2.0 @Suspended AsyncResponse
     public static final String CFG_SUSPENDED_ASYNC = "async";
-    
-    // CXF @UseAsyncMethod - equivalent of the proposed @ManagedAsync which
-    // which did not make it into JAX-RS 2.0
-    //public static final String CFG_MANAGED_ASYNC = "managedAsync";
-    
+        
     public static final String CFG_WADLURL = "wadl";
     
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/3594f331/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 348693e..2d91498 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
@@ -151,6 +151,7 @@ public class JAXRSContainer extends AbstractCXFToolContainer {
         sg.setMediaTypeMap(getMediaTypeMap());
 
         sg.setSuspendedAsyncMethods(getSuspendedAsyncMethods());
+        sg.setResponseMethods(getResponseMethods());
         
         sg.setGenerateEnums(context.optionSet(WadlToolConstants.CFG_GENERATE_ENUMS));
         sg.setInheritResourceParams(context.optionSet(WadlToolConstants.CFG_INHERIT_PARAMS));
@@ -161,6 +162,8 @@ public class JAXRSContainer extends AbstractCXFToolContainer {
             sg.setUseVoidForEmptyResponses(false);
         }
         
+        sg.setGenerateResponseIfHeadersSet(context.optionSet(WadlToolConstants.CFG_GENERATE_RESPONSE_IF_HEADERS_SET));
+        
         // generate
         String codeType = context.optionSet(WadlToolConstants.CFG_TYPES)
             ? SourceGenerator.CODE_TYPE_GRAMMAR : SourceGenerator.CODE_TYPE_PROXY;
@@ -209,7 +212,15 @@ public class JAXRSContainer extends AbstractCXFToolContainer {
     }
     
     public Set<String> getSuspendedAsyncMethods() {
-        Object value = context.get(WadlToolConstants.CFG_SUSPENDED_ASYNC);
+        return parseMethodList(WadlToolConstants.CFG_SUSPENDED_ASYNC);
+    }
+    
+    public Set<String> getResponseMethods() {
+        return parseMethodList(WadlToolConstants.CFG_GENERATE_RESPONSE_FOR_METHODS);
+    }
+    
+    private Set<String> parseMethodList(String paramName) {
+        Object value = context.get(paramName);
         if (value != null) {
             Set<String> methods = new HashSet<String>();
             String[] values = value.toString().split(",");

http://git-wip-us.apache.org/repos/asf/cxf/blob/3594f331/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 28958a4..a1259f4 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
@@ -188,6 +188,7 @@ public class SourceGenerator {
     private boolean skipSchemaGeneration;
     private boolean inheritResourceParams;
     private boolean useVoidForEmptyResponses = true;
+    private boolean generateResponseIfHeadersSet;
     
     private Map<String, String> properties; 
     
@@ -197,6 +198,7 @@ public class SourceGenerator {
     private List<InputSource> schemaPackageFiles = Collections.emptyList();
     private List<String> compilerArgs = new ArrayList<String>();
     private Set<String> suspendedAsyncMethods = Collections.emptySet();
+    private Set<String> responseMethods = Collections.emptySet();
     private Map<String, String> schemaPackageMap = Collections.emptyMap();
     private Map<String, String> javaTypeMap = Collections.emptyMap();
     private Map<String, String> schemaTypeMap = Collections.emptyMap();
@@ -225,6 +227,11 @@ public class SourceGenerator {
     public void setUseVoidForEmptyResponses(boolean use) {
         this.useVoidForEmptyResponses = use;
     }
+    
+    public void setGenerateResponseIfHeadersSet(boolean set) {
+        this.generateResponseIfHeadersSet = true;
+    }
+    
     public String getWadlNamespace() {
         return wadlNamespace;
     }
@@ -241,6 +248,10 @@ public class SourceGenerator {
         this.suspendedAsyncMethods = asyncMethods;
     }
     
+    public void setResponseMethods(Set<String> responseMethods) {
+        this.responseMethods = responseMethods;
+    }
+    
     private String getClassPackageName(String wadlPackageName) {
         if (resourcePackageName != null) {
             return resourcePackageName;
@@ -663,8 +674,9 @@ public class SourceGenerator {
         if (id.length() == 0) {
             id = methodNameLowerCase;
         }
-        final boolean suspendedAsync = suspendedAsyncMethods.contains(methodNameLowerCase)
-            || methodNameLowerCase != id && suspendedAsyncMethods.contains(id.toLowerCase());
+        final boolean responseRequired = isMethodMatched(responseMethods, methodNameLowerCase,
id);
+        final boolean suspendedAsync = responseRequired ? false
+            : isMethodMatched(suspendedAsyncMethods, methodNameLowerCase, id);
         
         boolean jaxpSourceRequired = xmlRequestReps.size() > 1 && !supportMultipleXmlReps;
         int numOfMethods = jaxpSourceRequired ? 1 : xmlRequestReps.size(); 
@@ -703,8 +715,10 @@ public class SourceGenerator {
                 sbCode.append("public ");
             }
             boolean responseTypeAvailable = true;
+            
             if (methodNameLowerCase.length() > 0) {
-                responseTypeAvailable = writeResponseType(responseEls, sbCode, imports, info,
suspendedAsync);
+                responseTypeAvailable = writeResponseType(responseEls, sbCode, imports, info,

+                                                          responseRequired, suspendedAsync);
                 String genMethodName = id + suffixName;
                 if (methodNameLowerCase.equals(genMethodName)) {
                     List<PathSegment> segments = JAXRSUtils.getPathSegments(currentPath,
true, true);
@@ -759,6 +773,15 @@ public class SourceGenerator {
             sbCode.append(getLineSep()).append(getLineSep());
         }
     }
+    
+    private static boolean isMethodMatched(Set<String> methodNames, String methodNameLowerCase,
String id) {
+        if (methodNames.isEmpty()) {
+            return false;
+        }
+        return methodNames.contains(methodNameLowerCase) 
+            || methodNameLowerCase != id && methodNames.contains(id.toLowerCase())
+            || methodNames.size() == 1 && "*".equals(methodNames.iterator().next());
+    }
 
     private List<Element> getXmlReps(List<Element> repElements) {
         Set<String> values = new HashSet<String>(repElements.size());
@@ -847,6 +870,7 @@ public class SourceGenerator {
                                       StringBuilder sbCode,
                                       Set<String> imports,  
                                       ContextInfo info,
+                                      boolean responseRequired,
                                       boolean suspendedAsync) {
         
         Element okResponse = !suspendedAsync ? getOKResponse(responseEls) : null;
@@ -857,27 +881,39 @@ public class SourceGenerator {
         } else {
             repElements = CastUtils.cast(Collections.emptyList(), Element.class);
         }
-        
+        if (!suspendedAsync && !responseRequired && responseEls.size() ==
1 && generateResponseIfHeadersSet) {
+            List<Element> outResponseParamElements = 
+                getParameters(responseEls.get(0), info.getInheritedParams(), false);
+            if (outResponseParamElements.size() > 0) {
+                writeJaxrResponse(sbCode, imports);
+                return true;
+            }
+        }
         if (repElements.size() == 0) {
-            if (useVoidForEmptyResponses || suspendedAsync) {
+            if (useVoidForEmptyResponses && !responseRequired || suspendedAsync)
{
                 sbCode.append("void ");
+                return false;
             } else {
-                addImport(imports, Response.class.getName());
-                sbCode.append("Response ");
+                writeJaxrResponse(sbCode, imports);
+                return true;
             }
-            return false;
         }
-        String elementName = getElementRefName(
+        
+        String elementName = responseRequired ? null : getElementRefName(
                 getActualRepElement(repElements, getXmlReps(repElements).get(0)), info, imports);
         if (elementName != null) {
             sbCode.append(elementName + " ");
         } else {
-            addImport(imports, Response.class.getName());
-            sbCode.append("Response ");
+            writeJaxrResponse(sbCode, imports);
         }
         return true;
     }
     
+    private void writeJaxrResponse(StringBuilder sbCode, Set<String> imports) {
+        addImport(imports, Response.class.getName());
+        sbCode.append(Response.class.getSimpleName()).append(" ");
+    }
+    
     private Element getOKResponse(List<Element> responseEls) {
         for (int i = 0; i < responseEls.size(); i++) {
             String statusValue = responseEls.get(i).getAttribute("status");

http://git-wip-us.apache.org/repos/asf/cxf/blob/3594f331/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 70831c2..1fd5cc3 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
@@ -161,7 +161,7 @@ Examples:
                 </annotation>
                 <switch>impl</switch>
             </option>
-            
+
             <option id="generateEnums" maxOccurs="1">
                 <annotation>
                     Specifies that Enum classes can be generated to represent
@@ -209,7 +209,22 @@ Examples:
                 </annotation>
                 <switch>supportMultipleXmlReps</switch>
             </option>
- 
+            <option id="generateResponseIfHeadersSet" maxOccurs="1">
+                <annotation>
+                   Use JAX-RS Response return type if WADL Response element has 'header'
parameters   
+                </annotation>
+                <switch>generateResponseIfHeadersSet</switch>
+            </option>
+            <option id="generateResponseForMethods" maxOccurs="unbounded">
+                <annotation>
+                    Specifies a comma separated list of method names or identifiers 
+                    which need to have JAXRS Response return type generated             
       
+                </annotation>
+                <associatedArgument placement="immediate">
+                    <annotation>methodNames</annotation>
+                </associatedArgument>
+                <switch>generateResponseForMethods</switch>
+            </option>
             <option id="async" maxOccurs="unbounded">
                 <annotation>
                     Specifies a comma separated list of method names or identifiers 
@@ -298,4 +313,4 @@ Examples:
             </annotation>
         </argument>
     </usage>
-</toolspec>
\ No newline at end of file
+</toolspec>

http://git-wip-us.apache.org/repos/asf/cxf/blob/3594f331/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 002ffd0..7d58f4d 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
@@ -45,6 +45,8 @@ public class JAXRSContainerTest extends ProcessorTestBase {
             context.put(WadlToolConstants.CFG_MEDIA_TYPE_MAP, 
                         "multipart/form-data=org.apache.cxf.jaxrs.ext.multipart.MultipartBody");
             context.put(WadlToolConstants.CFG_NO_VOID_FOR_EMPTY_RESPONSES, "true");
+            context.put(WadlToolConstants.CFG_GENERATE_RESPONSE_IF_HEADERS_SET, "true");
+            context.put(WadlToolConstants.CFG_GENERATE_RESPONSE_FOR_METHODS, "getName");
             context.put(WadlToolConstants.CFG_COMPILE, "true");
             
             container.setContext(context);


Mime
View raw message