cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1337362 - in /cxf/branches/2.4.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
Date Fri, 11 May 2012 20:23:55 GMT
Author: sergeyb
Date: Fri May 11 20:23:55 2012
New Revision: 1337362

URL: http://svn.apache.org/viewvc?rev=1337362&view=rev
Log:
Merged revisions 1337353 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/branches/2.5.x-fixes

................
  r1337353 | sergeyb | 2012-05-11 21:07:30 +0100 (Fri, 11 May 2012) | 9 lines
  
  Merged revisions 1337138 via svnmerge from 
  https://svn.apache.org/repos/asf/cxf/trunk
  
  ........
    r1337138 | sergeyb | 2012-05-11 14:04:48 +0100 (Fri, 11 May 2012) | 1 line
    
    [CXF-4310] Improving a bit the support for schemas with empty target namespaces
  ........
................

Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (added)
+++ svn:mergeinfo Fri May 11 20:23:55 2012
@@ -0,0 +1,2 @@
+/cxf/branches/2.5.x-fixes:1337353
+/cxf/trunk:1337138

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java?rev=1337362&r1=1337361&r2=1337362&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
Fri May 11 20:23:55 2012
@@ -877,10 +877,17 @@ public class WadlGenerator implements Re
         }
         SchemaCollection xmlSchemaCollection = new SchemaCollection();
         Collection<DOMSource> schemas = new HashSet<DOMSource>();
+        List<String> targetNamespaces = new ArrayList<String>();
         try {
             for (DOMResult r : JAXBUtils.generateJaxbSchemas(context,
                                     CastUtils.cast(Collections.emptyMap(), String.class,
DOMResult.class))) {
-                schemas.add(new DOMSource(r.getNode(), r.getSystemId()));
+                DOMSource source = new DOMSource(r.getNode(), r.getSystemId());
+                schemas.add(source);
+                String tns = 
+                    ((Document)source.getNode()).getDocumentElement().getAttribute("targetNamespace");
+                if (!StringUtils.isEmpty(tns)) {
+                    targetNamespaces.add(tns);
+                }
             }
         } catch (IOException e) {
             LOG.fine("No schema can be generated");
@@ -890,10 +897,12 @@ public class WadlGenerator implements Re
         boolean hackAroundEmptyNamespaceIssue = false;
         for (DOMSource r : schemas) {
             hackAroundEmptyNamespaceIssue =
-                              addSchemaDocument(xmlSchemaCollection,
-                             (Document)r.getNode(),
-                              r.getSystemId(),
-                              hackAroundEmptyNamespaceIssue);
+                              addSchemaDocument(
+                                  xmlSchemaCollection,
+                                  targetNamespaces,
+                                  (Document)r.getNode(),
+                                  r.getSystemId(),
+                                  hackAroundEmptyNamespaceIssue);
         }
         return xmlSchemaCollection;
     }
@@ -902,13 +911,13 @@ public class WadlGenerator implements Re
 
         XmlRootElement root = type.getAnnotation(XmlRootElement.class);
         if (root != null) {
-            QName qname = getQNameFromParts(root.name(), root.namespace(), clsMap);
+            QName qname = getQNameFromParts(root.name(), root.namespace(), type, clsMap);
             if (qname != null) {
                 return qname;
             }
             String ns = JAXBUtils.getPackageNamespace(type);
             if (ns != null) {
-                return getQNameFromParts(root.name(), ns, clsMap);
+                return getQNameFromParts(root.name(), ns, type, clsMap);
             } else {
                 return null;
             }
@@ -922,6 +931,7 @@ public class WadlGenerator implements Re
             Object instance = type.newInstance();
             return getQNameFromParts(jaxbInfo.getElementLocalName(instance),
                                      jaxbInfo.getElementNamespaceURI(instance),
+                                     type,
                                      clsMap);
         } catch (Exception ex) {
             // ignore
@@ -977,6 +987,7 @@ public class WadlGenerator implements Re
     
     // TODO : can we reuse this block with JAXBBinding somehow ?
     public boolean addSchemaDocument(SchemaCollection col,
+                                     List<String> tnsList,
                                      Document d,
                                      String systemId,
                                      boolean hackAroundEmptyNamespaceIssue) {
@@ -990,7 +1001,7 @@ public class WadlGenerator implements Re
             //create a copy of the dom so we
             //can modify it.
             d = copy(d);
-            ns = "";
+            ns = tnsList.isEmpty() ? "" : tnsList.get(0);
             d.getDocumentElement().setAttribute("targetNamespace", ns);
         }
 
@@ -1057,14 +1068,25 @@ public class WadlGenerator implements Re
     }
 
 
