cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1375535 - in /cxf/trunk/tools/wadlto/jaxrs/src: main/java/org/apache/cxf/tools/wadlto/ main/java/org/apache/cxf/tools/wadlto/jaxrs/ test/java/org/apache/cxf/tools/wadlto/jaxrs/ test/resources/wadl/
Date Tue, 21 Aug 2012 13:26:29 GMT
Author: sergeyb
Date: Tue Aug 21 13:26:28 2012
New Revision: 1375535

URL: http://svn.apache.org/viewvc?rev=1375535&view=rev
Log:
[CXF-4477] Avoiding using the schemaTypesMap for both primitive schema type to java type mappings
and complex Java to Java type mappings (as in case of W3C EPR), support for autoboxing of
all optional params, support for mapping representations with no element references to Java
types

Modified:
    cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
    cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
    cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
    cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
    cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
    cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstore.xml

Modified: cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java?rev=1375535&r1=1375534&r2=1375535&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
Tue Aug 21 13:26:28 2012
@@ -38,6 +38,7 @@ public final class WadlToolConstants {
     public static final String CFG_RESOURCENAME = "resourcename";
     
     public static final String CFG_TYPE_MAP = "typeMap";
+    public static final String CFG_MEDIA_TYPE_MAP = "mediaTypeMap";
     public static final String CFG_MULTIPLE_XML_REPS = "supportMultipleXmlReps";
     
     public static final String CFG_CATALOG = ToolConstants.CFG_CATALOG;

Modified: cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java?rev=1375535&r1=1375534&r2=1375535&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
Tue Aug 21 13:26:28 2012
@@ -103,6 +103,7 @@ public class JAXRSContainer extends Abst
         set.add(WadlToolConstants.CFG_BINDING);
         set.add(WadlToolConstants.CFG_SCHEMA_PACKAGENAME);
         set.add(WadlToolConstants.CFG_TYPE_MAP);
+        set.add(WadlToolConstants.CFG_MEDIA_TYPE_MAP);
         return set;
     }
     
@@ -144,7 +145,8 @@ public class JAXRSContainer extends Abst
         sg.setSchemaPackageFiles(schemaPackageFiles);
         sg.setSchemaPackageMap(context.getNamespacePackageMap());
         
-        sg.setSchemaTypesMap(getCustomTypeMap());
+        sg.setJavaTypeMap(getCustomTypeMap());
+        sg.setMediaTypeMap(getMediaTypeMap());
 
         if (context.optionSet(WadlToolConstants.CFG_GENERATE_ENUMS)) {
             sg.setGenerateEnums(true);
@@ -224,8 +226,20 @@ public class JAXRSContainer extends Abst
     }
     
     private Map<String, String> getCustomTypeMap() {
+        Map<String, String> typeMap = getMap(WadlToolConstants.CFG_TYPE_MAP);
+        if (!typeMap.containsKey(EPR_TYPE_KEY)) {
+            typeMap.put(EPR_TYPE_KEY, DEFAULT_TYPES_MAP.get(EPR_TYPE_KEY));
+        }
+        return typeMap;
+    }
+    
+    private Map<String, String> getMediaTypeMap() {
+        return getMap(WadlToolConstants.CFG_MEDIA_TYPE_MAP);
+    }
+    
+    private Map<String, String> getMap(String parameterName) {
         String[] typeToClasses = new String[]{};
-        Object value = context.get(WadlToolConstants.CFG_TYPE_MAP);
+        Object value = context.get(parameterName);
         if (value != null) {
             typeToClasses = value instanceof String ? new String[]{(String)value}
                                                    : (String[])value;
@@ -239,9 +253,6 @@ public class JAXRSContainer extends Abst
                 typeMap.put(type, clsName);
             }
         }
-        if (!typeMap.containsKey(EPR_TYPE_KEY)) {
-            typeMap.put(EPR_TYPE_KEY, DEFAULT_TYPES_MAP.get(EPR_TYPE_KEY));
-        }
         return typeMap;
     }
 }

Modified: cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java?rev=1375535&r1=1375534&r2=1375535&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
Tue Aug 21 13:26:28 2012
@@ -79,6 +79,7 @@ import org.apache.cxf.common.jaxb.JAXBUt
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.PackageUtils;
 import org.apache.cxf.common.util.ReflectionInvokationHandler;
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.common.util.SystemPropertyAction;
 import org.apache.cxf.common.xmlschema.SchemaCollection;
 import org.apache.cxf.common.xmlschema.XmlSchemaConstants;
