cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5644] Updating XSLTTransform to let the client run transformations
Date Sun, 30 Mar 2014 20:26:58 GMT
Repository: cxf
Updated Branches:
  refs/heads/master b0e7be04d -> fad0c99f7


[CXF-5644] Updating XSLTTransform to let the client run transformations


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/fad0c99f
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/fad0c99f
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/fad0c99f

Branch: refs/heads/master
Commit: fad0c99f780802d1859c90d3fd5300f0c2615b21
Parents: b0e7be0
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Sun Mar 30 21:26:27 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Sun Mar 30 21:26:27 2014 +0100

----------------------------------------------------------------------
 .../apache/cxf/jaxrs/ext/xml/XSLTTransform.java |  6 +-
 .../cxf/jaxrs/provider/JAXBElementProvider.java | 70 +++++++++++++-------
 .../cxf/jaxrs/provider/XSLTJaxbProvider.java    | 12 ++--
 3 files changed, 60 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/fad0c99f/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XSLTTransform.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XSLTTransform.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XSLTTransform.java
index f0beb76..245106c 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XSLTTransform.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XSLTTransform.java
@@ -31,6 +31,9 @@ import java.lang.annotation.Target;
 @Target({ElementType.METHOD, ElementType.PARAMETER })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface XSLTTransform {
+    public enum TransformType {
+        CLIENT, SERVER, BOTH
+    }
     /**
      * Template location
      */
@@ -38,5 +41,6 @@ public @interface XSLTTransform {
     /**
      * Supported media types 
      **/
-    String[] mediaTypes() default {"*/*" };
+    String[] mediaTypes() default { };
+    TransformType type() default TransformType.SERVER;
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/fad0c99f/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
index 8ff1106..6da794b 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/JAXBElementProvider.java
@@ -64,6 +64,7 @@ import org.apache.cxf.jaxrs.ext.Nullable;
 import org.apache.cxf.jaxrs.ext.xml.XMLInstruction;
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
 import org.apache.cxf.jaxrs.ext.xml.XSISchemaLocation;
+import org.apache.cxf.jaxrs.ext.xml.XSLTTransform;
 import org.apache.cxf.jaxrs.utils.AnnotationUtils;
 import org.apache.cxf.jaxrs.utils.ExceptionUtils;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
@@ -441,37 +442,60 @@ public class JAXBElementProvider<T> extends AbstractJAXBProvider<T>
 {
             setNamespaceMapper(ms, nsPrefixes);
         }
         addAttachmentMarshaller(ms);
-        addProcessingInstructions(ms, anns);
-        addSchemaLocation(ms, anns);
+        processXmlAnnotations(ms, mt, anns);
         marshal(obj, cls, genericType, enc, os, anns, 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();
-            int ind = value.indexOf("href='");
-            if (ind > 0) {
-                String relRef = value.substring(ind + 6);
-                relRef = relRef.substring(0, relRef.length() - 3).trim();
-                if (relRef.endsWith("'")) {
-                    relRef = relRef.substring(0, relRef.length() - 1);
-                }
-                String absRef = resolveXMLResourceURI(relRef);
-                value = value.substring(0, ind + 6) + absRef + "'?>";
+    private void processXmlAnnotations(Marshaller ms, MediaType mt, Annotation[] anns) throws
Exception {
+        if (anns == null) {
+            return;
+        }
+        for (Annotation ann : anns) {
+            if (ann.annotationType() == XMLInstruction.class) {
+                addProcessingInstructions(ms, (XMLInstruction)ann);
+            } else if (ann.annotationType() == XSISchemaLocation.class) {
+                addSchemaLocation(ms, (XSISchemaLocation)ann);
+            } else if (ann.annotationType() == XSLTTransform.class) {
+                addXslProcessingInstruction(ms, mt, (XSLTTransform)ann);
             }
-            setXmlPiProperty(ms, value);
         }
     }
     
-    private void addSchemaLocation(Marshaller ms, Annotation[] anns) throws Exception {
-        XSISchemaLocation sl = AnnotationUtils.getAnnotation(anns, XSISchemaLocation.class);
-        if (sl != null) {
-            String value = sl.resolve() ? resolveXMLResourceURI(sl.value()) : sl.value();
-            String propName = !sl.noNamespace() 
-                ? Marshaller.JAXB_SCHEMA_LOCATION : Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION;
-            ms.setProperty(propName, value);
+    private void addProcessingInstructions(Marshaller ms, XMLInstruction pi) throws Exception
{
+        String value = pi.value();
+        int ind = value.indexOf("href='");
+        if (ind > 0) {
+            String relRef = value.substring(ind + 6);
+            relRef = relRef.substring(0, relRef.length() - 3).trim();
+            if (relRef.endsWith("'")) {
+                relRef = relRef.substring(0, relRef.length() - 1);
+            }
+            String absRef = resolveXMLResourceURI(relRef);
+            value = value.substring(0, ind + 6) + absRef + "'?>";
         }
+        setXmlPiProperty(ms, value);
+    }
+    
+    private void addXslProcessingInstruction(Marshaller ms, MediaType mt, XSLTTransform ann)

+        throws Exception {
+        if (ann.type() == XSLTTransform.TransformType.CLIENT 
+            || ann.type() == XSLTTransform.TransformType.BOTH && ann.mediaTypes().length
> 0) {
+            for (String s : ann.mediaTypes()) {
+                if (mt.isCompatible(JAXRSUtils.toMediaType(s))) {
+                    return;
+                }
+            }
+            String absRef = resolveXMLResourceURI(ann.value());
+            String xslPi = "<?xml-stylesheet type=\"text/xsl\" href=\"" + absRef + "\"?>";
+            setXmlPiProperty(ms, xslPi);
+        }
+    }
+    
+    private void addSchemaLocation(Marshaller ms, XSISchemaLocation sl) throws Exception
{
+        String value = sl.resolve() ? resolveXMLResourceURI(sl.value()) : sl.value();
+        String propName = !sl.noNamespace() 
+            ? Marshaller.JAXB_SCHEMA_LOCATION : Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION;
+        ms.setProperty(propName, value);
     }
     
     protected String resolveXMLResourceURI(String path) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/fad0c99f/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
index 10be945..34713cb 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/XSLTJaxbProvider.java
@@ -200,12 +200,16 @@ public class XSLTJaxbProvider<T> extends JAXBElementProvider<T>
{
     
     protected XSLTTransform getXsltTransformAnn(Annotation[] anns, MediaType mt) {
         XSLTTransform ann = AnnotationUtils.getAnnotation(anns, XSLTTransform.class);
-        if (ann != null) {
-            for (String s : ann.mediaTypes()) {
-                if (mt.isCompatible(JAXRSUtils.toMediaType(s))) {
-                    return ann;
+        if (ann != null && ann.type() != XSLTTransform.TransformType.CLIENT) {
+            if (ann.mediaTypes().length > 0) {
+                for (String s : ann.mediaTypes()) {
+                    if (mt.isCompatible(JAXRSUtils.toMediaType(s))) {
+                        return ann;
+                    }
                 }
+                return null;
             }
+            return ann;
         }
         return null;
     }


Mime
View raw message