Repository: cxf Updated Branches: refs/heads/master 7f7be98e9 -> e05f42938
[CXF-5648] Adding an optional 'schemas' property to SchemaValidation and checking it in JAXB providers Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/e05f4293 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/e05f4293 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/e05f4293 Branch: refs/heads/master Commit: e05f429384cd199dd8aae19afc0079027f0c98b6 Parents: 7f7be98 Author: Sergey Beryozkin <[email protected]> Authored: Fri Apr 4 09:59:18 2014 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Fri Apr 4 09:59:18 2014 +0100 ---------------------------------------------------------------------- .../cxf/annotations/SchemaValidation.java | 14 +++++++---- .../factory/AnnotationsFactoryBeanListener.java | 8 +------ .../jaxrs/provider/AbstractJAXBProvider.java | 25 ++++++++++++++++---- .../jaxrs/provider/JAXBElementProviderTest.java | 12 ++++++++++ 4 files changed, 43 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/e05f4293/core/src/main/java/org/apache/cxf/annotations/SchemaValidation.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/annotations/SchemaValidation.java b/core/src/main/java/org/apache/cxf/annotations/SchemaValidation.java index d0d6e72..2d338ad 100644 --- a/core/src/main/java/org/apache/cxf/annotations/SchemaValidation.java +++ b/core/src/main/java/org/apache/cxf/annotations/SchemaValidation.java @@ -35,10 +35,16 @@ public @interface SchemaValidation { public enum SchemaValidationType { IN, OUT, BOTH, NONE } - - @Deprecated - boolean enabled() default true; - + /** + * Schema Validation Type + * @return validation type + */ SchemaValidationType type() default SchemaValidationType.BOTH; + + /** + * Optional list of schema locations + * @return schema locations + */ + String[] schemas() default { }; } http://git-wip-us.apache.org/repos/asf/cxf/blob/e05f4293/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java b/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java index 7100f2c..655eb68 100644 --- a/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java +++ b/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java @@ -32,7 +32,6 @@ import org.apache.cxf.annotations.FastInfoset; import org.apache.cxf.annotations.GZIP; import org.apache.cxf.annotations.Logging; import org.apache.cxf.annotations.SchemaValidation; -import org.apache.cxf.annotations.SchemaValidation.SchemaValidationType; import org.apache.cxf.annotations.WSDLDocumentation; import org.apache.cxf.annotations.WSDLDocumentation.Placement; import org.apache.cxf.annotations.WSDLDocumentationCollection; @@ -291,16 +290,11 @@ public class AnnotationsFactoryBeanListener implements FactoryBeanListener { * @param endpoint * @param annotation */ - @SuppressWarnings("deprecation") private void addSchemaValidationSupport(Endpoint endpoint, SchemaValidation annotation) { if (annotation != null) { // if someone has gone to the effort of specifying enabled=false, then we need to // handle that, otherwise we use the new SchemaValidationType type only - if (!annotation.enabled()) { - endpoint.put(Message.SCHEMA_VALIDATION_ENABLED, SchemaValidationType.NONE); - } else { - endpoint.put(Message.SCHEMA_VALIDATION_ENABLED, annotation.type()); - } + endpoint.put(Message.SCHEMA_VALIDATION_ENABLED, annotation.type()); } } http://git-wip-us.apache.org/repos/asf/cxf/blob/e05f4293/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java index 90149ec..e446e45 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java @@ -32,6 +32,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.ResourceBundle; @@ -67,6 +68,7 @@ import org.w3c.dom.Element; import org.xml.sax.helpers.DefaultHandler; +import org.apache.cxf.annotations.SchemaValidation; import org.apache.cxf.common.i18n.BundleUtils; import org.apache.cxf.common.jaxb.JAXBUtils; import org.apache.cxf.common.logging.LogUtils; @@ -209,6 +211,24 @@ public abstract class AbstractJAXBProvider<T> extends AbstractConfigurableProvid } } } + if (cris != null) { + List<String> schemaLocs = new LinkedList<String>(); + for (ClassResourceInfo cri : cris) { + SchemaValidation sv = cri.getServiceClass().getAnnotation(SchemaValidation.class); + if (sv != null && sv.schemas() != null) { + for (String s : sv.schemas()) { + String theSchema = s; + if (!theSchema.startsWith("classpath:")) { + theSchema = "classpath:" + theSchema; + } + schemaLocs.add(theSchema); + } + } + } + if (!schemaLocs.isEmpty()) { + this.setSchemaLocations(schemaLocs); + } + } } public void setContextProperties(Map<String, Object> contextProperties) { @@ -428,11 +448,6 @@ public abstract class AbstractJAXBProvider<T> extends AbstractConfigurableProvid return unmarshalAsJaxbElement && type != Response.class; } - @Deprecated - public void setSchemas(List<String> locations) { - setSchemaLocations(locations); - } - public void setSchemaLocations(List<String> locations) { schema = SchemaHandler.createSchema(locations, catalogLocation, getBus()); } http://git-wip-us.apache.org/repos/asf/cxf/blob/e05f4293/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java index 7dc8954..f69d8b9 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JAXBElementProviderTest.java @@ -67,6 +67,7 @@ import org.w3c.dom.Node; import org.xml.sax.ContentHandler; +import org.apache.cxf.annotations.SchemaValidation; import org.apache.cxf.endpoint.Endpoint; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.jaxrs.ext.MessageContextImpl; @@ -1285,6 +1286,16 @@ public class JAXBElementProviderTest extends Assert { } @Test + public void testSetSchemasFromAnnotation() { + JAXBElementProvider<?> provider = new JAXBElementProvider<Object>(); + ClassResourceInfo cri = + ResourceUtils.createClassResourceInfo(JAXBResource.class, JAXBResource.class, true, true); + provider.init(Collections.singletonList(cri)); + Schema s = provider.getSchema(); + assertNotNull("schema can not be read from classpath", s); + } + + @Test public void testSetSchemasFromDisk() throws Exception { JAXBElementProvider<?> provider = new JAXBElementProvider<Object>(); List<String> locations = new ArrayList<String>(); @@ -1567,6 +1578,7 @@ public class JAXBElementProviderTest extends Assert { } @Path("/") + @SchemaValidation(schemas = "/test.xsd") public static class JAXBResource { @GET
