cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1329178 - 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 Mon, 23 Apr 2012 11:07:10 GMT
Author: sergeyb
Date: Mon Apr 23 11:07:09 2012
New Revision: 1329178

URL: http://svn.apache.org/viewvc?rev=1329178&view=rev
Log:
[CXF-4243] Fixing 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=1329178&r1=1329177&r2=1329178&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 Mon
Apr 23 11:07:09 2012
@@ -36,6 +36,7 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.core.Variant;
 
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.message.Message;
@@ -61,22 +62,25 @@ public class RequestImpl implements Requ
         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<MediaType> acceptMediaTypes = headers.getAcceptableMediaTypes();
+        List<Locale> acceptLangs = headers.getAcceptableLanguages();
+        List<String> acceptEncs = parseAcceptEnc(
+            headers.getRequestHeaders().getFirst(HttpHeaders.ACCEPT_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 || acceptMediaTypes.isEmpty()
+                || JAXRSUtils.intersectMimeTypes(acceptMediaTypes, mt).size() != 0;
             
-            boolean mtMatched = mt == null || inMediaType == null
-                || JAXRSUtils.intersectMimeTypes(Collections.singletonList(inMediaType),
mt).size() != 0;
+            boolean encMatched = acceptEncs.isEmpty() || enc == null 
+                || acceptEncs.contains(enc);
             
-            boolean encMatched = inEnc == null || enc == null || inEnc.equalsIgnoreCase(enc);
-            
-            boolean langMatched = inLang == null || lang == null || inLang.equals(lang);
+            boolean langMatched = lang == null || acceptLangs.isEmpty()
+                || isLanguageMatched(acceptLangs, lang);
             
             if (mtMatched && encMatched && langMatched) {
                 matchingVars.add(var);
@@ -88,8 +92,29 @@ public class RequestImpl implements Requ
         return matchingVars.isEmpty() ? null : matchingVars.get(0);
     }
 
+    private static boolean isLanguageMatched(List<Locale> locales, Locale l) {
+        for (Locale locale : locales) {
+            if (locale.getLanguage().equalsIgnoreCase(l.getLanguage())) {
+                return true;
+            }
+        }
+        return false;
+    }
 
-
+    private static List<String> parseAcceptEnc(String acceptEnc) {
+        if (StringUtils.isEmpty(acceptEnc)) {
+            return Collections.emptyList();
+        }
+        List<String> list = new LinkedList<String>();
+        String[] values = acceptEnc.split(",");
+        for (String value : values) {
+            String[] pair = value.trim().split(";");
+            // ignore encoding qualifiers if any for now
+            list.add(pair[0]);
+        }
+        return list;
+    }
+    
     public ResponseBuilder evaluatePreconditions(EntityTag eTag) {
         ResponseBuilder rb = evaluateIfMatch(eTag);
         if (rb == null) {

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=1329178&r1=1329177&r2=1329178&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
Mon Apr 23 11:07:09 2012
@@ -86,9 +86,9 @@ public class RequestImplTest extends Ass
     
     @Test
     public void testSingleNonMatchingVariant() {
-        metadata.putSingle(HttpHeaders.CONTENT_TYPE, "application/xml");
-        metadata.putSingle(HttpHeaders.CONTENT_LANGUAGE, "en");
-        metadata.putSingle(HttpHeaders.CONTENT_ENCODING, "utf-8");
+        metadata.putSingle(HttpHeaders.ACCEPT, "application/xml");
+        metadata.putSingle(HttpHeaders.ACCEPT_LANGUAGE, "en");
+        metadata.putSingle(HttpHeaders.ACCEPT_ENCODING, "utf-8");
         
         List<Variant> list = new ArrayList<Variant>();
         list.add(new Variant(MediaType.APPLICATION_JSON_TYPE, new Locale("en"), "utf-8"));
@@ -98,9 +98,9 @@ public class RequestImplTest extends Ass
     
     @Test
     public void testMultipleNonMatchingVariants() {
-        metadata.putSingle(HttpHeaders.CONTENT_TYPE, "application/xml");
-        metadata.putSingle(HttpHeaders.CONTENT_LANGUAGE, "en");
-        metadata.putSingle(HttpHeaders.CONTENT_ENCODING, "utf-8");
+        metadata.putSingle(HttpHeaders.ACCEPT, "application/xml");
+        metadata.putSingle(HttpHeaders.ACCEPT_LANGUAGE, "en");
+        metadata.putSingle(HttpHeaders.ACCEPT_ENCODING, "utf-8");
         
         List<Variant> list = new ArrayList<Variant>();
         list.add(new Variant(MediaType.APPLICATION_JSON_TYPE, new Locale("en"), "utf-8"));
@@ -112,15 +112,15 @@ public class RequestImplTest extends Ass
     
     @Test
     public void testMultipleVariantsSingleMatch() {
-        metadata.putSingle(HttpHeaders.CONTENT_TYPE, "application/xml");
-        metadata.putSingle(HttpHeaders.CONTENT_LANGUAGE, "en");
-        metadata.putSingle(HttpHeaders.CONTENT_ENCODING, "utf-8");
+        metadata.putSingle(HttpHeaders.ACCEPT, "application/xml");
+        metadata.putSingle(HttpHeaders.ACCEPT_LANGUAGE, "en");
+        metadata.putSingle(HttpHeaders.ACCEPT_ENCODING, "gzip");
         
         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");
+        Variant var3 = new Variant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), "gzip");
         list.add(var3);
         assertSame(var3, new RequestImpl(m).selectVariant(list));
         
@@ -128,13 +128,13 @@ public class RequestImplTest extends Ass
     
     @Test
     public void testMultipleVariantsBestMatch() {
-        metadata.putSingle(HttpHeaders.CONTENT_TYPE, "application/xml");
-        metadata.putSingle(HttpHeaders.CONTENT_LANGUAGE, "en");
-        metadata.putSingle(HttpHeaders.CONTENT_ENCODING, "utf-8");
+        metadata.putSingle(HttpHeaders.ACCEPT, "application/xml");
+        metadata.putSingle(HttpHeaders.ACCEPT_LANGUAGE, "en-us");
+        metadata.putSingle(HttpHeaders.ACCEPT_ENCODING, "gzip;q=1.0, compress");
         
         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(new Variant(MediaType.APPLICATION_JSON_TYPE, new Locale("en"), "gzip"));
+        Variant var2 = new Variant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), "gzip");
         list.add(var2);
         Variant var3 = new Variant(MediaType.APPLICATION_XML_TYPE, new Locale("en"), null);
         list.add(var3);
@@ -142,7 +142,6 @@ public class RequestImplTest extends Ass
         list.clear();
         list.add(var3);
         assertSame(var3, new RequestImpl(m).selectVariant(list));
-        
     }
     
     private void assertSameVariant(MediaType mt, Locale lang, String enc) {



Mime
View raw message