cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1127874 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ rt/frontend/jaxrs/src/main/resource...
Date Thu, 26 May 2011 11:28:19 GMT
Author: sergeyb
Date: Thu May 26 11:28:18 2011
New Revision: 1127874

URL: http://svn.apache.org/viewvc?rev=1127874&view=rev
Log:
[CXF-3549] Support for docLocation attribute

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/blueprint/jaxrs.xsd
    cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java
    cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java?rev=1127874&r1=1127873&r2=1127874&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
Thu May 26 11:28:18 2011
@@ -43,6 +43,7 @@ import org.apache.cxf.jaxrs.model.ClassR
 import org.apache.cxf.jaxrs.model.ProviderInfo;
 import org.apache.cxf.jaxrs.provider.ProviderFactory;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.service.factory.FactoryBeanListener;
 import org.apache.cxf.service.factory.ServiceConstructionException;
 import org.apache.cxf.service.invoker.Invoker;
@@ -75,6 +76,7 @@ public class JAXRSServerFactoryBean exte
     private Map<Object, Object> extensionMappings;
     private ResourceComparator rc;
     private ProviderInfo<Application> appProvider;
+    private String documentLocation;
     
     public JAXRSServerFactoryBean() {
         this(new JAXRSServiceFactoryBean());
@@ -160,9 +162,12 @@ public class JAXRSServerFactoryBean exte
             ProviderFactory factory = setupFactory(ep);
             ep.put(Application.class.getName(), appProvider);
             factory.setApplicationProvider(appProvider);
-            
             factory.setRequestPreprocessor(
                 new RequestPreprocessor(languageMappings, extensionMappings));
+            ep.put(Bus.class.getName(), getBus());
+            if (documentLocation != null) {
+                ep.put(JAXRSUtils.DOC_LOCATION, documentLocation);
+            }
             if (rc != null) {
                 ep.put("org.apache.cxf.jaxrs.comparator", rc);
             }
@@ -375,4 +380,20 @@ public class JAXRSServerFactoryBean exte
         }
         injectContexts();
     }
+
+    /**
+     * Set the reference to the document (WADL, etc) describing the endpoint
+     * @param documentLocation document location
+     */
+    public void setDocLocation(String docLocation) {
+        this.documentLocation = docLocation;
+    }
+
+    /**
+     * Get the reference to the document (WADL, etc) describing the endpoint
+     * @return document location
+     */
+    public String getDocLocation() {
+        return documentLocation;
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java?rev=1127874&r1=1127873&r2=1127874&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
Thu May 26 11:28:18 2011
@@ -42,6 +42,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.logging.Logger;
 
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
@@ -61,6 +62,7 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 
+import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.common.WSDLConstants;
 import org.apache.cxf.common.logging.LogUtils;
@@ -70,6 +72,7 @@ import org.apache.cxf.common.util.String
 import org.apache.cxf.common.util.XmlSchemaPrimitiveUtils;
 import org.apache.cxf.common.xmlschema.SchemaCollection;
 import org.apache.cxf.common.xmlschema.XmlSchemaConstants;
+import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.helpers.IOUtils;
@@ -162,6 +165,15 @@ public class WadlGenerator implements Re
             return Response.status(401).build();
         }
 
+        HttpHeaders headers = new HttpHeadersImpl(m);
+        MediaType type = headers.getAcceptableMediaTypes().contains(MediaType.APPLICATION_XML_TYPE)
+            ? MediaType.APPLICATION_XML_TYPE : WADL_TYPE;
+        
+        Response response = getExistingWadl(m, ui, type);
+        if (response != null) {
+            return response;
+        }
+        
         StringBuilder sbMain = new StringBuilder();
         sbMain.append("<application xmlns=\"").append(getNamespace())
               .append("\" xmlns:xs=\"").append(XmlSchemaConstants.XSD_NAMESPACE_URI).append("\"");
@@ -169,7 +181,7 @@ public class WadlGenerator implements Re
         sbGrammars.append("<grammars>");
 
         StringBuilder sbResources = new StringBuilder();
-        sbResources.append("<resources base=\"").append(ui.getBaseUri().toString()).append("\">");
+        sbResources.append("<resources base=\"").append(getBaseURI(ui)).append("\">");
 
         List<ClassResourceInfo> cris = getResourcesList(m, resource);
 
@@ -202,13 +214,13 @@ public class WadlGenerator implements Re
         sbMain.append("</application>");
 
         m.getExchange().put(JAXRSUtils.IGNORE_MESSAGE_WRITERS, ignoreMessageWriters);
-
-        HttpHeaders headers = new HttpHeadersImpl(m);
-        MediaType type = headers.getAcceptableMediaTypes().contains(MediaType.APPLICATION_XML_TYPE)
-                      ? MediaType.APPLICATION_XML_TYPE : WADL_TYPE;
         return Response.ok().type(type).entity(sbMain.toString()).build();
     }
 
