View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import java.util.Collections;
18  import java.util.List;
19  
20  import org.htmlunit.util.NameValuePair;
21  import org.htmlunit.util.WebResponseWrapper;
22  
23  /**
24   * A {@link WebResponse} implementation to deliver with content from cache. The response
25   * is the same but the request may have some variation like an anchor.
26   *
27   * @author Marc Guillemot
28   * @author Ronald Brill
29   */
30  class WebResponseFromCache extends WebResponseWrapper {
31  
32      private final WebRequest request_;
33      private final List<NameValuePair> responseHeaders_;
34  
35      /**
36       * Wraps the provided cached response for a new request.
37       * @param cachedResponse the response from cache
38       * @param overwriteHeaders list of headers to overwrite cachedResponse headers
39       * @param currentRequest the new request
40       */
41      WebResponseFromCache(final WebResponse cachedResponse,
42              final List<NameValuePair> overwriteHeaders, final WebRequest currentRequest) {
43          super(cachedResponse);
44          request_ = currentRequest;
45          responseHeaders_ = Collections.unmodifiableList(overwriteHeaders);
46      }
47  
48      /**
49       * Wraps the provided response for the given request.
50       *
51       * @param cachedResponse the response from cache
52       * @param currentRequest the new request
53       */
54      WebResponseFromCache(final WebResponse cachedResponse, final WebRequest currentRequest) {
55          super(cachedResponse);
56          request_ = currentRequest;
57          responseHeaders_ = null;
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public WebRequest getWebRequest() {
65          return request_;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public List<NameValuePair> getResponseHeaders() {
73          return responseHeaders_ != null ? responseHeaders_ : super.getResponseHeaders();
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public String getResponseHeaderValue(final String headerName) {
81          if (responseHeaders_ == null) {
82              return super.getResponseHeaderValue(headerName);
83          }
84  
85          for (final NameValuePair pair : responseHeaders_) {
86              if (pair.getName().equalsIgnoreCase(headerName)) {
87                  return pair.getValue();
88              }
89          }
90          return null;
91      }
92  }