cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1497709 - in /cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs: provider/AbstractJAXBProvider.java provider/JAXBElementProvider.java utils/JAXRSUtils.java
Date Fri, 28 Jun 2013 09:56:54 GMT
Author: sergeyb
Date: Fri Jun 28 09:56:54 2013
New Revision: 1497709

URL: http://svn.apache.org/r1497709
Log:
[CXF-5100] Support for customizing namespace mapper and XML PI property names

Modified:
    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/provider/JAXBElementProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java

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=1497709&r1=1497708&r2=1497709&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 28 09:56:54 2013
@@ -91,8 +91,8 @@ public abstract class AbstractJAXBProvid
     protected static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractJAXBProvider.class);
 
     protected static final Logger LOG = LogUtils.getL7dLogger(AbstractJAXBProvider.class);
-    protected static final String NS_MAPPER_PROPERTY = "com.sun.xml.bind.namespacePrefixMapper";
-    protected static final String NS_MAPPER_PROPERTY_INT = "com.sun.xml.internal.bind.namespacePrefixMapper";
+    protected static final String NS_MAPPER_PROPERTY_RI = "com.sun.xml.bind.namespacePrefixMapper";
+    protected static final String NS_MAPPER_PROPERTY_RI_INT = "com.sun.xml.internal.bind.namespacePrefixMapper";
     private static final String JAXB_DEFAULT_NAMESPACE = "##default";
     private static final String JAXB_DEFAULT_NAME = "##default";
     
@@ -137,10 +137,16 @@ public abstract class AbstractJAXBProvid
     private Unmarshaller.Listener unmarshallerListener;
     private Marshaller.Listener marshallerListener;
     private DocumentDepthProperties depthProperties;
+    private String namespaceMapperPropertyName;
     
-    protected static void setNamespaceMapper(Marshaller ms, Map<String, String> map)
throws Exception {
+    protected void setNamespaceMapper(Marshaller ms, 
+                                      Map<String, String> map) throws Exception {
         NamespaceMapper nsMapper = new NamespaceMapper(map);
-        setMarshallerProp(ms, nsMapper, NS_MAPPER_PROPERTY, NS_MAPPER_PROPERTY_INT);
+        if (namespaceMapperPropertyName != null) {
+            setMarshallerProp(ms, nsMapper, namespaceMapperPropertyName, null);
+        } else {
+            setMarshallerProp(ms, nsMapper, NS_MAPPER_PROPERTY_RI, NS_MAPPER_PROPERTY_RI_INT);
+        }
     }
     
     protected static void setMarshallerProp(Marshaller ms, Object value, 
@@ -148,7 +154,11 @@ public abstract class AbstractJAXBProvid
         try {
             ms.setProperty(name1, value);
         } catch (PropertyException ex) {
-            ms.setProperty(name2, value);
+            if (name2 != null) {
+                ms.setProperty(name2, value);
+            } else {
+                throw ex;
+            }
         }
         
     }
@@ -800,6 +810,10 @@ public abstract class AbstractJAXBProvid
         this.marshallerListener = marshallerListener;
     }
 
+    public void setNamespaceMapperPropertyName(String namespaceMapperProperty) {
+        this.namespaceMapperPropertyName = namespaceMapperProperty;
+    }
+
     @XmlRootElement
     protected static class CollectionWrapper {
         

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=1497709&r1=1497708&r2=1497709&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 Jun 28 09:56:54 2013
@@ -79,8 +79,8 @@ import org.apache.cxf.staxutils.transfor
 @Provider
 public class JAXBElementProvider<T> extends AbstractJAXBProvider<T>  {
     private static final String XML_PI_START = "<?xml version=\"1.0\" encoding=\"";
-    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 String XML_PI_PROPERTY_RI = "com.sun.xml.bind.xmlHeaders";
+    private static final String XML_PI_PROPERTY_RI_INT = "com.sun.xml.internal.bind.xmlHeaders";
     
     private static final List<String> MARSHALLER_PROPERTIES =
         Arrays.asList(new String[] {Marshaller.JAXB_ENCODING,
@@ -88,14 +88,15 @@ public class JAXBElementProvider<T> exte
                                     Marshaller.JAXB_FRAGMENT,
                                     Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION,
                                     Marshaller.JAXB_SCHEMA_LOCATION,
-                                    NS_MAPPER_PROPERTY,
-                                    NS_MAPPER_PROPERTY_INT,
-                                    XML_PI_PROPERTY,
-                                    XML_PI_PROPERTY_INT});
+                                    NS_MAPPER_PROPERTY_RI,
+                                    NS_MAPPER_PROPERTY_RI_INT,
+                                    XML_PI_PROPERTY_RI,
+                                    XML_PI_PROPERTY_RI_INT});
     
     private Map<String, Object> mProperties = Collections.emptyMap();
     private Map<String, String> nsPrefixes = Collections.emptyMap();
     private String xmlResourceOffset = "";
