You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2013/04/03 15:55:29 UTC
svn commit: r1464017 - in /cxf/trunk:
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/
systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
systests/jaxrs/src/test/resources/jaxrs/WEB-INF/
systests/jaxrs/src/test/resources/wadl/
Author: sergeyb
Date: Wed Apr 3 13:55:28 2013
New Revision: 1464017
URL: http://svn.apache.org/r1464017
Log:
[CXF-4924] Initial update WADLGenerator to support the retrieval of the documents containing the referenced resource_types
Added:
cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.xml (with props)
cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResourceType.xml (with props)
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/wadl/WadlGenerator.java
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.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/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=1464017&r1=1464016&r2=1464017&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 Wed Apr 3 13:55:28 2013
@@ -140,7 +140,7 @@ public class WadlGenerator implements Co
private List<URI> externalSchemaLinks;
private Map<String, List<String>> externalQnamesMap;
- private ConcurrentHashMap<String, String> schemaLocationMap =
+ private ConcurrentHashMap<String, String> docLocationMap =
new ConcurrentHashMap<String, String>();
private ElementQNameResolver resolver;
@@ -180,13 +180,13 @@ public class WadlGenerator implements Co
UriInfo ui = context.getUriInfo();
if (!ui.getQueryParameters().containsKey(WADL_QUERY)) {
- if (!schemaLocationMap.isEmpty()) {
+ if (!docLocationMap.isEmpty()) {
String path = ui.getPath(false);
if (path.startsWith("/") && path.length() > 0) {
path = path.substring(1);
}
- if (schemaLocationMap.containsKey(path)) {
- context.abortWith(getExistingSchema(m, ui, path));
+ if (docLocationMap.containsKey(path)) {
+ context.abortWith(getExistingResource(m, ui, path));
}
}
return;
@@ -887,15 +887,21 @@ public class WadlGenerator implements Co
WadlGenerator.WADL_NS,
"grammars");
if (grammarEls.size() == 1) {
- handleSchemaRefs(DOMUtils.getChildrenWithName(grammarEls.get(0),
+ handleExistingDocRefs(DOMUtils.getChildrenWithName(grammarEls.get(0),
WadlGenerator.WADL_NS, "include"), "href", loc, "", m, ui);
}
- List<Element> resourceEls = DOMUtils.getChildrenWithName(appEl,
+ List<Element> resourcesEls = DOMUtils.getChildrenWithName(appEl,
WadlGenerator.WADL_NS,
"resources");
- if (resourceEls.size() == 1) {
- DOMUtils.setAttribute(resourceEls.get(0), "base", getBaseURI(m, ui));
+ if (resourcesEls.size() == 1) {
+ DOMUtils.setAttribute(resourcesEls.get(0), "base", getBaseURI(m, ui));
+
+ List<Element> resourceEls = DOMUtils.getChildrenWithName(resourcesEls.get(0),
+ WadlGenerator.WADL_NS,
+ "resource");
+ handleExistingDocRefs(resourceEls, "type", loc, "", m, ui);
+
return Response.ok().type(mt).entity(new DOMSource(appEl)).build();
}
@@ -909,18 +915,34 @@ public class WadlGenerator implements Co
}
//TODO: deal with caching later on
- public Response getExistingSchema(Message m, UriInfo ui, String href) {
- String loc = schemaLocationMap.get(href);
+ public Response getExistingResource(Message m, UriInfo ui, String href) {
+ String loc = docLocationMap.get(href);
Endpoint ep = m.getExchange().get(Endpoint.class);
if (ep != null && loc != null) {
try {
+ int fragmentIndex = loc.lastIndexOf("#");
+ if (fragmentIndex != -1) {
+ loc = loc.substring(0, fragmentIndex);
+ }
InputStream is = ResourceUtils.getResourceStream(loc, (Bus)ep.get(Bus.class.getName()));
if (is != null) {
Element docEl = DOMUtils.readXml(is).getDocumentElement();
- handleSchemaRefs(DOMUtils.getChildrenWithName(docEl,
- XmlSchemaConstants.XSD_NAMESPACE_URI, "import"), "schemaLocation", loc, href, m, ui);
- handleSchemaRefs(DOMUtils.getChildrenWithName(docEl,
- XmlSchemaConstants.XSD_NAMESPACE_URI, "include"), "schemaLocation", loc, href, m, ui);
+ if (fragmentIndex != -1) {
+ List<Element> grammarEls = DOMUtils.getChildrenWithName(docEl,
+ WadlGenerator.WADL_NS,
+ "grammars");
+ if (grammarEls.size() == 1) {
+ handleExistingDocRefs(DOMUtils.getChildrenWithName(grammarEls.get(0),
+ WadlGenerator.WADL_NS, "include"), "href", loc, href, m, ui);
+ }
+ } else {
+ handleExistingDocRefs(DOMUtils.getChildrenWithName(docEl,
+ XmlSchemaConstants.XSD_NAMESPACE_URI, "import"), "schemaLocation", loc, href, m, ui);
+ handleExistingDocRefs(DOMUtils.getChildrenWithName(docEl,
+ XmlSchemaConstants.XSD_NAMESPACE_URI, "include"), "schemaLocation", loc, href, m, ui);
+ }
+
+
return Response.ok().type(MediaType.APPLICATION_XML_TYPE).entity(
new DOMSource(docEl)).build();
}
@@ -932,7 +954,7 @@ public class WadlGenerator implements Co
return null;
}
- private void handleSchemaRefs(List<Element> schemaRefEls, String attrName,
+ private void handleExistingDocRefs(List<Element> elements, String attrName,
String parentDocLoc, String parentRef, Message m, UriInfo ui) {
int index = parentDocLoc.lastIndexOf('/');
parentDocLoc = index == -1 ? parentDocLoc : parentDocLoc.substring(0, index + 1);
@@ -940,13 +962,25 @@ public class WadlGenerator implements Co
index = parentRef.lastIndexOf('/');
parentRef = index == -1 ? "" : parentRef.substring(0, index + 1);
- for (Element schemaRefEl : schemaRefEls) {
- String href = schemaRefEl.getAttribute(attrName);
- if (!StringUtils.isEmpty(href)) {
+ for (Element element : elements) {
+ String href = element.getAttribute(attrName);
+ String originalRef = href;
+ if (!StringUtils.isEmpty(href) && !href.startsWith("#")) {
+ int fragmentIndex = href.lastIndexOf("#");
+ String fragment = null;
+ if (fragmentIndex != -1) {
+ fragment = href.substring(fragmentIndex + 1);
+ href = href.substring(0, fragmentIndex);
+ }
+
String actualRef = parentRef + href;
- schemaLocationMap.put(actualRef, parentDocLoc + href);
- URI schemaURI = UriBuilder.fromUri(getBaseURI(m, ui)).path(actualRef).build();
- DOMUtils.setAttribute(schemaRefEl, attrName, schemaURI.toString());
+ docLocationMap.put(actualRef, parentDocLoc + originalRef);
+ if (fragment != null) {
+ docLocationMap.put(parentRef + originalRef, parentDocLoc + originalRef);
+ }
+ UriBuilder ub = UriBuilder.fromUri(getBaseURI(m, ui)).path(actualRef).fragment(fragment);
+ URI schemaURI = ub.build();
+ DOMUtils.setAttribute(element, attrName, schemaURI.toString());
}
}
}
@@ -1398,7 +1432,7 @@ public class WadlGenerator implements Co
if (href.startsWith("classpath:")) {
int index = href.lastIndexOf('/');
href = index == -1 ? href.substring(9) : href.substring(index + 1);
- schemaLocationMap.put(href, s);
+ docLocationMap.put(href, s);
}
externalSchemaLinks.add(URI.create(href));
} catch (Exception ex) {
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=1464017&r1=1464016&r2=1464017&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 Wed Apr 3 13:55:28 2013
@@ -134,10 +134,18 @@ public class JAXRSClientServerSpringBook
@Test
public void testGetWadlFromWadlLocation() throws Exception {
String address = "http://localhost:" + PORT + "/the/generated";
- checkWadlResourcesInfo(address, address + "/bookstore", "/schemas/book.xsd", 1);
-
+ List<Element> resources =
+ checkWadlResourcesInfo(address, address + "/bookstore", "/schemas/book.xsd", 2);
+ assertEquals("", resources.get(0).getAttribute("type"));
+ String type = resources.get(1).getAttribute("type");
+ String resourceTypeAddress = address + "/bookstoreImportResourceType.xml#bookstoreType";
+ assertEquals(resourceTypeAddress, type);
+
checkSchemas(address, "/schemas/book.xsd", "/schemas/chapter.xsd", "include");
checkSchemas(address, "/schemas/chapter.xsd", null, null);
+
+ // check resource type resource
+ checkWadlResourcesType(address, resourceTypeAddress, "/schemas/book.xsd");
}
@Test
@@ -169,7 +177,31 @@ public class JAXRSClientServerSpringBook
}
- private void checkWadlResourcesInfo(String baseURI, String requestURI,
+ private void checkWadlResourcesType(String baseURI, String requestTypeURI, String schemaRef) throws Exception {
+ WebClient client = WebClient.create(requestTypeURI);
+ WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(1000000);
+
+ 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(baseURI + schemaRef, href);
+ List<Element> resourcesEls = DOMUtils.getChildrenWithName(root,
+ WadlGenerator.WADL_NS, "resources");
+ assertEquals(0, resourcesEls.size());
+ List<Element> resourceTypeEls =
+ DOMUtils.getChildrenWithName(root, WadlGenerator.WADL_NS, "resource_type");
+ assertEquals(1, resourceTypeEls.size());
+ }
+
+ private List<Element> checkWadlResourcesInfo(String baseURI, String requestURI,
String schemaRef, int size) throws Exception {
WebClient client = WebClient.create(requestURI + "?_wadl&_type=xml");
Document doc = DOMUtils.readXml(new InputStreamReader(client.get(InputStream.class), "UTF-8"));
@@ -193,6 +225,8 @@ public class JAXRSClientServerSpringBook
DOMUtils.getChildrenWithName(resourcesEl,
WadlGenerator.WADL_NS, "resource");
assertEquals(size, resourceEls.size());
+ return resourceEls;
+
}
@Test
@@ -208,7 +242,6 @@ public class JAXRSClientServerSpringBook
String endpointAddress =
"http://localhost:" + PORT + "/the/thebooks/bookstore/semicolon%3B";
WebClient client = WebClient.create(endpointAddress);
- WebClient.getConfig(client).getHttpConduit().getClient().setReceiveTimeout(1000000);
Book book = client.get(Book.class);
assertEquals(333L, book.getId());
assertEquals(";", book.getName());
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=1464017&r1=1464016&r2=1464017&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 Wed Apr 3 13:55:28 2013
@@ -209,7 +209,7 @@ http://cxf.apache.org/schemas/core.xsd">
</jaxrs:server>
<jaxrs:server id="bookserviceGenerated" address="/generated"
- docLocation="classpath:/wadl/bookstoreImport.xml">
+ docLocation="classpath:/wadl/bookstoreImportResource.xml">
<jaxrs:serviceBeans>
<bean class="org.apache.cxf.systest.jaxrs.codegen.service.BookStore"/>
</jaxrs:serviceBeans>
Added: cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.xml?rev=1464017&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.xml (added)
+++ cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.xml Wed Apr 3 13:55:28 2013
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prefix1="http://superbooks">
+
+ <grammars>
+ <include href="schemas/book.xsd"/>
+ </grammars>
+ <resources base="http://localhost:8080/baz">
+ <resource path="/bookstore" id="{wadl.model.jaxrs.cxf.apache.org}BookStore">
+
+ <resource path="/books/{bookid}">
+ <param name="bookid" style="template" type="xs:int"/>
+
+ <method name="POST" id="addBook">
+ <request>
+ <representation mediaType="application/xml" element="prefix1:thebook"/>
+ </request>
+ </method>
+ </resource>
+ </resource>
+ <resource path="/bookstore2" type="bookstoreImportResourceType.xml#bookstoreType"/>
+
+</resources>
+
+</application>
Propchange: cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResource.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
Added: cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResourceType.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResourceType.xml?rev=1464017&view=auto
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResourceType.xml (added)
+++ cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResourceType.xml Wed Apr 3 13:55:28 2013
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prefix1="http://superbooks">
+
+ <grammars>
+ <include href="schemas/book.xsd"/>
+ </grammars>
+
+ <method name="POST" id="addBook">
+ <request>
+ <representation mediaType="application/xml" element="prefix1:thebook"/>
+ </request>
+ </method>
+
+ <resource_type id="bookstoreType">
+ <resource path="/books/{bookid}">
+ <param name="bookid" style="template" type="xs:int"/>
+
+ <method href="#addBook"/>
+
+ </resource>
+ </resource_type>
+
+</application>
Propchange: cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResourceType.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResourceType.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange: cxf/trunk/systests/jaxrs/src/test/resources/wadl/bookstoreImportResourceType.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml