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.net.URI;
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.utils.DateUtils;
34  import org.apache.hc.client5.http.utils.URIUtils;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HttpHeaders;
37  import org.apache.hc.core5.http.HttpRequest;
38  import org.apache.hc.core5.http.HttpResponse;
39  
40  class CacheInvalidatorBase {
41  
42      static boolean shouldInvalidateHeadCacheEntry(final HttpRequest req, final HttpCacheEntry parentCacheEntry) {
43          return requestIsGet(req) && isAHeadCacheEntry(parentCacheEntry);
44      }
45  
46      static boolean requestIsGet(final HttpRequest req) {
47          return req.getMethod().equals((HeaderConstants.GET_METHOD));
48      }
49  
50      static boolean isAHeadCacheEntry(final HttpCacheEntry parentCacheEntry) {
51          return parentCacheEntry != null && parentCacheEntry.getRequestMethod().equals(HeaderConstants.HEAD_METHOD);
52      }
53  
54      static boolean isSameHost(final URI requestURI, final URI targetURI) {
55          return targetURI.isAbsolute() && targetURI.getAuthority().equalsIgnoreCase(requestURI.getAuthority());
56      }
57  
58      static boolean requestShouldNotBeCached(final HttpRequest req) {
59          final String method = req.getMethod();
60          return notGetOrHeadRequest(method);
61      }
62  
63      static boolean notGetOrHeadRequest(final String method) {
64          return !(HeaderConstants.GET_METHOD.equals(method) || HeaderConstants.HEAD_METHOD.equals(method));
65      }
66  
67      private static URI getLocationURI(final URI requestUri, final HttpResponse response, final String headerName) {
68          final Header h = response.getFirstHeader(headerName);
69          if (h == null) {
70              return null;
71          }
72          final URI locationUri = HttpCacheSupport.normalizeQuetly(h.getValue());
73          if (locationUri == null) {
74              return requestUri;
75          }
76          if (locationUri.isAbsolute()) {
77              return locationUri;
78          } else {
79              return URIUtils.resolve(requestUri, locationUri);
80          }
81      }
82  
83      static URI getContentLocationURI(final URI requestUri, final HttpResponse response) {
84          return getLocationURI(requestUri, response, HttpHeaders.CONTENT_LOCATION);
85      }
86  
87      static URI getLocationURI(final URI requestUri, final HttpResponse response) {
88          return getLocationURI(requestUri, response, HttpHeaders.LOCATION);
89      }
90  
91      static boolean responseAndEntryEtagsDiffer(final HttpResponse response,
92              final HttpCacheEntry entry) {
93          final Header entryEtag = entry.getFirstHeader(HeaderConstants.ETAG);
94          final Header responseEtag = response.getFirstHeader(HeaderConstants.ETAG);
95          if (entryEtag == null || responseEtag == null) {
96              return false;
97          }
98          return (!entryEtag.getValue().equals(responseEtag.getValue()));
99      }
100 
101     static boolean responseDateOlderThanEntryDate(final HttpResponse response, final HttpCacheEntry entry) {
102         return DateUtils.isBefore(response, entry, HttpHeaders.DATE);
103     }
104 
105 }