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  
31  import org.apache.hc.client5.http.cache.HeaderConstants;
32  import org.apache.hc.core5.http.HeaderElement;
33  import org.apache.hc.core5.http.HttpRequest;
34  import org.apache.hc.core5.http.HttpVersion;
35  import org.apache.hc.core5.http.ProtocolVersion;
36  import org.apache.hc.core5.http.message.MessageSupport;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * Determines if an HttpRequest is allowed to be served from the cache.
42   */
43  class CacheableRequestPolicy {
44  
45      private static final Logger LOG = LoggerFactory.getLogger(CacheableRequestPolicy.class);
46  
47      /**
48       * Determines if an HttpRequest can be served from the cache.
49       *
50       * @param request
51       *            an HttpRequest
52       * @return boolean Is it possible to serve this request from cache
53       */
54      public boolean isServableFromCache(final HttpRequest request) {
55          final String method = request.getMethod();
56  
57          final ProtocolVersion pv = request.getVersion() != null ? request.getVersion() : HttpVersion.DEFAULT;
58          if (HttpVersion.HTTP_1_1.compareToVersion(pv) != 0) {
59              LOG.debug("non-HTTP/1.1 request is not serveable from cache");
60              return false;
61          }
62  
63          if (!method.equals(HeaderConstants.GET_METHOD) && !method.equals(HeaderConstants.HEAD_METHOD)) {
64              if (LOG.isDebugEnabled()) {
65                  LOG.debug("{} request is not serveable from cache", method);
66              }
67              return false;
68          }
69  
70          if (request.countHeaders(HeaderConstants.PRAGMA) > 0) {
71              LOG.debug("request with Pragma header is not serveable from cache");
72              return false;
73          }
74  
75          final Iterator<HeaderElement> it = MessageSupport.iterate(request, HeaderConstants.CACHE_CONTROL);
76          while (it.hasNext()) {
77              final HeaderElement cacheControlElement = it.next();
78              if (HeaderConstants.CACHE_CONTROL_NO_STORE.equalsIgnoreCase(cacheControlElement.getName())) {
79                  LOG.debug("Request with no-store is not serveable from cache");
80                  return false;
81              }
82              if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equalsIgnoreCase(cacheControlElement.getName())) {
83                  LOG.debug("Request with no-cache is not serveable from cache");
84                  return false;
85              }
86          }
87  
88          LOG.debug("Request is serveable from cache");
89          return true;
90      }
91  
92  }