cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r980236 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java
Date Wed, 28 Jul 2010 21:33:32 GMT
Author: sergeyb
Date: Wed Jul 28 21:33:31 2010
New Revision: 980236

URL: http://svn.apache.org/viewvc?rev=980236&view=rev
Log:
[CXF-2904] initial Request.selectVariant implementation

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java?rev=980236&r1=980235&r2=980236&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java Wed
Jul 28 21:33:31 2010
@@ -21,17 +21,23 @@ package org.apache.cxf.jaxrs.impl;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.Date;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Locale;
 
 import javax.ws.rs.core.EntityTag;
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Request;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.core.Variant;
 
 import org.apache.cxf.jaxrs.utils.HttpUtils;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.message.Message;
 
 /**
@@ -52,8 +58,34 @@ public class RequestImpl implements Requ
     
 
     public Variant selectVariant(List<Variant> vars) throws IllegalArgumentException
{
-        // TODO Auto-generated method stub
-        return null;
+        if (vars == null || vars.isEmpty()) {
+            throw new IllegalArgumentException("List of Variants is either null or empty");
+        }
+        MediaType inMediaType = headers.getMediaType();
+        Locale inLang = headers.getLanguage();
+        String inEnc = headers.getRequestHeaders().getFirst(HttpHeaders.CONTENT_ENCODING);
+        
+        List<Variant> matchingVars = new LinkedList<Variant>();
+        for (Variant var : vars) {
+            MediaType mt = var.getMediaType();
+            Locale lang = var.getLanguage();
+            String enc = var.getEncoding();
+            
+            boolean mtMatched = mt == null || inMediaType == null
+                || JAXRSUtils.intersectMimeTypes(Collections.singletonList(inMediaType),
mt).size() != 0;
+            
+            boolean encMatched = inEnc == null || enc == null || inEnc.equalsIgnoreCase(enc);
+            
+            boolean langMatched = inLang == null || lang == null || inLang.equals(lang);
+            
+            if (mtMatched && encMatched && langMatched) {
+                matchingVars.add(var);
+            }
+        }
+        if (matchingVars.size() > 1) {
+            Collections.sort(matchingVars, new VariantComparator());       
+        }
+        return matchingVars.isEmpty() ? null : matchingVars.get(0);
     }
 
 
@@ -199,4 +231,50 @@ public class RequestImpl implements Requ
         return null;
     }
 
+    private static class VariantComparator implements Comparator<Variant> {
+
+        @Override
+        public int compare(Variant v1, Variant v2) {
+            int result = compareMediaTypes(v1.getMediaType(), v2.getMediaType());
+            
+            if (result != 0) {
+                return result;
+            }
+            
+            result = compareLanguages(v1.getLanguage(), v2.getLanguage());
+            
+            if (result == 0) {
+                result = compareEncodings(v1.getEncoding(), v2.getEncoding());
+            }
+            
+            return result;
+        }
+        
+        private static int compareMediaTypes(MediaType mt1, MediaType mt2) {
+            if (mt1 != null && mt2 == null) {
+                return -1;
+            } else if (mt1 == null && mt2 != null) {
+                return 1;
+            } 
+            return JAXRSUtils.compareMediaTypes(mt1, mt2);
+        }
+        
+        private static int compareLanguages(Locale l1, Locale l2) {
+            if (l1 != null && l2 == null) {
+                return -1;
+            } else if (l1 == null && l2 != null) {
+                return 1;
+            }
+            return 0;
+        }
+        
+        private static int compareEncodings(String enc1, String enc2) {
+            if (enc1 != null && enc2 == null) {
+                return -1;
+            } else if (enc1 == null && enc2 != null) {
+                return 1;
+            }
+            return 0;
+        }
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java?rev=980236&r1=980235&r2=980236&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java
Wed Jul 28 21:33:31 2010
@@ -20,14 +20,18 @@
 package org.apache.cxf.jaxrs.impl;
 
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.List;
 import java.util.Locale;
 
 import javax.ws.rs.core.EntityTag;
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
+import javax.ws.rs.core.Variant;
 
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageImpl;
@@ -57,6 +61,98 @@ public class RequestImplTest extends Ass
     }
     
     @Test
+    public void testSingleMatchingVariant() {
+        metadata.putSingle(HttpHeaders.CONTENT_TYPE, "application/xml");
+        metadata.putSingle(HttpHeaders.CONTENT_LANGUAGE, "en");
+        metadata.putSingle(HttpHeaders.CONTENT_ENCODING, "utf-8");
+        
+        assertSameVariant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), "UTF-8");
+        assertSameVariant(null, new Locale("en"), "UTF-8");
+        assertSameVariant(MediaType.APPLICATION_XML_TYPE, null, "UTF-8");
+        assertSameVariant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), null);
+        
+    }
+    
+    @Test
+    public void testSingleMatchingVariantWithContentTypeOnly() {
+        metadata.putSingle(HttpHeaders.CONTENT_TYPE, "application/xml");
+        
+        assertSameVariant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), "UTF-8");
+        assertSameVariant(null, new Locale("en"), "UTF-8");
+        assertSameVariant(MediaType.APPLICATION_XML_TYPE, null, "UTF-8");
+        assertSameVariant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), null);
+        
+    }
+    
+    @Test
+    public void testSingleNonMatchingVariant() {
+        metadata.putSingle(HttpHeaders.CONTENT_TYPE, "application/xml");
+        metadata.putSingle(HttpHeaders.CONTENT_LANGUAGE, "en");
+        metadata.putSingle(HttpHeaders.CONTENT_ENCODING, "utf-8");
+        
+        List<Variant> list = new ArrayList<Variant>();
+        list.add(new Variant(MediaType.APPLICATION_JSON_TYPE, new Locale("en"), "utf-8"));
+        assertNull(new RequestImpl(m).selectVariant(list));
+        
+    }
+    
+    @Test
+    public void testMultipleNonMatchingVariants() {
+        metadata.putSingle(HttpHeaders.CONTENT_TYPE, "application/xml");
+        metadata.putSingle(HttpHeaders.CONTENT_LANGUAGE, "en");
+        metadata.putSingle(HttpHeaders.CONTENT_ENCODING, "utf-8");
+        
+        List<Variant> list = new ArrayList<Variant>();
+        list.add(new Variant(MediaType.APPLICATION_JSON_TYPE, new Locale("en"), "utf-8"));
+        list.add(new Variant(MediaType.APPLICATION_XML_TYPE, new Locale("es"), "utf-8"));
+        list.add(new Variant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), "abc"));
+        assertNull(new RequestImpl(m).selectVariant(list));
+        
+    }
+    
+    @Test
+    public void testMultipleVariantsSingleMatch() {
+        metadata.putSingle(HttpHeaders.CONTENT_TYPE, "application/xml");
+        metadata.putSingle(HttpHeaders.CONTENT_LANGUAGE, "en");
+        metadata.putSingle(HttpHeaders.CONTENT_ENCODING, "utf-8");
+        
+        List<Variant> list = new ArrayList<Variant>();
+        list.add(new Variant(MediaType.APPLICATION_JSON_TYPE, new Locale("en"), "utf-8"));
+        list.add(new Variant(MediaType.APPLICATION_XML_TYPE, new Locale("es"), "utf-8"));
+        
+        Variant var3 = new Variant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), "utf-8");
+        list.add(var3);
+        assertSame(var3, new RequestImpl(m).selectVariant(list));
+        
+    }
+    
+    @Test
+    public void testMultipleVariantsBestMatch() {
+        metadata.putSingle(HttpHeaders.CONTENT_TYPE, "application/xml");
+        metadata.putSingle(HttpHeaders.CONTENT_LANGUAGE, "en");
+        metadata.putSingle(HttpHeaders.CONTENT_ENCODING, "utf-8");
+        
+        List<Variant> list = new ArrayList<Variant>();
+        list.add(new Variant(MediaType.APPLICATION_JSON_TYPE, new Locale("en"), "utf-8"));
+        Variant var2 = new Variant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), "utf-8");
+        list.add(var2);
+        Variant var3 = new Variant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), null);
+        list.add(var3);
+        assertSame(var2, new RequestImpl(m).selectVariant(list));
+        list.clear();
+        list.add(var3);
+        assertSame(var3, new RequestImpl(m).selectVariant(list));
+        
+    }
+    
+    private void assertSameVariant(MediaType mt, Locale lang, String enc) {
+        Variant var = new Variant(mt, lang, enc);
+        List<Variant> list = new ArrayList<Variant>();
+        list.add(var);
+        assertSame(var, new RequestImpl(m).selectVariant(list));    
+    }
+    
+    @Test
     public void testWeakEtags() {
         metadata.putSingle("If-Match", new EntityTag("123", true).toString());
         



Mime
View raw message