You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2013/06/19 14:54:27 UTC

svn commit: r1494596 - in /cxf/branches/2.7.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

Author: sergeyb
Date: Wed Jun 19 12:54:27 2013
New Revision: 1494596

URL: http://svn.apache.org/r1494596
Log:
Merged revisions 1489766 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1489766 | sergeyb | 2013-06-05 10:22:56 +0100 (Wed, 05 Jun 2013) | 1 line
  
  [CXF-5007] Some updates to Request implementation
........

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

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1489766

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

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java?rev=1494596&r1=1494595&r2=1494596&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java (original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/RequestImpl.java Wed Jun 19 12:54:27 2013
@@ -40,6 +40,7 @@ import org.apache.cxf.common.util.String
 import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.PhaseInterceptorChain;
 
 /**
  * TODO : deal with InvalidStateExceptions
@@ -67,6 +68,8 @@ public class RequestImpl implements Requ
         List<String> acceptEncs = parseAcceptEnc(
             headers.getRequestHeaders().getFirst(HttpHeaders.ACCEPT_ENCODING));
         
+        List<Object> varyValues = new LinkedList<Object>();
+        
         List<Variant> matchingVars = new LinkedList<Variant>();
         for (Variant var : vars) {
             MediaType mt = var.getMediaType();
@@ -75,23 +78,63 @@ public class RequestImpl implements Requ
                         
             boolean mtMatched = mt == null || acceptMediaTypes.isEmpty()
                 || JAXRSUtils.intersectMimeTypes(acceptMediaTypes, mt).size() != 0;
-            
-            boolean encMatched = acceptEncs.isEmpty() || enc == null 
-                || acceptEncs.contains(enc);
+            if (mtMatched) {
+                handleVaryValues(varyValues, HttpHeaders.ACCEPT);
+            }
             
             boolean langMatched = lang == null || acceptLangs.isEmpty()
                 || isLanguageMatched(acceptLangs, lang);
+            if (langMatched) {
+                handleVaryValues(varyValues, HttpHeaders.ACCEPT_LANGUAGE);
+            }
+            
+            boolean encMatched = acceptEncs.isEmpty() || enc == null 
+                || acceptEncs.contains(enc);
+            if (encMatched) {
+                handleVaryValues(varyValues, HttpHeaders.ACCEPT_ENCODING);
+            }
             
             if (mtMatched && encMatched && langMatched) {
                 matchingVars.add(var);
             }
         }
-        if (matchingVars.size() > 1) {
-            Collections.sort(matchingVars, new VariantComparator());       
-        }
-        return matchingVars.isEmpty() ? null : matchingVars.get(0);
+        if (matchingVars.size() > 0) {
+            addVaryHeader(varyValues);
+            Collections.sort(matchingVars, new VariantComparator());
+            return matchingVars.get(0);
+        } 
+        return null;
     }
 
+    private static void handleVaryValues(List<Object> varyValues, String ...values) {
+        for (String v : values) {
+            if (v != null && !varyValues.contains(v)) {
+                varyValues.add(v);
+            }
+        }
+    }
+    
+    private static void addVaryHeader(List<Object> varyValues) {
+        // at this point we still have no out-bound message so lets
+        // use HttpServletResponse. If needed we can save the header on the exchange
+        // and then copy it into the out-bound message's headers
+        Message message = PhaseInterceptorChain.getCurrentMessage();
+        if (message != null) {
+            Object httpResponse = message.get("HTTP.RESPONSE");
+            if (httpResponse != null) {
+                StringBuilder sb = new StringBuilder();
+                for (int i = 0; i < varyValues.size(); i++) {
+                    if (i > 0) {
+                        sb.append(',');
+                    }
+                    sb.append(varyValues.get(i).toString());
+                }
+                ((javax.servlet.http.HttpServletResponse)httpResponse)
+                    .setHeader(HttpHeaders.VARY, sb.toString());
+            }
+        }
+    }
+    
     private static boolean isLanguageMatched(List<Locale> locales, Locale l) {
         
         for (Locale locale : locales) {
@@ -119,6 +162,9 @@ public class RequestImpl implements Requ
     }
     
     public ResponseBuilder evaluatePreconditions(EntityTag eTag) {
+        if (eTag == null) {
+            throw new IllegalArgumentException("ETag is null");
+        }
         ResponseBuilder rb = evaluateIfMatch(eTag);
         if (rb == null) {
             rb = evaluateIfNonMatch(eTag);
@@ -180,6 +226,9 @@ public class RequestImpl implements Requ
     }
     
     public ResponseBuilder evaluatePreconditions(Date lastModified) {
+        if (lastModified == null) {
+            throw new IllegalArgumentException("Date is null");
+        }
         List<String> ifModifiedSince = headers.getRequestHeader(HttpHeaders.IF_MODIFIED_SINCE);
         
         if (ifModifiedSince == null || ifModifiedSince.size() == 0) {
@@ -234,13 +283,13 @@ public class RequestImpl implements Requ
 
     public ResponseBuilder evaluatePreconditions(Date lastModified, EntityTag eTag) {
         final ResponseBuilder rb = evaluatePreconditions(eTag);
-        if (rb != null) {
+        if (rb == null) {
             // the ETag conditions match; so now conditions for last modified must match
             return evaluatePreconditions(lastModified);
         } else {
             // the ETag conditions do not match, so last modified should be ignored
             // see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html (section 14.26 for
-            // "If-None-Match", behavior not specified for "If-Match", section 14.24)
+            // "If-None-Match", behaviour not specified for "If-Match", section 14.24)
             return null;
         }
     }

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java?rev=1494596&r1=1494595&r2=1494596&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java (original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/RequestImplTest.java Wed Jun 19 12:54:27 2013
@@ -274,14 +274,11 @@ public class RequestImplTest extends Ass
         metadata.putSingle("If-Modified-Since", "Tue, 21 Oct 2008 14:00:00 GMT");
         
         Date lastModified = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH)
-            .parse("Mon, 20 Oct 2008 14:00:00 GMT");
+            .parse("Mon, 22 Oct 2008 14:00:00 GMT");
         
         ResponseBuilder rb = 
-            new RequestImpl(m).evaluatePreconditions(lastModified, new EntityTag("123"));
-        assertNotNull("Precondition is not met", rb);
-        
-        Response r = rb.build();
-        assertEquals("If-Modified-Since precondition was not met", 304, r.getStatus());
+            new RequestImpl(m).evaluatePreconditions(lastModified, new EntityTag("\"123\""));
+        assertNull("Precondition is not met", rb);
     }
     
     @Test
@@ -294,7 +291,7 @@ public class RequestImplTest extends Ass
         
         ResponseBuilder rb = 
             new RequestImpl(m).evaluatePreconditions(lastModified, new EntityTag("124"));
-        assertNull("Dates must not be checked if tags do not match", rb);
+        assertNotNull("Dates must not be checked if tags do not match", rb);
     }
    
 }