Author: sergeyb
Date: Fri Jun 22 16:48:11 2012
New Revision: 1352965
URL: http://svn.apache.org/viewvc?rev=1352965&view=rev
Log:
[CXF-4393] Initial support for checking custom catalogs when creating JAXB
Schemas
Added:
cxf/trunk/api/src/main/java/org/apache/cxf/common/xmlschema/LSInputImpl.java
(with props)
cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/META-INF/
cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/META-INF/jax-rs-catalog.xml
(with props)
Modified:
cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/book.xsd
cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml
Added:
cxf/trunk/api/src/main/java/org/apache/cxf/common/xmlschema/LSInputImpl.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/common/xmlschema/LSInputImpl.java?rev=1352965&view=auto
==============================================================================
---
cxf/trunk/api/src/main/java/org/apache/cxf/common/xmlschema/LSInputImpl.java
(added)
+++
cxf/trunk/api/src/main/java/org/apache/cxf/common/xmlschema/LSInputImpl.java
Fri Jun 22 16:48:11 2012
@@ -0,0 +1,122 @@
+/**
+ * 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.
+ */
+package org.apache.cxf.common.xmlschema;
+
+import java.io.InputStream;
+import java.io.Reader;
+
+import org.w3c.dom.ls.LSInput;
+
+/**
+ * LSInput implementation that can be used in
+ * custom LSResourceResolver implementations for getting
+ * schema import and include references resolved.
+ */
+public class LSInputImpl implements LSInput {
+
+ protected String fPublicId;
+
+ protected String fSystemId;
+
+ protected String fBaseSystemId;
+
+ protected InputStream fByteStream;
+
+ protected Reader fCharStream;
+
+ protected String fData;
+
+ protected String fEncoding;
+
+ protected boolean fCertifiedText;
+
+ public LSInputImpl() {
+ }
+
+ public LSInputImpl(String publicId, String systemId, InputStream
byteStream) {
+ fPublicId = publicId;
+ fSystemId = systemId;
+ fByteStream = byteStream;
+ }
+
+ public InputStream getByteStream() {
+ return fByteStream;
+ }
+
+ public void setByteStream(InputStream byteStream) {
+ fByteStream = byteStream;
+ }
+
+ public Reader getCharacterStream() {
+ return fCharStream;
+ }
+
+ public void setCharacterStream(Reader characterStream) {
+ fCharStream = characterStream;
+ }
+
+ public String getStringData() {
+ return fData;
+ }
+
+ public void setStringData(String stringData) {
+ fData = stringData;
+ }
+
+ public String getEncoding() {
+ return fEncoding;
+ }
+
+ public void setEncoding(String encoding) {
+ fEncoding = encoding;
+ }
+
+ public String getPublicId() {
+ return fPublicId;
+ }
+
+ public void setPublicId(String publicId) {
+ fPublicId = publicId;
+ }
+
+ public String getSystemId() {
+ return fSystemId;
+ }
+
+ public void setSystemId(String systemId) {
+ fSystemId = systemId;
+ }
+
+ public String getBaseURI() {
+ return fBaseSystemId;
+ }
+
+ public void setBaseURI(String baseURI) {
+ fBaseSystemId = baseURI;
+ }
+
+ public boolean getCertifiedText() {
+ return fCertifiedText;
+ }
+
+ public void setCertifiedText(boolean certifiedText) {
+ fCertifiedText = certifiedText;
+ }
+
+}
Propchange:
cxf/trunk/api/src/main/java/org/apache/cxf/common/xmlschema/LSInputImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/api/src/main/java/org/apache/cxf/common/xmlschema/LSInputImpl.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java?rev=1352965&r1=1352964&r2=1352965&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java
(original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/wsdl/EndpointReferenceUtils.java
Fri Jun 22 16:48:11 2012
@@ -22,7 +22,6 @@ package org.apache.cxf.wsdl;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.Reader;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
@@ -73,6 +72,7 @@ import org.apache.cxf.common.i18n.Messag
import org.apache.cxf.common.jaxb.JAXBContextCache;
import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.common.xmlschema.LSInputImpl;
import org.apache.cxf.endpoint.EndpointResolverRegistry;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.endpoint.ServerRegistry;
@@ -1097,97 +1097,3 @@ public final class EndpointReferenceUtil
}
-
-
-class LSInputImpl implements LSInput {
-
- protected String fPublicId;
-
- protected String fSystemId;
-
- protected String fBaseSystemId;
-
- protected InputStream fByteStream;
-
- protected Reader fCharStream;
-
- protected String fData;
-
- protected String fEncoding;
-
- protected boolean fCertifiedText;
-
- public LSInputImpl() {
- }
-
- public LSInputImpl(String publicId, String systemId, InputStream
byteStream) {
- fPublicId = publicId;
- fSystemId = systemId;
- fByteStream = byteStream;
- }
-
- public InputStream getByteStream() {
- return fByteStream;
- }
-
- public void setByteStream(InputStream byteStream) {
- fByteStream = byteStream;
- }
-
- public Reader getCharacterStream() {
- return fCharStream;
- }
-
- public void setCharacterStream(Reader characterStream) {
- fCharStream = characterStream;
- }
-
- public String getStringData() {
- return fData;
- }
-
- public void setStringData(String stringData) {
- fData = stringData;
- }
-
- public String getEncoding() {
- return fEncoding;
- }
-
- public void setEncoding(String encoding) {
- fEncoding = encoding;
- }
-
- public String getPublicId() {
- return fPublicId;
- }
-
- public void setPublicId(String publicId) {
- fPublicId = publicId;
- }
-
- public String getSystemId() {
- return fSystemId;
- }
-
- public void setSystemId(String systemId) {
- fSystemId = systemId;
- }
-
- public String getBaseURI() {
- return fBaseSystemId;
- }
-
- public void setBaseURI(String baseURI) {
- fBaseSystemId = baseURI;
- }
-
- public boolean getCertifiedText() {
- return fCertifiedText;
- }
-
- public void setCertifiedText(boolean certifiedText) {
- fCertifiedText = certifiedText;
- }
-
-}
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1352965&r1=1352964&r2=1352965&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Fri Jun 22 16:48:11 2012
@@ -112,6 +112,7 @@ public abstract class AbstractJAXBProvid
private MessageContext mc;
private Schema schema;
+ private String catalogLocation;
private String collectionWrapperName;
private Map<String, String> collectionWrapperMap;
private List<String> jaxbElementClassNames = Collections.emptyList();
@@ -369,7 +370,11 @@ public abstract class AbstractJAXBProvid
}
public void setSchemaLocations(List<String> locations) {
- schema = SchemaHandler.createSchema(locations, getBus());
+ schema = SchemaHandler.createSchema(locations, catalogLocation,
getBus());
+ }
+
+ public void setCatalogLocation(String name) {
+ this.catalogLocation = name;
}
public void setSchema(Schema s) {
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java?rev=1352965&r1=1352964&r2=1352965&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/schemas/SchemaHandler.java
Fri Jun 22 16:48:11 2012
@@ -20,6 +20,8 @@
package org.apache.cxf.jaxrs.utils.schemas;
import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
@@ -32,18 +34,26 @@ import javax.xml.transform.stream.Stream
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSResourceResolver;
+
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
+import org.apache.cxf.catalog.OASISCatalogManager;
import org.apache.cxf.common.WSDLConstants;
import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.common.xmlschema.LSInputImpl;
import org.apache.cxf.jaxrs.utils.ResourceUtils;
public class SchemaHandler {
+ static final String DEFAULT_CATALOG_LOCATION =
"classpath:META-INF/jax-rs-catalog.xml";
+
private static final Logger LOG =
LogUtils.getL7dLogger(SchemaHandler.class);
private Schema schema;
private Bus bus;
+ private String catalogLocation;
public SchemaHandler() {
@@ -54,14 +64,19 @@ public class SchemaHandler {
}
public void setSchemas(List<String> locations) {
- schema = createSchema(locations, bus == null ?
BusFactory.getThreadDefaultBus() : bus);
+ schema = createSchema(locations, catalogLocation,
+ bus == null ? BusFactory.getThreadDefaultBus() :
bus);
+ }
+
+ public void setCatalogLocation(String name) {
+ this.catalogLocation = name;
}
public Schema getSchema() {
return schema;
}
- public static Schema createSchema(List<String> locations, Bus bus) {
+ public static Schema createSchema(List<String> locations, String
catalogLocation, final Bus bus) {
SchemaFactory factory =
SchemaFactory.newInstance(WSDLConstants.NS_SCHEMA_XSD);
Schema s = null;
@@ -78,6 +93,46 @@ public class SchemaHandler {
source.setSystemId(url.toString());
sources.add(source);
}
+ final OASISCatalogManager catalogResolver =
OASISCatalogManager.getCatalogManager(bus);
+ if (catalogResolver != null) {
+ catalogLocation = catalogLocation == null
+ ? SchemaHandler.DEFAULT_CATALOG_LOCATION : catalogLocation;
+ URL catalogURL = ResourceUtils.getResourceURL(catalogLocation,
bus);
+ if (catalogURL != null) {
+ try {
+ catalogResolver.loadCatalog(catalogURL);
+ factory.setResourceResolver(new LSResourceResolver() {
+
+ public LSInput resolveResource(String type, String
namespaceURI, String publicId,
+ String systemId,
String baseURI) {
+ try {
+ String resolvedLocation =
catalogResolver.resolveSystem(systemId);
+
+ if (resolvedLocation == null) {
+ resolvedLocation =
catalogResolver.resolveURI(namespaceURI);
+ }
+ if (resolvedLocation == null) {
+ resolvedLocation =
catalogResolver.resolvePublic(publicId, systemId);
+ }
+ if (resolvedLocation != null) {
+ InputStream resourceStream =
+
ResourceUtils.getResourceStream(resolvedLocation, bus);
+ if (resourceStream != null) {
+ return new LSInputImpl(publicId,
systemId, resourceStream);
+ }
+ }
+ } catch (Exception ex) {
+ // ignore
+ }
+ return null;
+ }
+
+ });
+ } catch (IOException ex) {
+ LOG.warning("Catalog " + catalogLocation + " can not
be loaded");
+ }
+ }
+ }
s = factory.newSchema(sources.toArray(new Source[]{}));
} catch (Exception ex) {
LOG.warning("Validation will be disabled, failed to create schema
: " + ex.getMessage());
@@ -85,4 +140,5 @@ public class SchemaHandler {
return s;
}
+
}
Modified:
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/book.xsd
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/book.xsd?rev=1352965&r1=1352964&r2=1352965&view=diff
==============================================================================
---
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/book.xsd
(original)
+++
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/book.xsd
Fri Jun 22 16:48:11 2012
@@ -19,7 +19,7 @@
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/books"
xmlns:tns="http://www.example.org/books"
xmlns:bookids="http://www.example.org/books/id" elementFormDefault="qualified">
- <xs:import namespace="http://www.example.org/books/id"/>
+ <xs:import namespace="http://www.example.org/books/id"
schemaLocation="http://schemas/bookid.xsd"/>
<element name="Book">
<complexType>
<sequence>
Added:
cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/META-INF/jax-rs-catalog.xml
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/META-INF/jax-rs-catalog.xml?rev=1352965&view=auto
==============================================================================
---
cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/META-INF/jax-rs-catalog.xml
(added)
+++
cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/META-INF/jax-rs-catalog.xml
Fri Jun 22 16:48:11 2012
@@ -0,0 +1,22 @@
+<?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.
+-->
+<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog" prefer="system">
+ <system systemId="http://schemas/bookid.xsd"
uri="classpath:WEB-INF/schemas/bookid.xsd"/>
+</catalog>
Propchange:
cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/META-INF/jax-rs-catalog.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/META-INF/jax-rs-catalog.xml
------------------------------------------------------------------------------
svn:keywords = Rev Date
Propchange:
cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/META-INF/jax-rs-catalog.xml
------------------------------------------------------------------------------
svn:mime-type = text/xml
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=1352965&r1=1352964&r2=1352965&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 Fri Jun
22 16:48:11 2012
@@ -269,8 +269,7 @@ http://cxf.apache.org/schemas/core.xsd">
</bean>
<util:list id="theSchemas">
- <value>classpath:/WEB-INF/schemas/bookid.xsd</value>
- <value>classpath:/org/apache/cxf/systest/jaxrs/resources/book.xsd</value>
+ <value>classpath:/org/apache/cxf/systest/jaxrs/resources/book.xsd</value>
</util:list>
<util:list id="jsonTypes">