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