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