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.http.impl.client.cache;
28  
29  import java.io.IOException;
30  import java.util.Date;
31  
32  import org.apache.http.Header;
33  import org.apache.http.HeaderIterator;
34  import org.apache.http.HttpResponse;
35  import org.apache.http.HttpStatus;
36  import org.apache.http.annotation.Contract;
37  import org.apache.http.annotation.ThreadingBehavior;
38  import org.apache.http.client.cache.HeaderConstants;
39  import org.apache.http.client.cache.HttpCacheEntry;
40  import org.apache.http.client.cache.Resource;
41  import org.apache.http.client.cache.ResourceFactory;
42  import org.apache.http.client.utils.DateUtils;
43  import org.apache.http.message.HeaderGroup;
44  import org.apache.http.protocol.HTTP;
45  import org.apache.http.util.Args;
46  
47  /**
48   * Update a {@link HttpCacheEntry} with new or updated information based on the latest
49   * 304 status response from the Server.  Use the {@link HttpResponse} to perform
50   * the update.
51   *
52   * @since 4.1
53   */
54  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
55  class CacheEntryUpdater {
56  
57      private final ResourceFactory resourceFactory;
58  
59      CacheEntryUpdater() {
60          this(new HeapResourceFactory());
61      }
62  
63      CacheEntryUpdater(final ResourceFactory resourceFactory) {
64          super();
65          this.resourceFactory = resourceFactory;
66      }
67  
68      /**
69       * Update the entry with the new information from the response.  Should only be used for
70       * 304 responses.
71       *
72       * @param requestId
73       * @param entry The cache Entry to be updated
74       * @param requestDate When the request was performed
75       * @param responseDate When the response was gotten
76       * @param response The HttpResponse from the backend server call
77       * @return HttpCacheEntry an updated version of the cache entry
78       * @throws java.io.IOException if something bad happens while trying to read the body from the original entry
79       */
80      public HttpCacheEntry updateCacheEntry(
81              final String requestId,
82              final HttpCacheEntry entry,
83              final Date requestDate,
84              final Date responseDate,
85              final HttpResponse response) throws IOException {
86          Args.check(response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_MODIFIED,
87                  "Response must have 304 status code");
88          final Header[] mergedHeaders = mergeHeaders(entry, response);
89          Resource resource = null;
90          if (entry.getResource() != null) {
91              resource = resourceFactory.copy(requestId, entry.getResource());
92          }
93          return new HttpCacheEntry(
94                  requestDate,
95                  responseDate,
96                  entry.getStatusLine(),
97                  mergedHeaders,
98                  resource,
99                  entry.getRequestMethod());
100     }
101 
102     protected Header[] mergeHeaders(final HttpCacheEntry entry, final HttpResponse response) {
103         if (entryAndResponseHaveDateHeader(entry, response)
104                 && entryDateHeaderNewerThenResponse(entry, response)) {
105             // Don't merge headers, keep the entry's headers as they are newer.
106             return entry.getAllHeaders();
107         }
108 
109         final HeaderGroup headerGroup = new HeaderGroup();
110         headerGroup.setHeaders(entry.getAllHeaders());
111         // Remove cache headers that match response
112         for (final HeaderIterator it = response.headerIterator(); it.hasNext(); ) {
113             final Header responseHeader = it.nextHeader();
114             // Since we do not expect a content in a 304 response, should retain the original Content-Encoding and Content-Length header
115             if (HTTP.CONTENT_ENCODING.equals(responseHeader.getName())
116                     || HTTP.CONTENT_LEN.equals(responseHeader.getName())) {
117                 continue;
118             }
119             final Header[] matchingHeaders = headerGroup.getHeaders(responseHeader.getName());
120             for (final Header matchingHeader : matchingHeaders) {
121                 headerGroup.removeHeader(matchingHeader);
122             }
123 
124         }
125         // remove cache entry 1xx warnings
126         for (final HeaderIterator it = headerGroup.iterator(); it.hasNext(); ) {
127             final Header cacheHeader = it.nextHeader();
128             if (HeaderConstants.WARNING.equalsIgnoreCase(cacheHeader.getName())) {
129                 final String warningValue = cacheHeader.getValue();
130                 if (warningValue != null && warningValue.startsWith("1")) {
131                     it.remove();
132                 }
133             }
134         }
135         for (final HeaderIterator it = response.headerIterator(); it.hasNext(); ) {
136             final Header responseHeader = it.nextHeader();
137             // Since we do not expect a content in a 304 response, should avoid updating Content-Encoding and Content-Length header
138             if (HTTP.CONTENT_ENCODING.equals(responseHeader.getName())
139                     || HTTP.CONTENT_LEN.equals(responseHeader.getName())) {
140                 continue;
141             }
142             headerGroup.addHeader(responseHeader);
143         }
144         return headerGroup.getAllHeaders();
145     }
146 
147     private boolean entryDateHeaderNewerThenResponse(final HttpCacheEntry entry, final HttpResponse response) {
148         final Date entryDate = DateUtils.parseDate(entry.getFirstHeader(HTTP.DATE_HEADER)
149                 .getValue());
150         final Date responseDate = DateUtils.parseDate(response.getFirstHeader(HTTP.DATE_HEADER)
151                 .getValue());
152         return entryDate != null && responseDate != null && entryDate.after(responseDate);
153     }
154 
155     private boolean entryAndResponseHaveDateHeader(final HttpCacheEntry entry, final HttpResponse response) {
156         return entry.getFirstHeader(HTTP.DATE_HEADER) != null
157                 && response.getFirstHeader(HTTP.DATE_HEADER) != null;
158 
159     }
160 
161 }