cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r980239 - in /cxf/branches/2.2.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java
Date Wed, 28 Jul 2010 21:36:43 GMT
Author: sergeyb
Date: Wed Jul 28 21:36:43 2010
New Revision: 980239

URL: http://svn.apache.org/viewvc?rev=980239&view=rev
Log:
Merged revisions 980236 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r980236 | sergeyb | 2010-07-28 22:33:31 +0100 (Wed, 28 Jul 2010) | 1 line
  
  [CXF-2904] initial Request.selectVariant implementation
........

Modified:
    cxf/branches/2.2.x-fixes/   (props changed)
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java
    cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Jul 28 21:36:43 2010
@@ -1 +1 @@
-/cxf/trunk:979457,979459,979764
+/cxf/trunk:979457,979459,979764,980236

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java?rev=980239&r1=980238&r2=980239&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java
Wed Jul 28 21:36:43 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);
     }
 
 
@@ -187,4 +219,50 @@ public class RequestImpl implements Requ
 
 
 
+    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/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java?rev=980239&r1=980238&r2=980239&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java
(original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java
Wed Jul 28 21:36:43 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