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 2011/12/04 16:57:05 UTC

svn commit: r1210127 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/

Author: sergeyb
Date: Sun Dec  4 15:57:04 2011
New Revision: 1210127

URL: http://svn.apache.org/viewvc?rev=1210127&view=rev
Log:
[CXF-3952] Adding SchemaLocation annotation

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/SchemaLocation.java   (with props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/SchemaLocation.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/SchemaLocation.java?rev=1210127&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/SchemaLocation.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/SchemaLocation.java Sun Dec  4 15:57:04 2011
@@ -0,0 +1,42 @@
+/**
+ * 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.jaxrs.ext.xml;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * This annotation can be used to simplify adding 
+ * xsi:schemaLocation attributes
+ */
+@Target({ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface SchemaLocation {
+    /**
+     * schema location relative to a base URI of the web application
+     */
+    String value();
+    /**
+     * Can be used to get xsi:noNamespaceSchemaLocation produced.
+     * By default, xsi:schemaLocation will be set.
+     */
+    boolean noNamespace() default false;
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/SchemaLocation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/SchemaLocation.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java?rev=1210127&r1=1210126&r2=1210127&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java Sun Dec  4 15:57:04 2011
@@ -57,6 +57,7 @@ import javax.xml.transform.Source;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxb.NamespaceMapper;
 import org.apache.cxf.jaxrs.ext.MessageContext;
+import org.apache.cxf.jaxrs.ext.xml.SchemaLocation;
 import org.apache.cxf.jaxrs.ext.xml.XMLInstruction;
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
 import org.apache.cxf.jaxrs.utils.AnnotationUtils;
@@ -74,13 +75,21 @@ import org.apache.cxf.staxutils.transfor
 @Provider
 public class JAXBElementProvider extends AbstractJAXBProvider  {
     private static final String XML_PI_START = "<?xml version=\"1.0\" encoding=\"";
+    private static final String NS_MAPPER_PROPERTY = "com.sun.xml.bind.namespacePrefixMapper";
+    private static final String NS_MAPPER_PROPERTY_INT = "com.sun.xml.internal.bind.namespacePrefixMapper";
+    private static final String XML_PI_PROPERTY = "com.sun.xml.bind.xmlHeaders";
+    private static final String XML_PI_PROPERTY_INT = "com.sun.xml.internal.bind.xmlHeaders";
     
     private static final List<String> MARSHALLER_PROPERTIES =
         Arrays.asList(new String[] {Marshaller.JAXB_ENCODING,
                                     Marshaller.JAXB_FORMATTED_OUTPUT,
                                     Marshaller.JAXB_FRAGMENT,
                                     Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION,
-                                    Marshaller.JAXB_SCHEMA_LOCATION});
+                                    Marshaller.JAXB_SCHEMA_LOCATION,
+                                    NS_MAPPER_PROPERTY,
+                                    NS_MAPPER_PROPERTY_INT,
+                                    XML_PI_PROPERTY,
+                                    XML_PI_PROPERTY_INT});
     
     private Map<String, Object> mProperties = Collections.emptyMap();
     private Map<String, String> nsPrefixes = Collections.emptyMap();
@@ -348,7 +357,7 @@ public class JAXBElementProvider extends
     
     protected static void setNamespaceMapper(Marshaller ms, Map<String, String> map) throws Exception {
         NamespaceMapper nsMapper = new NamespaceMapper(map);
-        setMarshallerProp(ms, nsMapper, "namespacePrefixMapper");
+        setMarshallerProp(ms, nsMapper, NS_MAPPER_PROPERTY, NS_MAPPER_PROPERTY_INT);
     }
     
     protected void marshal(Object obj, Class<?> cls, Type genericType, 
@@ -370,6 +379,7 @@ public class JAXBElementProvider extends
         }
         addAttachmentMarshaller(ms);
         addProcessingInstructions(ms, anns);
+        addSchemaLocation(ms, anns);
         marshal(obj, cls, genericType, enc, os, mt, ms);
     }
     
@@ -384,15 +394,30 @@ public class JAXBElementProvider extends
                     getContext().getUriInfo().getBaseUriBuilder().path(relRef).build().toString();
                 value = value.substring(0, ind + 6) + absRef + "'?>";
             }
-            setMarshallerProp(ms, value, "xmlHeaders");
+            setMarshallerProp(ms, value, XML_PI_PROPERTY, XML_PI_PROPERTY_INT);
         }
     }
     
-    private static void setMarshallerProp(Marshaller ms, Object value, String name) throws Exception {
+    private void addSchemaLocation(Marshaller ms, Annotation[] anns) throws Exception {
+        SchemaLocation sl = AnnotationUtils.getAnnotation(anns, SchemaLocation.class);
+        if (sl != null) {
+            String value = sl.value();
+            if (getContext() != null) {
+                value = 
+                    getContext().getUriInfo().getBaseUriBuilder().path(value).build().toString();
+            }
+            String propName = !sl.noNamespace() 
+                ? Marshaller.JAXB_SCHEMA_LOCATION : Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION;
+            ms.setProperty(propName, value);
+        }
+    }
+    
+    private static void setMarshallerProp(Marshaller ms, Object value, 
+                                          String name1, String name2) throws Exception {
         try {
-            ms.setProperty("com.sun.xml.bind." + name, value);
+            ms.setProperty(name1, value);
         } catch (PropertyException ex) {
-            ms.setProperty("com.sun.xml.internal.bind." + name, value);
+            ms.setProperty(name2, value);
         }
         
     }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1210127&r1=1210126&r2=1210127&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Sun Dec  4 15:57:04 2011
@@ -78,6 +78,7 @@ import org.apache.cxf.helpers.XMLUtils;
 import org.apache.cxf.jaxrs.ext.Oneway;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
 import org.apache.cxf.jaxrs.ext.search.SearchContext;
+import org.apache.cxf.jaxrs.ext.xml.SchemaLocation;
 import org.apache.cxf.jaxrs.ext.xml.XMLInstruction;
 import org.apache.cxf.phase.PhaseInterceptorChain;
 
@@ -210,6 +211,7 @@ public class BookStore {
     @Path("name-in-query")
     @Produces("application/xml")
     @XMLInstruction("<!DOCTYPE Something SYSTEM 'my.dtd'><?xmlstylesheet href='common.css'?>")
+    @SchemaLocation("book.xsd")
     public Book getBookFromQuery(@QueryParam("name") String name) {
         return new Book(name, 321L);
     }

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1210127&r1=1210126&r2=1210127&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java (original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Sun Dec  4 15:57:04 2011
@@ -141,8 +141,11 @@ public class JAXRSClientServerBookTest e
         String name = "Many        spaces";
         wc.query("name", name);
         String content = wc.get(String.class);
+        System.out.println(content);
         assertTrue(content.contains("<!DOCTYPE Something SYSTEM 'my.dtd'>"));
         assertTrue(content.contains("<?xmlstylesheet href='" + base + "/common.css'?>"));
+        assertTrue(content.contains("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""));
+        assertTrue(content.contains("xsi:schemaLocation=\"" + base + "/book.xsd\""));
     }
     
     @Test