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/02 18:08:58 UTC

svn commit: r1209588 - 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: Fri Dec  2 17:08:58 2011
New Revision: 1209588

URL: http://svn.apache.org/viewvc?rev=1209588&view=rev
Log:
[CXF-3950] Adding XMLInstruction annotation - no substitution is supported at the moment

Added:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLInstruction.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/XMLInstruction.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLInstruction.java?rev=1209588&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLInstruction.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLInstruction.java Fri Dec  2 17:08:58 2011
@@ -0,0 +1,34 @@
+/**
+ * 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;
+
+/**
+ * Can be used to add custom XML processing 
+ * instructions to out-bound XML messages 
+ */
+@Target({ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface XMLInstruction {
+    String value();
+}

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

Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLInstruction.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=1209588&r1=1209587&r2=1209588&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 Fri Dec  2 17:08:58 2011
@@ -56,7 +56,9 @@ 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.XMLInstruction;
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
+import org.apache.cxf.jaxrs.utils.AnnotationUtils;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.JAXBUtils;
@@ -71,6 +73,7 @@ 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 List<String> MARSHALLER_PROPERTIES =
         Arrays.asList(new String[] {Marshaller.JAXB_ENCODING,
                                     Marshaller.JAXB_FORMATTED_OUTPUT,
@@ -234,7 +237,7 @@ public class JAXBElementProvider extends
                 Object actualObject = checkAdapter(obj, cls, anns, true);
                 Class<?> actualClass = obj != actualObject || cls.isInterface() 
                     ? actualObject.getClass() : cls;
-                marshal(actualObject, actualClass, genericType, encoding, os, m);
+                marshal(actualObject, actualClass, genericType, encoding, os, m, anns);
             }
         } catch (JAXBException e) {
             handleJAXBException(e);
@@ -344,6 +347,12 @@ public class JAXBElementProvider extends
     
     protected void marshal(Object obj, Class<?> cls, Type genericType, 
                            String enc, OutputStream os, MediaType mt) throws Exception {
+        marshal(obj, cls, genericType, enc, os, mt, new Annotation[]{});
+    }
+    
+    protected void marshal(Object obj, Class<?> cls, Type genericType, 
+                           String enc, OutputStream os, MediaType mt,
+                           Annotation[] anns) throws Exception {
         obj = convertToJaxbElementIfNeeded(obj, cls, genericType);
         if (obj instanceof JAXBElement && cls != JAXBElement.class) {
             cls = JAXBElement.class;
@@ -354,9 +363,24 @@ public class JAXBElementProvider extends
             setNamespaceMapper(ms, nsPrefixes);
         }
         addAttachmentMarshaller(ms);
+        addProcessingInstructions(ms, anns);
         marshal(obj, cls, genericType, enc, os, mt, ms);
     }
     
+    private void addProcessingInstructions(Marshaller ms, Annotation[] anns) throws Exception {
+        XMLInstruction pi = AnnotationUtils.getAnnotation(anns, XMLInstruction.class);
+        if (pi != null) {
+            String value = pi.value();
+            // Should we even consider adding a base URI here ?
+            // Relative references may be resolved OK, to be verified
+            try {
+                ms.setProperty("com.sun.xml.bind.xmlHeaders", value);
+            } catch (PropertyException ex) {
+                ms.setProperty("com.sun.xml.internal.bind.xmlHeaders", value);
+            }
+        }
+    }
+    
     protected void addAttachmentMarshaller(Marshaller ms) {
         Collection<Attachment> attachments = getAttachments(true);
         if (attachments != null) {

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=1209588&r1=1209587&r2=1209588&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 Fri Dec  2 17:08:58 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.XMLInstruction;
 import org.apache.cxf.phase.PhaseInterceptorChain;
 
 @Path("/bookstore")
@@ -208,6 +209,7 @@ public class BookStore {
     @GET
     @Path("name-in-query")
     @Produces("application/xml")
+    @XMLInstruction("<!DOCTYPE Something SYSTEM 'my.dtd'><?xmlstylesheet href='common.css'?>")
     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=1209588&r1=1209587&r2=1209588&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 Fri Dec  2 17:08:58 2011
@@ -134,6 +134,19 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testProcessingInstruction() throws Exception {
+        
+        String endpointAddress =
+            "http://localhost:" + PORT + "/bookstore/name-in-query"; 
+        WebClient wc = WebClient.create(endpointAddress);
+        String name = "Many        spaces";
+        wc.query("name", name);
+        String content = wc.get(String.class);
+        assertTrue(content.contains("<!DOCTYPE Something SYSTEM 'my.dtd'>"));
+        assertTrue(content.contains("<?xmlstylesheet href='common.css'?>"));
+    }
+    
+    @Test
     public void testGetBookWithColonMarks() throws Exception {
         
         // URLEncoder will turn ":" into "%3A" but ':' is actually