-    private QName getQNameFromParts(String name, String namespace, Map<Class<?>,
QName> clsMap) {
-        if (name == null || JAXB_DEFAULT_NAME.equals(name) || name.length() == 0) {
+    private QName getQNameFromParts(String name, 
+                                    String namespace,
+                                    Class<?> type,
+                                    Map<Class<?>, QName> clsMap) {
+        if (namespace == null || JAXB_DEFAULT_NAMESPACE.equals(namespace) || namespace.length()
== 0) {
             return null;
         }
-        if (namespace == null || JAXB_DEFAULT_NAMESPACE.equals(namespace) || namespace.length()
== 0) {
+        if (name == null || name.length() == 0) {
             return null;
         }
-
+        if (JAXB_DEFAULT_NAME.equals(name)) {
+            name = type.getSimpleName();
+            StringBuilder sb = new StringBuilder();
+            sb.append(Character.toLowerCase(name.charAt(0))); 
+            if (name.length() > 1) {
+                sb.append(name.substring(1));
+            }
+            name = sb.toString();
+        }
         String prefix = getPrefix(namespace, clsMap);
         return new QName(namespace, name, prefix);
     }
@@ -1386,7 +1408,9 @@ public class WadlGenerator implements Re
                     return qname;
                 } else {
                     return getQNameFromParts(qname.getLocalPart(),
-                                             qname.getNamespaceURI(), clsMap);
+                                             qname.getNamespaceURI(),
+                                             type,
+                                             clsMap);
                 }
             }
             return null;
@@ -1412,7 +1436,7 @@ public class WadlGenerator implements Re
                     elementName = name.toLowerCase();
                 }
                 if (elementName != null) {
-                    return getQNameFromParts(elementName, entry.getKey(), clsMap);
+                    return getQNameFromParts(elementName, entry.getKey(), type, clsMap);
                 }
             }
             return null;

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java?rev=1337362&r1=1337361&r2=1337362&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/wadl/WadlGeneratorTest.java
Fri May 11 20:23:55 2012
@@ -26,9 +26,14 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import javax.ws.rs.Consumes;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import javax.xml.bind.annotation.XmlRootElement;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -211,6 +216,35 @@ public class WadlGeneratorTest extends A
     }
     
     @Test
+    public void testTwoSchemasSameNs() throws Exception {
+        WadlGenerator wg = new WadlGenerator();
+        wg.setApplicationTitle("My Application");
+        wg.setNamespacePrefix("ns");
+        ClassResourceInfo cri = 
+            ResourceUtils.createClassResourceInfo(TestResource.class, TestResource.class,
true, true);
+        Message m = mockMessage("http://localhost:8080/baz", "/bar", WadlGenerator.WADL_QUERY,
null);
+        
+        Response r = wg.handleRequest(m, cri);
+        checkResponse(r);
+        Document doc = DOMUtils.readXml(new StringReader(r.getEntity().toString()));
+        checkDocs(doc.getDocumentElement(), "My Application", "", "");
+        List<Element> grammarEls = DOMUtils.getChildrenWithName(doc.getDocumentElement(),

+                                                                WadlGenerator.WADL_NS, 
+                                                                "grammars");
+        assertEquals(1, grammarEls.size());
+        List<Element> schemasEls = DOMUtils.getChildrenWithName(grammarEls.get(0),

+              XmlSchemaConstants.XSD_NAMESPACE_URI, "schema");
+        assertEquals(2, schemasEls.size());
+        assertEquals("http://example.com/test", schemasEls.get(0).getAttribute("targetNamespace"));
+        assertEquals("http://example.com/test", schemasEls.get(1).getAttribute("targetNamespace"));
+        List<Element> reps = DOMUtils.findAllElementsByTagNameNS(doc.getDocumentElement(),

+                                       WadlGenerator.WADL_NS, "representation");
+        assertEquals(2, reps.size());
+        assertEquals("ns1:testCompositeObject", reps.get(0).getAttribute("element"));
+        assertEquals("ns1:testCompositeObject", reps.get(1).getAttribute("element"));
+    }
+    
+    @Test
     public void testRootResourceWithSingleSlash() throws Exception {
         WadlGenerator wg = new WadlGenerator();
         ClassResourceInfo cri = 
@@ -708,4 +742,33 @@ public class WadlGeneratorTest extends A
             return repeating;
         }
     }
+    
+    @XmlRootElement(namespace = "http://example.com/test")
+    public static class TestCompositeObject {
+        private int id;
+        private String name;
+        public int getId() {
+            return id;
+        }
+        public void setId(int id) {
+            this.id = id;
+        }
+        public String getName() {
+            return name;
+        }
+        public void setName(String name) {
+            this.name = name;
+        }
+    }
+
+    public static class TestResource {
+    
+        @PUT
+        @Path("setTest3")
+        @Produces("application/xml")
+        @Consumes("application/xml")
+        public TestCompositeObject setTest3(TestCompositeObject transfer) {
+            return transfer;
+        }
+    }
 }



Mime
View raw message