cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r618172 - in /incubator/cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/JAXRSUtils.java test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
Date Mon, 04 Feb 2008 06:13:45 GMT
Author: jliu
Date: Sun Feb  3 22:13:43 2008
New Revision: 618172

URL: http://svn.apache.org/viewvc?rev=618172&view=rev
Log:
Apply patch CXF-1413 on behalf of Sergey Beryozkin. 

Modified:
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java?rev=618172&r1=618171&r2=618172&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSUtils.java
Sun Feb  3 22:13:43 2008
@@ -53,6 +53,8 @@
 
 public final class JAXRSUtils {
     
+    private static final String ALL_TYPES = "*/*";
+    
     private JAXRSUtils() {        
     }
     
@@ -115,13 +117,13 @@
     private static class OperationResourceInfoComparator implements Comparator<OperationResourceInfo>
{
         public int compare(OperationResourceInfo e1, OperationResourceInfo e2) {
             ConsumeMime c1 = e1.getMethod().getAnnotation(ConsumeMime.class);
-            String[] mimeType1 = {"*/*"};
+            String[] mimeType1 = {ALL_TYPES};
             if (c1 != null) {
                 mimeType1 = c1.value();               
             }
             
             ConsumeMime c2 = e2.getMethod().getAnnotation(ConsumeMime.class);
-            String[] mimeType2 = {"*/*"};
+            String[] mimeType2 = {ALL_TYPES};
             if (c2 != null) {
                 mimeType2 = c2.value();               
             }
@@ -130,13 +132,13 @@
             if (resultOfComparingConsumeMime == 0) {
                 //use the media type of output data as the secondary key.
                 ProduceMime p1 = e1.getMethod().getAnnotation(ProduceMime.class);
-                String[] mimeTypeP1 = {"*/*"};
+                String[] mimeTypeP1 = {ALL_TYPES};
                 if (p1 != null) {
                     mimeTypeP1 = p1.value();               
                 }
                 
                 ProduceMime p2 = e2.getMethod().getAnnotation(ProduceMime.class);
-                String[] mimeTypeP2 = {"*/*"};
+                String[] mimeTypeP2 = {ALL_TYPES};
                 if (p2 != null) {
                     mimeTypeP2 = p2.value();               
                 }    
@@ -258,29 +260,42 @@
                 // ignore
             }
         }
+        List<String> acceptValues = new ArrayList<String>();
         if (acceptContentTypes != null) {
-            try {
-                MimeType mt = new MimeType(acceptContentTypes);
-                acceptContentTypes = mt.getBaseType();
-            } catch (MimeTypeParseException e) {
-                // ignore
+            while (acceptContentTypes.length() > 0) {
+                String tp = acceptContentTypes;
+                int index = acceptContentTypes.indexOf(',');
+                if (index != -1) {
+                    tp = acceptContentTypes.substring(0, index);
+                    acceptContentTypes = acceptContentTypes.substring(index + 1).trim();
+                } else {
+                    acceptContentTypes = "";
+                }
+                try {
+                    MimeType mt = new MimeType(tp);
+                    acceptValues.add(mt.getBaseType());
+                } catch (MimeTypeParseException e) {
+                    // ignore
+                }   
             }
-        }    
+        } else {
+            acceptValues.add(ALL_TYPES);
+        }
           
-        String[] consumeMimeTypes = {"*/*"};          
+        String[] consumeMimeTypes = {ALL_TYPES};          
         ConsumeMime c = m.getAnnotation(ConsumeMime.class);
         if (c != null) {
             consumeMimeTypes = c.value();               
         } 
         
-        String[] produceMimeTypes = {"*/*"};          
+        String[] produceMimeTypes = {ALL_TYPES};          
         ProduceMime p = m.getAnnotation(ProduceMime.class);
         if (p != null) {
             produceMimeTypes = p.value();               
         }     
         
         if (intersectMimeTypes(consumeMimeTypes, contentTypes).length != 0
-            && intersectMimeTypes(produceMimeTypes, acceptContentTypes).length !=
0) {
+            && intersectMimeTypes(produceMimeTypes, acceptValues.toArray(new String[]{})).length
!= 0) {
             return true;
         }
         return false;

Modified: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java?rev=618172&r1=618171&r2=618172&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
(original)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/JAXRSUtilsTest.java
Sun Feb  3 22:13:43 2008
@@ -18,10 +18,13 @@
  */
 package org.apache.cxf.jaxrs;
 
+import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.ws.rs.ProduceMime;
+
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
 import org.junit.Assert;
@@ -30,9 +33,15 @@
 
 public class JAXRSUtilsTest extends Assert {
 
+    public class Customer {
+        @ProduceMime("text/xml")   
+        public void test() {
+            // complete
+        }
+    };
+    
     @Before
-    public void setUp() throws Exception {
-
+    public void setUp() {
     }
 
     @Test
@@ -224,5 +233,26 @@
         candidateList = JAXRSUtils.intersectMimeTypes(acceptedMimeTypes, providerMimeTypes);
 
         assertEquals(0, candidateList.length);
+    }
+    
+    @Test
+    public void testAcceptTypesMatch() throws Exception {
+        
+        Method m = Customer.class.getMethod("test", new Class[]{});
+        
+        assertTrue("Accept types with multiple values can not be matched properly",
+                   JAXRSUtils.matchMimeTypes(null, "text/xml,text/bar", m));
+        assertTrue("Accept types with multiple values can not be matched properly",
+                   JAXRSUtils.matchMimeTypes(null, "text/foo, text/bar, text/xml ", m));
+        assertTrue("Accept types with multiple values can not be matched properly",
+                   JAXRSUtils.matchMimeTypes(null, "text/bar,text/xml", m));
+        assertTrue("Accept types with multiple values can not be matched properly",
+                   JAXRSUtils.matchMimeTypes(null, "text/*", m));
+        assertTrue("Accept types with multiple values can not be matched properly",
+                   JAXRSUtils.matchMimeTypes(null, "*/*", m));
+        assertTrue("Accept types with multiple values can not be matched properly",
+                   JAXRSUtils.matchMimeTypes(null, null, m));
+        
+        
     }
 }



Mime
View raw message