@@ -107,6 +108,11 @@ public class SourceGenerator {
     private static final List<String> HTTP_OK_STATUSES =
         Arrays.asList(new String[] {"200", "201", "202", "203", "204"});
     
+    private static final Set<Class<?>> OPTIONAL_PARAMS = 
+        new HashSet<Class<?>>(Arrays.<Class<?>>asList(QueryParam.class,

+                                                      HeaderParam.class,
+                                                      MatrixParam.class,
+                                                      FormParam.class));
     private static final Map<String, Class<?>> HTTP_METHOD_ANNOTATIONS;
     private static final Map<String, Class<?>> PARAM_ANNOTATIONS;
     private static final Set<String> RESOURCE_LEVEL_PARAMS;
@@ -178,7 +184,8 @@ public class SourceGenerator {
     private List<InputSource> schemaPackageFiles = Collections.emptyList();
     private List<String> compilerArgs = new ArrayList<String>();
     private Map<String, String> schemaPackageMap = Collections.emptyMap();
-    private Map<String, String> schemaTypesMap = Collections.emptyMap();
+    private Map<String, String> javaTypeMap = Collections.emptyMap();
+    private Map<String, String> mediaTypesMap = Collections.emptyMap();
     private Bus bus;
     private boolean supportMultipleXmlReps;
     
@@ -773,7 +780,7 @@ public class SourceGenerator {
                 return el;
             }
         }
-        return null;
+        return repElements.isEmpty() ? null : repElements.get(0);
     }
     
     private boolean writeResponseType(List<Element> responseEls,
@@ -876,7 +883,7 @@ public class SourceGenerator {
             boolean isRepeating = Boolean.valueOf(paramEl.getAttribute("repeating"));
             String type = enumCreated ? getTypicalClassName(name)
                 : getPrimitiveType(paramEl, info, imports);
-            if (paramAnn == QueryParam.class
+            if (OPTIONAL_PARAMS.contains(paramAnn)
                 && (isRepeating || !Boolean.valueOf(paramEl.getAttribute("required")))
   
                 && AUTOBOXED_PRIMITIVES_MAP.containsKey(type)) {
                 type = AUTOBOXED_PRIMITIVES_MAP.get(type);
@@ -900,6 +907,10 @@ public class SourceGenerator {
             if (!jaxpRequired) {    
                 elementParamType = getElementRefName(repElement, info, imports);
                 if (elementParamType != null) {
+                    int lastIndex = elementParamType.lastIndexOf('.');
+                    if (lastIndex != -1) {
+                        elementParamType = elementParamType.substring(lastIndex + 1);
+                    }
                     elementParamName = elementParamType.toLowerCase();
                 }
             } else {
@@ -1023,10 +1034,12 @@ public class SourceGenerator {
         }
         String[] pair = type.split(":");
         String value = pair.length == 2 ? pair[1] : type;
-        if (schemaTypesMap.containsKey(value)) {
-            return schemaTypesMap.get(value);
-        } else if (XSD_SPECIFIC_TYPE_MAP.containsKey(value)) {
-            return XSD_SPECIFIC_TYPE_MAP.get(value);
+        if (XSD_SPECIFIC_TYPE_MAP.containsKey(value)) {
+            String theType = XSD_SPECIFIC_TYPE_MAP.get(value);
+            if (javaTypeMap.containsKey(theType)) {
+                theType = javaTypeMap.get(theType);
+            }
+            return theType;
         } else {
             String actualValue = value.replaceAll("[\\-\\_]", "");
             if (pair.length > 1) {
@@ -1076,6 +1089,12 @@ public class SourceGenerator {
                 return convertRefToClassName(pair[0], pair[1], null, info, imports);
             }
         } else {
+            // try mediaTypesMap first
+            String mediaType = repElement.getAttribute("mediaType");
+            if (!StringUtils.isEmpty(mediaType) && mediaTypesMap.containsKey(mediaType))
{
+                return mediaTypesMap.get(mediaType);
+            }
+            
             Element param = DOMUtils.getFirstChildWithName(repElement, getWadlNamespace(),
"param");
             if (param != null) {
                 return getPrimitiveType(param, info, imports);
@@ -1094,8 +1113,8 @@ public class SourceGenerator {
                 clsName = matchClassName(typeClassNames, packageName, elementTypeName.replaceAll("_",
""));
             }
         }
-        if (clsName == null && schemaTypesMap != null) {
-            clsName = schemaTypesMap.get(packageName + "." + localName);
+        if (clsName == null && javaTypeMap != null) {
+            clsName = javaTypeMap.get(packageName + "." + localName);
         }
         return clsName;
     }
@@ -1400,8 +1419,12 @@ public class SourceGenerator {
         this.schemaPackageMap = map;
     }
     
-    public void setSchemaTypesMap(Map<String, String> map) {
-        this.schemaTypesMap = map;
+    public void setJavaTypeMap(Map<String, String> map) {
+        this.javaTypeMap = map;
+    }
+    
+    public void setMediaTypeMap(Map<String, String> map) {
+        this.mediaTypesMap = map;
     }
     
     public void setBus(Bus bus) {

Modified: cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml?rev=1375535&r1=1375534&r2=1375535&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
Tue Aug 21 13:26:28 2012
@@ -69,13 +69,26 @@ Examples:
             
             <option id="typeMap" maxOccurs="unbounded">
                 <annotation>
-                    Specifies the optional mapping between WADL representation 
-                    or parameter type and Java class.
+                    Specifies the optional mapping between default Java types
+                    representing WADL representation or parameter types 
+                    and custom Java types.
                 </annotation>
                 <switch>tMap</switch>
                 <associatedArgument placement="afterSpace">
                     <valuetype>NamingSpacePackageString</valuetype>
-                    <annotation>type-name=class-name</annotation>
+                    <annotation>default-java-type=preferred-java-type</annotation>
+                </associatedArgument>
+            </option>
+            
+            <option id="mediaTypeMap" maxOccurs="unbounded">
+                <annotation>
+                    Specifies the optional mapping between a WADL representation
+                    with no wadl:element attribute and Java class.
+                </annotation>
+                <switch>repMap</switch>
+                <associatedArgument placement="afterSpace">
+                    <valuetype>NamingSpacePackageString</valuetype>
+                    <annotation>media-type=class-name</annotation>
                 </associatedArgument>
             </option>
 

Modified: cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java?rev=1375535&r1=1375534&r2=1375535&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
Tue Aug 21 13:26:28 2012
@@ -40,6 +40,10 @@ public class JAXRSContainerTest extends 
             ToolContext context = new ToolContext();
             context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
             context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/bookstore.xml"));
+            context.put(WadlToolConstants.CFG_MEDIA_TYPE_MAP, 
+                        "application/xml=javax.xml.transform.Source");
+            context.put(WadlToolConstants.CFG_MEDIA_TYPE_MAP, 
+                        "multipart/form-data=org.apache.cxf.jaxrs.ext.multipart.MultipartBody");
             context.put(WadlToolConstants.CFG_COMPILE, "true");
             
             container.setContext(context);
@@ -177,7 +181,7 @@ public class JAXRSContainerTest extends 
             ToolContext context = new ToolContext();
             context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
             context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/resourceWithEPR.xml"));
-            context.put(WadlToolConstants.CFG_TYPE_MAP, "date=javax.xml.datatype.XMLGregorianCalendar");
+            context.put(WadlToolConstants.CFG_TYPE_MAP, "java.util.Date=javax.xml.datatype.XMLGregorianCalendar");
             context.put(WadlToolConstants.CFG_COMPILE, "true");
 
             container.setContext(context);

Modified: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstore.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstore.xml?rev=1375535&r1=1375534&r2=1375535&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstore.xml (original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/bookstore.xml Tue Aug 21 13:26:28
2012
@@ -125,7 +125,9 @@ required="true">
        <representation mediaType="application/xml" element="prefix1:theBook2">
         <doc>InputBook</doc>
        </representation>
-       
+       <representation mediaType="application/json" element="prefix1:theBook2">
+        <doc>InputBook</doc>
+       </representation>
       </request>
       <response>
        <doc>Response</doc>
@@ -143,7 +145,7 @@ required="true">
        <representation mediaType="application/xml"/>
       </request>
       <response>
-       <representation mediaType="application/xml"/>
+       <representation mediaType="multipart/form-data"/>
       </response>
      </method>
 



Mime
View raw message