View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client.cache;
28  
29  import java.util.Map;
30  
31  import org.apache.http.Header;
32  import org.apache.http.HeaderElement;
33  import org.apache.http.ProtocolException;
34  import org.apache.http.annotation.Contract;
35  import org.apache.http.annotation.ThreadingBehavior;
36  import org.apache.http.client.cache.HeaderConstants;
37  import org.apache.http.client.cache.HttpCacheEntry;
38  import org.apache.http.client.methods.HttpRequestWrapper;
39  
40  /**
41   * @since 4.1
42   */
43  @Contract(threading = ThreadingBehavior.IMMUTABLE)
44  class ConditionalRequestBuilder {
45  
46      /**
47       * When a {@link HttpCacheEntry} is stale but 'might' be used as a response
48       * to an {@link org.apache.http.HttpRequest} we will attempt to revalidate
49       * the entry with the origin.  Build the origin {@link org.apache.http.HttpRequest}
50       * here and return it.
51       *
52       * @param request the original request from the caller
53       * @param cacheEntry the entry that needs to be re-validated
54       * @return the wrapped request
55       * @throws ProtocolException when I am unable to build a new origin request.
56       */
57      public HttpRequestWrapper buildConditionalRequest(final HttpRequestWrapper request, final HttpCacheEntry cacheEntry)
58              throws ProtocolException {
59          final HttpRequestWrapper newRequest = HttpRequestWrapper.wrap(request.getOriginal());
60          newRequest.setHeaders(request.getAllHeaders());
61          final Header eTag = cacheEntry.getFirstHeader(HeaderConstants.ETAG);
62          if (eTag != null) {
63              newRequest.setHeader(HeaderConstants.IF_NONE_MATCH, eTag.getValue());
64          }
65          final Header lastModified = cacheEntry.getFirstHeader(HeaderConstants.LAST_MODIFIED);
66          if (lastModified != null) {
67              newRequest.setHeader(HeaderConstants.IF_MODIFIED_SINCE, lastModified.getValue());
68          }
69          boolean mustRevalidate = false;
70          for(final Header h : cacheEntry.getHeaders(HeaderConstants.CACHE_CONTROL)) {
71              for(final HeaderElement elt : h.getElements()) {
72                  if (HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE.equalsIgnoreCase(elt.getName())
73                      || HeaderConstants.CACHE_CONTROL_PROXY_REVALIDATE.equalsIgnoreCase(elt.getName())) {
74                      mustRevalidate = true;
75                      break;
76                  }
77              }
78          }
79          if (mustRevalidate) {
80              newRequest.addHeader(HeaderConstants.CACHE_CONTROL, HeaderConstants.CACHE_CONTROL_MAX_AGE + "=0");
81          }
82          return newRequest;
83  
84      }
85  
86      /**
87       * When a {@link HttpCacheEntry} does not exist for a specific
88       * {@link org.apache.http.HttpRequest} we attempt to see if an existing
89       * {@link HttpCacheEntry} is appropriate by building a conditional
90       * {@link org.apache.http.HttpRequest} using the variants' ETag values.
91       * If no such values exist, the request is unmodified
92       *
93       * @param request the original request from the caller
94       * @param variants
95       * @return the wrapped request
96       */
97      public HttpRequestWrapper buildConditionalRequestFromVariants(final HttpRequestWrapper request,
98              final Map<String, Variant> variants) {
99          final HttpRequestWrapper newRequest = HttpRequestWrapper.wrap(request.getOriginal());
100         newRequest.setHeaders(request.getAllHeaders());
101 
102         // we do not support partial content so all etags are used
103         final StringBuilder etags = new StringBuilder();
104         boolean first = true;
105         for(final String etag : variants.keySet()) {
106             if (!first) {
107                 etags.append(",");
108             }
109             first = false;
110             etags.append(etag);
111         }
112 
113         newRequest.setHeader(HeaderConstants.IF_NONE_MATCH, etags.toString());
114         return newRequest;
115     }
116 
117     /**
118      * Returns a request to unconditionally validate a cache entry with
119      * the origin. In certain cases (due to multiple intervening caches)
120      * our cache may actually receive a response to a normal conditional
121      * validation where the Date header is actually older than that of
122      * our current cache entry. In this case, the protocol recommendation
123      * is to retry the validation and force syncup with the origin.
124      * @param request client request we are trying to satisfy
125      * @param entry existing cache entry we are trying to validate
126      * @return an unconditional validation request
127      */
128     public HttpRequestWrapper buildUnconditionalRequest(final HttpRequestWrapper request, final HttpCacheEntry entry) {
129         final HttpRequestWrapper newRequest = HttpRequestWrapper.wrap(request.getOriginal());
130         newRequest.setHeaders(request.getAllHeaders());
131         newRequest.addHeader(HeaderConstants.CACHE_CONTROL,HeaderConstants.CACHE_CONTROL_NO_CACHE);
132         newRequest.addHeader(HeaderConstants.PRAGMA,HeaderConstants.CACHE_CONTROL_NO_CACHE);
133         newRequest.removeHeaders(HeaderConstants.IF_RANGE);
134         newRequest.removeHeaders(HeaderConstants.IF_MATCH);
135         newRequest.removeHeaders(HeaderConstants.IF_NONE_MATCH);
136         newRequest.removeHeaders(HeaderConstants.IF_UNMODIFIED_SINCE);
137         newRequest.removeHeaders(HeaderConstants.IF_MODIFIED_SINCE);
138         return newRequest;
139     }
140 
141 }