Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7C29610BA5 for ; Wed, 5 Jun 2013 09:23:20 +0000 (UTC) Received: (qmail 92513 invoked by uid 500); 5 Jun 2013 09:23:20 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 92394 invoked by uid 500); 5 Jun 2013 09:23:19 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 92383 invoked by uid 99); 5 Jun 2013 09:23:18 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 05 Jun 2013 09:23:18 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 05 Jun 2013 09:23:16 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id E08662388906; Wed, 5 Jun 2013 09:22:56 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1489766 - 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, 05 Jun 2013 09:22:56 -0000 To: commits@cxf.apache.org From: sergeyb@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130605092256.E08662388906@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sergeyb Date: Wed Jun 5 09:22:56 2013 New Revision: 1489766 URL: http://svn.apache.org/r1489766 Log: [CXF-5007] Some updates to Request 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=1489766&r1=1489765&r2=1489766&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 Jun 5 09:22:56 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 acceptEncs = parseAcceptEnc( headers.getRequestHeaders().getFirst(HttpHeaders.ACCEPT_ENCODING)); + List varyValues = new LinkedList(); + List matchingVars = new LinkedList(); 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 varyValues, String ...values) { + for (String v : values) { + if (v != null && !varyValues.contains(v)) { + varyValues.add(v); + } + } + } + + private static void addVaryHeader(List 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 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 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/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=1489766&r1=1489765&r2=1489766&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 Jun 5 09:22:56 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); } }