+    private String xmlPiPropertyName;
     
     public JAXBElementProvider() {
         
@@ -109,6 +110,14 @@ public class JAXBElementProvider<T> exte
         nsPrefixes = prefixes;
     }
     
+    protected void setXmlPiProperty(Marshaller ms, String value) throws Exception {
+        if (xmlPiPropertyName != null) {
+            setMarshallerProp(ms, value, xmlPiPropertyName, null);
+        } else {
+            setMarshallerProp(ms, value, XML_PI_PROPERTY_RI, XML_PI_PROPERTY_RI_INT);
+        }
+    }
+    
     @Override
     protected boolean canBeReadAsJaxbElement(Class<?> type) {
         return super.canBeReadAsJaxbElement(type) 
@@ -427,7 +436,7 @@ public class JAXBElementProvider<T> exte
                 String absRef = buildAbsoluteXMLResourceURI(relRef);
                 value = value.substring(0, ind + 6) + absRef + "'?>";
             }
-            setMarshallerProp(ms, value, XML_PI_PROPERTY, XML_PI_PROPERTY_INT);
+            setXmlPiProperty(ms, value);
         }
     }
     
@@ -572,5 +581,9 @@ public class JAXBElementProvider<T> exte
         throws Exception {
         ms.marshal(obj, writer);
     }
+
+    public void setXmlPiPropertyName(String xmlPiPropertyName) {
+        this.xmlPiPropertyName = xmlPiPropertyName;
+    }
     
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=1497709&r1=1497708&r2=1497709&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java Fri
Jun 28 09:56:54 2013
@@ -163,11 +163,12 @@ public final class JAXRSUtils {
     public static final String ROOT_PROVIDER = "service.root.provider";
     public static final String EXCEPTION_FROM_MAPPER = "exception.from.mapper";
     public static final String PARTIAL_HIERARCHICAL_MEDIA_SUBTYPE_CHECK = 
-        "media.subtype.partial.check"; 
+        "media.subtype.partial.check";
     public static final String DOC_LOCATION = "wadl.location";
     public static final String MEDIA_TYPE_Q_PARAM = "q";
     public static final String MEDIA_TYPE_QS_PARAM = "qs";
     private static final String MEDIA_TYPE_DISTANCE_PARAM = "d";
+    private static final String DEFAULT_CONTENT_TYPE = "default.content.type";
     
     private static final Logger LOG = LogUtils.getL7dLogger(JAXRSUtils.class);
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(JAXRSUtils.class);
@@ -827,7 +828,8 @@ public final class JAXRSUtils {
             String contentType = (String)message.get(Message.CONTENT_TYPE);
 
             if (contentType == null) {
-                contentType = MediaType.APPLICATION_OCTET_STREAM;
+                String defaultCt = (String)message.getContextualProperty(DEFAULT_CONTENT_TYPE);
+                contentType = defaultCt == null ? MediaType.APPLICATION_OCTET_STREAM : defaultCt;
             }
 
             return readFromMessageBody(parameterClass,
@@ -1304,17 +1306,15 @@ public final class JAXRSUtils {
                 } catch (Exception ex) {
                     throw new Fault(ex);
                 }
-            } else {
-                String errorMessage = new org.apache.cxf.common.i18n.Message("NO_MSG_READER",
-                                                       BUNDLE,
-                                                       targetTypeClass.getSimpleName(),
-                                                       mediaTypeToString(contentType)).toString();
-                LOG.warning(errorMessage);
-                throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
             }
         }
 
-        return null;
+        String errorMessage = new org.apache.cxf.common.i18n.Message("NO_MSG_READER",
+                                                       BUNDLE,
+                                                       targetTypeClass.getSimpleName(),
+                                                       mediaTypeToString(contentType)).toString();
+        LOG.warning(errorMessage);
+        throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
     }
     
     @SuppressWarnings("unchecked")



Mime
View raw message