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.Iterator;
30  import java.util.Map;
31  
32  import org.apache.hc.client5.http.cache.HeaderConstants;
33  import org.apache.hc.client5.http.cache.HttpCacheEntry;
34  import org.apache.hc.core5.function.Factory;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HeaderElement;
37  import org.apache.hc.core5.http.HttpRequest;
38  import org.apache.hc.core5.http.message.MessageSupport;
39  
40  class ConditionalRequestBuilder<T extends HttpRequest> {
41  
42      private final Factory<T, T> messageCopier;
43  
44      ConditionalRequestBuilder(final Factory<T, T> messageCopier) {
45          this.messageCopier = messageCopier;
46      }
47  
48      /**
49       * When a {@link HttpCacheEntry} is stale but 'might' be used as a response
50       * to an {@link org.apache.hc.core5.http.HttpRequest} we will attempt to revalidate
51       * the entry with the origin.  Build the origin {@link org.apache.hc.core5.http.HttpRequest}
52       * here and return it.
53       *
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 T request, final HttpCacheEntry cacheEntry) {
59          final T newRequest = messageCopier.create(request);
60  
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          final Iterator<HeaderElement> it = MessageSupport.iterate(cacheEntry, HeaderConstants.CACHE_CONTROL);
71          while (it.hasNext()) {
72              final HeaderElement elt = it.next();
73              if (HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE.equalsIgnoreCase(elt.getName())
74                      || HeaderConstants.CACHE_CONTROL_PROXY_REVALIDATE.equalsIgnoreCase(elt.getName())) {
75                  mustRevalidate = true;
76                  break;
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.hc.core5.http.HttpRequest} we attempt to see if an existing
89       * {@link HttpCacheEntry} is appropriate by building a conditional
90       * {@link org.apache.hc.core5.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 T buildConditionalRequestFromVariants(final T request, final Map<String, Variant> variants) {
98          final T newRequest = messageCopier.create(request);
99  
100         // we do not support partial content so all etags are used
101         final StringBuilder etags = new StringBuilder();
102         boolean first = true;
103         for(final String etag : variants.keySet()) {
104             if (!first) {
105                 etags.append(",");
106             }
107             first = false;
108             etags.append(etag);
109         }
110 
111         newRequest.setHeader(HeaderConstants.IF_NONE_MATCH, etags.toString());
112         return newRequest;
113     }
114 
115     /**
116      * Returns a request to unconditionally validate a cache entry with
117      * the origin. In certain cases (due to multiple intervening caches)
118      * our cache may actually receive a response to a normal conditional
119      * validation where the Date header is actually older than that of
120      * our current cache entry. In this case, the protocol recommendation
121      * is to retry the validation and force syncup with the origin.
122      * @param request client request we are trying to satisfy
123      * @return an unconditional validation request
124      */
125     public T buildUnconditionalRequest(final T request) {
126         final T newRequest = messageCopier.create(request);
127         newRequest.addHeader(HeaderConstants.CACHE_CONTROL,HeaderConstants.CACHE_CONTROL_NO_CACHE);
128         newRequest.addHeader(HeaderConstants.PRAGMA,HeaderConstants.CACHE_CONTROL_NO_CACHE);
129         newRequest.removeHeaders(HeaderConstants.IF_RANGE);
130         newRequest.removeHeaders(HeaderConstants.IF_MATCH);
131         newRequest.removeHeaders(HeaderConstants.IF_NONE_MATCH);
132         newRequest.removeHeaders(HeaderConstants.IF_UNMODIFIED_SINCE);
133         newRequest.removeHeaders(HeaderConstants.IF_MODIFIED_SINCE);
134         return newRequest;
135     }
136 
137 }