+    private String getBaseURI(UriInfo ui) {
+        return ui.getBaseUri().toString();
+    }
+    
     private void handleGrammars(StringBuilder sbApp, StringBuilder sbGrammars,
                                 SchemaWriter writer, Map<Class<?>, QName> clsMap)
{
         if (writer == null) {
@@ -644,6 +656,32 @@ public class WadlGenerator implements Re
         return cri != null ? Collections.singletonList(cri)
                : ((JAXRSServiceImpl)m.getExchange().get(Service.class)).getClassResourceInfos();
     }
+    
+    //TODO: deal with caching later on
+    public Response getExistingWadl(Message m, UriInfo ui, MediaType mt) {
+        Endpoint ep = m.getExchange().get(Endpoint.class);
+        if (ep != null) {
+            String loc = (String)ep.get(JAXRSUtils.DOC_LOCATION);
+            if (loc != null) {
+                try {
+                    InputStream is = ResourceUtils.getResourceStream(loc, (Bus)ep.get(Bus.class.getName()));
+                    if (is != null) {
+                        Element docEl = DOMUtils.readXml(is).getDocumentElement();
+                        List<Element> resourceEls = DOMUtils.getChildrenWithName(docEl,

+                                                                                 WadlGenerator.WADL_NS,

+                                                                                 "resources");
+                        if (resourceEls.size() == 1) {
+                            DOMUtils.setAttribute(resourceEls.get(0), "base", getBaseURI(ui));
+                            return Response.ok().type(mt).entity(new DOMSource(docEl)).build();
+                        }
+                    }
+                } catch (Exception ex) {
+                    throw new WebApplicationException(ex, 400);
+                }
+            }
+        }
+        return null;
+    }
 
 
     private void generateQName(StringBuilder sb,
@@ -935,21 +973,7 @@ public class WadlGenerator implements Re
         for (int i = 0; i < locations.size(); i++) {
             String loc = locations.get(i);
             try {
-                InputStream is = ResourceUtils.getResourceStream(loc, BusFactory.getDefaultBus());
-                if (is == null) {
-                    return;
-                }
-                ByteArrayInputStream bis = IOUtils.loadIntoBAIS(is);
-                XMLSource source = new XMLSource(bis);
-                source.setBuffering(true);
-                String targetNs = source.getValue("/*/@targetNamespace");
-
-                Map<String, String> nsMap =
-                    Collections.singletonMap("xs", XmlSchemaConstants.XSD_NAMESPACE_URI);
-                String[] elementNames = source.getValues("/*/xs:element/@name", nsMap);
-                externalQnamesMap.put(targetNs, Arrays.asList(elementNames));
-                String schemaValue = source.getNode("/xs:schema", nsMap, String.class);
-                externalSchemasCache.add(schemaValue);
+                loadSchemasIntoCache(loc);
             } catch (Exception ex) {
                 LOG.warning("No schema resource " + loc + " can be loaded : " + ex.getMessage());
                 externalSchemasCache = null;
@@ -957,9 +981,28 @@ public class WadlGenerator implements Re
                 return;
             }
         }
-
     }
 
+    private void loadSchemasIntoCache(String loc) throws Exception {
+        InputStream is = ResourceUtils.getResourceStream(loc, BusFactory.getDefaultBus());
+        if (is == null) {
+            return;
+        }
+        ByteArrayInputStream bis = IOUtils.loadIntoBAIS(is);
+        XMLSource source = new XMLSource(bis);
+        source.setBuffering(true);
+        String targetNs = source.getValue("/*/@targetNamespace");
+
+        Map<String, String> nsMap =
+            Collections.singletonMap("xs", XmlSchemaConstants.XSD_NAMESPACE_URI);
+        String[] elementNames = source.getValues("/*/xs:element/@name", nsMap);
+        externalQnamesMap.put(targetNs, Arrays.asList(elementNames));
+        String schemaValue = source.getNode("/xs:schema", nsMap, String.class);
+        externalSchemasCache.add(schemaValue);
+        
+        
+    }
+    
     public void setUseJaxbContextForQnames(boolean checkJaxbOnly) {
         this.useJaxbContextForQnames = checkJaxbOnly;
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=1127874&r1=1127873&r2=1127874&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java Thu
May 26 11:28:18 2011
@@ -109,6 +109,7 @@ public final class JAXRSUtils {
     public static final String IGNORE_MESSAGE_WRITERS = "ignore.message.writers";
     public static final String ROOT_INSTANCE = "service.root.instance";
     public static final String ROOT_PROVIDER = "service.root.provider";
+    public static final String DOC_LOCATION = "wadl.location";
     
     private static final Logger LOG = LogUtils.getL7dLogger(JAXRSUtils.class);
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(JAXRSUtils.class);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/blueprint/jaxrs.xsd
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/blueprint/jaxrs.xsd?rev=1127874&r1=1127873&r2=1127874&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/blueprint/jaxrs.xsd (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/blueprint/jaxrs.xsd Thu May 26
11:28:18 2011
@@ -65,6 +65,7 @@
           <xsd:attribute name="staticSubresourceResolution" type="xsd:boolean"/>  
       
           <xsd:attribute name="beanNames" type="xsd:string"/>
           <xsd:attribute name="serviceName" type="xsd:QName"/>
+          <xsd:attribute name="docLocation" type="xsd:string"/>
         </xsd:extension>
       </xsd:complexContent>
     </xsd:complexType>

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd?rev=1127874&r1=1127873&r2=1127874&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/resources/schemas/jaxrs.xsd Thu May 26 11:28:18 2011
@@ -65,6 +65,7 @@
           <xsd:attribute name="staticSubresourceResolution" type="xsd:boolean"/>  
       
           <xsd:attribute name="beanNames" type="xsd:string"/>
           <xsd:attribute name="serviceName" type="xsd:QName"/>
+          <xsd:attribute name="docLocation" type="xsd:string"/>
         </xsd:extension>
       </xsd:complexContent>
     </xsd:complexType>

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java?rev=1127874&r1=1127873&r2=1127874&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
Thu May 26 11:28:18 2011
@@ -28,21 +28,27 @@ import java.net.URL;
 import java.net.URLConnection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.ws.rs.core.Response;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.methods.FileRequestEntity;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.RequestEntity;
+import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
 import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
+import org.apache.cxf.jaxrs.model.wadl.WadlGenerator;
 import org.apache.cxf.jaxrs.provider.AegisElementProvider;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
@@ -57,7 +63,7 @@ public class JAXRSClientServerSpringBook
     @BeforeClass
     public static void startServers() throws Exception {
         assertTrue("server did not launch correctly", 
-                   launchServer(BookServerSpring.class, true));
+                   launchServer(BookServerSpring.class));
     }
     
     @Test
@@ -90,6 +96,39 @@ public class JAXRSClientServerSpringBook
     }
     
     @Test
+    public void testGetWadlFromWadlLocation() throws Exception {
+        String address = "http://localhost:" + PORT + "/the/generated";    
+        checkWadlResourcesInfo(address, address + "/bookstore", 1);
+        
+    }
+    
+    private void checkWadlResourcesInfo(String baseURI, String requestURI, int size) throws
Exception {
+        WebClient client = WebClient.create(requestURI + "?_wadl&_type=xml");
+        WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(10000000L);
+        Document doc = DOMUtils.readXml(new InputStreamReader(client.get(InputStream.class),
"UTF-8"));
+        Element root = doc.getDocumentElement();
+        assertEquals(WadlGenerator.WADL_NS, root.getNamespaceURI());
+        assertEquals("application", root.getLocalName());
+        List<Element> grammarEls = DOMUtils.getChildrenWithName(root, 
+                                                                  WadlGenerator.WADL_NS,
"grammars");
+        assertEquals(1, grammarEls.size());
+        List<Element> includeEls = DOMUtils.getChildrenWithName(grammarEls.get(0),

+                                                                WadlGenerator.WADL_NS, "include");
+        assertEquals(1, includeEls.size());
+        String href = includeEls.get(0).getAttribute("href");
+        assertEquals("schemas/book.xsd", href);
+        List<Element> resourcesEls = DOMUtils.getChildrenWithName(root, 
+                                                                  WadlGenerator.WADL_NS,
"resources");
+        assertEquals(1, resourcesEls.size());
+        Element resourcesEl =  resourcesEls.get(0);
+        assertEquals(baseURI, resourcesEl.getAttribute("base"));
+        List<Element> resourceEls = 
+            DOMUtils.getChildrenWithName(resourcesEl, 
+                                         WadlGenerator.WADL_NS, "resource");
+        assertEquals(size, resourceEls.size());
+    }
+    
+    @Test
     public void testGetBookByUriInfo() throws Exception {
         String endpointAddress =
             "http://localhost:" + PORT + "/the/thebooks/bookstore/bookinfo?"

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java?rev=1127874&r1=1127873&r2=1127874&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSSoapBookTest.java
Thu May 26 11:28:18 2011
@@ -760,7 +760,7 @@ public class JAXRSSoapBookTest extends A
         bean.setResourceClass(BookStoreJaxrsJaxws.class);
         TestFeature testFeature = new TestFeature();
         List<AbstractFeature> features = new ArrayList<AbstractFeature>();
-        features.add((AbstractFeature)testFeature);
+        features.add(testFeature);
         bean.setFeatures(features);
         BookStoreJaxrsJaxws proxy = (BookStoreJaxrsJaxws)bean.create();
         Book b = proxy.getBook(new Long("123"));

Modified: cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml?rev=1127874&r1=1127873&r2=1127874&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml (original)
+++ cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml Thu May 26 11:28:18
2011
@@ -182,7 +182,8 @@ http://cxf.apache.org/schemas/core.xsd">
     </jaxrs:serviceBeans>
   </jaxrs:server>
 
-  <jaxrs:server id="bookserviceGenerated" address="/generated">
+  <jaxrs:server id="bookserviceGenerated" address="/generated"
+    docLocation="classpath:/wadl/bookstoreImport.xml">
     <jaxrs:serviceBeans>
       <bean class="org.apache.cxf.systest.jaxrs.codegen.service.BookStore"/>
     </jaxrs:serviceBeans>



Mime
View raw message