cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1521413 - in /cxf/trunk/rt: frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/ rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/
Date Tue, 10 Sep 2013 09:55:39 GMT
Author: sergeyb
Date: Tue Sep 10 09:55:39 2013
New Revision: 1521413

URL: http://svn.apache.org/r1521413
Log:
[CXF-4381] Updating DOM4JProvider to better support custom media types

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
    cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java

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=1521413&r1=1521412&r2=1521413&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 Tue
Sep 10 09:55:39 2013
@@ -214,26 +214,25 @@ public final class JAXRSUtils {
         return theList;
     }
 
-    @SuppressWarnings("unchecked")
-    private static String[] getUserMediaTypes(Object provider, String methodName) {
+    private static String[] getUserMediaTypes(Object provider, boolean consumes) {
         String[] values = null;
         if (AbstractConfigurableProvider.class.isAssignableFrom(provider.getClass())) {
-            try {
-                Method m = provider.getClass().getMethod(methodName, new Class[]{});
-                List<String> types = (List<String>)m.invoke(provider, new Object[]{});
-                if (types != null) {
-                    values = types.size() > 0 ? types.toArray(new String[types.size()])
-                                               : new String[]{"*/*"};
-                }
-            } catch (Exception ex) {
-                // ignore
+            List<String> types = null;
+            if (consumes) {
+                types = ((AbstractConfigurableProvider)provider).getConsumeMediaTypes();
+            } else {
+                types = ((AbstractConfigurableProvider)provider).getProduceMediaTypes();
+            }
+            if (types != null) {
+                values = types.size() > 0 ? types.toArray(new String[types.size()])
+                                           : new String[]{"*/*"};
             }
         }
         return values;
     }
     
     public static List<MediaType> getProviderConsumeTypes(MessageBodyReader<?>
provider) {
-        String[] values = getUserMediaTypes(provider, "getConsumeMediaTypes");
+        String[] values = getUserMediaTypes(provider, true);
         
         if (values == null) {
             return getConsumeTypes(provider.getClass().getAnnotation(Consumes.class));
@@ -243,7 +242,7 @@ public final class JAXRSUtils {
     }
     
     public static List<MediaType> getProviderProduceTypes(MessageBodyWriter<?>
provider) {
-        String[] values = getUserMediaTypes(provider, "getProduceMediaTypes");
+        String[] values = getUserMediaTypes(provider, false);
         if (values == null) {
             return getProduceTypes(provider.getClass().getAnnotation(Produces.class));
         } else {

Modified: cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java?rev=1521413&r1=1521412&r2=1521413&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProvider.java
Tue Sep 10 09:55:39 2013
@@ -35,12 +35,13 @@ import javax.ws.rs.ext.MessageBodyReader
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Providers;
 
+import org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider;
 import org.apache.cxf.staxutils.StaxUtils;
 
-@javax.ws.rs.Produces({"application/xml", "text/xml", "application/json" })
-@javax.ws.rs.Consumes({"application/xml", "text/xml", "application/json" })
-public class DOM4JProvider implements MessageBodyReader<org.dom4j.Document>,
-                MessageBodyWriter<org.dom4j.Document> {
+@javax.ws.rs.Produces({"application/xml", "application/*+xml", "text/xml", "application/json"
})
+@javax.ws.rs.Consumes({"application/xml", "application/*+xml", "text/xml", "application/json"
})
+public class DOM4JProvider extends AbstractConfigurableProvider 
+    implements MessageBodyReader<org.dom4j.Document>, MessageBodyWriter<org.dom4j.Document>
{
 
     private static final Class<org.w3c.dom.Document> DOM_DOC_CLS = 
         org.w3c.dom.Document.class;

Modified: cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java?rev=1521413&r1=1521412&r2=1521413&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/providers/src/test/java/org/apache/cxf/jaxrs/provider/dom4j/DOM4JProviderTest.java
Tue Sep 10 09:55:39 2013
@@ -50,13 +50,13 @@ public class DOM4JProviderTest extends A
         assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
     }
     private org.dom4j.Document readXML() throws Exception {
-        return readXML("<a/>");
+        return readXML(MediaType.APPLICATION_XML_TYPE, "<a/>");
     }
-    private org.dom4j.Document readXML(final String xml) throws Exception {
+    private org.dom4j.Document readXML(MediaType ct, final String xml) throws Exception {
         DOM4JProvider p = new DOM4JProvider();
         p.setProviders(new ProvidersImpl(createMessage()));
         org.dom4j.Document dom = p.readFrom(org.dom4j.Document.class, org.dom4j.Document.class,

-            new Annotation[] {}, MediaType.APPLICATION_XML_TYPE, new MetadataMap<String,
String>(),
+            new Annotation[] {}, ct, new MetadataMap<String, String>(),
             new ByteArrayInputStream(xml.getBytes("UTF-8")));
         return dom;
     }
@@ -76,14 +76,22 @@ public class DOM4JProviderTest extends A
 
     @Test
     public void testWriteXML() throws Exception {
-        org.dom4j.Document dom = readXML();
+        doTestWriteXML(MediaType.APPLICATION_XML_TYPE);
+    }
+    
+    @Test
+    public void testWriteXMLCustomCt() throws Exception {
+        doTestWriteXML(MediaType.valueOf("application/custom+xml"));
+    }
+    
+    private void doTestWriteXML(MediaType ct) throws Exception {
+        org.dom4j.Document dom = readXML(ct, "<a/>");
         DOM4JProvider p = new DOM4JProvider();
         p.setProviders(new ProvidersImpl(createMessage()));
         
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, 
-            new Annotation[]{}, MediaType.APPLICATION_XML_TYPE, new MetadataMap<String,
Object>(),
-            bos);
+            new Annotation[]{}, ct, new MetadataMap<String, Object>(), bos);
         String str = bos.toString();
         // starts with the xml PI
         assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
@@ -106,7 +114,7 @@ public class DOM4JProviderTest extends A
     
     @Test
     public void testWriteJSONDropRoot() throws Exception {
-        org.dom4j.Document dom = readXML("<root><a/></root>");
+        org.dom4j.Document dom = readXML(MediaType.APPLICATION_XML_TYPE, "<root><a/></root>");
         DOM4JProvider p = new DOM4JProvider();
         p.setProviders(new ProvidersImpl(createMessageWithJSONProvider()));
         



Mime
View raw message