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.hc.client5.http.impl.cache;
28  
29  import java.util.Collection;
30  
31  import org.apache.hc.client5.http.cache.HeaderConstants;
32  import org.apache.hc.client5.http.cache.HttpCacheEntry;
33  import org.apache.hc.client5.http.cache.ResponseCacheControl;
34  import org.apache.hc.client5.http.validator.ETag;
35  import org.apache.hc.core5.function.Factory;
36  import org.apache.hc.core5.http.Header;
37  import org.apache.hc.core5.http.HttpHeaders;
38  import org.apache.hc.core5.http.HttpRequest;
39  import org.apache.hc.core5.http.message.BufferedHeader;
40  import org.apache.hc.core5.util.CharArrayBuffer;
41  
42  class ConditionalRequestBuilder<T extends HttpRequest> {
43  
44      private final Factory<T, T> messageCopier;
45  
46      ConditionalRequestBuilder(final Factory<T, T> messageCopier) {
47          this.messageCopier = messageCopier;
48      }
49  
50      /**
51       * When a {@link HttpCacheEntry} is stale but 'might' be used as a response
52       * to an {@link org.apache.hc.core5.http.HttpRequest} we will attempt to revalidate
53       * the entry with the origin.  Build the origin {@link org.apache.hc.core5.http.HttpRequest}
54       * here and return it.
55       *
56       * @param cacheControl the cache control directives.
57       * @param request the original request from the caller
58       * @param cacheEntry the entry that needs to be re-validated
59       * @return the wrapped request
60       */
61      public T buildConditionalRequest(final ResponseCacheControl cacheControl, final T request, final HttpCacheEntry cacheEntry) {
62          final T newRequest = messageCopier.create(request);
63  
64          final ETag eTag = cacheEntry.getETag();
65          if (eTag != null) {
66              newRequest.setHeader(HttpHeaders.IF_NONE_MATCH, eTag.toString());
67          }
68          final Header lastModified = cacheEntry.getFirstHeader(HttpHeaders.LAST_MODIFIED);
69          if (lastModified != null) {
70              newRequest.setHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified.getValue());
71          }
72          if (cacheControl.isMustRevalidate() || cacheControl.isProxyRevalidate()) {
73              newRequest.addHeader(HttpHeaders.CACHE_CONTROL, HeaderConstants.CACHE_CONTROL_MAX_AGE + "=0");
74          }
75          return newRequest;
76  
77      }
78  
79      /**
80       * When a {@link HttpCacheEntry} does not exist for a specific
81       * {@link org.apache.hc.core5.http.HttpRequest} we attempt to see if an existing
82       * {@link HttpCacheEntry} is appropriate by building a conditional
83       * {@link org.apache.hc.core5.http.HttpRequest} using the variants' ETag values.
84       * If no such values exist, the request is unmodified
85       *
86       * @param request the original request from the caller
87       * @param variants
88       * @return the wrapped request
89       */
90      public T buildConditionalRequestFromVariants(final T request, final Collection<ETag> variants) {
91          final T newRequest = messageCopier.create(request);
92          final CharArrayBuffer buffer = new CharArrayBuffer(256);
93          buffer.append(HttpHeaders.IF_NONE_MATCH);
94          buffer.append(": ");
95          int i = 0;
96          for (final ETag variant : variants) {
97              if (i > 0) {
98                  buffer.append(", ");
99              }
100             variant.format(buffer);
101             i++;
102         }
103         newRequest.setHeader(BufferedHeader.create(buffer));
104         return newRequest;
105     }
106 
107     /**
108      * Returns a request to unconditionally validate a cache entry with
109      * the origin. In certain cases (due to multiple intervening caches)
110      * our cache may actually receive a response to a normal conditional
111      * validation where the Date header is actually older than that of
112      * our current cache entry. In this case, the protocol recommendation
113      * is to retry the validation and force syncup with the origin.
114      * @param request client request we are trying to satisfy
115      * @return an unconditional validation request
116      */
117     public T buildUnconditionalRequest(final T request) {
118         final T newRequest = messageCopier.create(request);
119         newRequest.addHeader(HttpHeaders.CACHE_CONTROL,HeaderConstants.CACHE_CONTROL_NO_CACHE);
120         newRequest.removeHeaders(HttpHeaders.IF_RANGE);
121         newRequest.removeHeaders(HttpHeaders.IF_MATCH);
122         newRequest.removeHeaders(HttpHeaders.IF_NONE_MATCH);
123         newRequest.removeHeaders(HttpHeaders.IF_UNMODIFIED_SINCE);
124         newRequest.removeHeaders(HttpHeaders.IF_MODIFIED_SINCE);
125         return newRequest;
126     }
127 
128 }