View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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.util;
16  
17  import java.io.IOException;
18  import java.io.InputStream;
19  import java.nio.charset.Charset;
20  import java.util.List;
21  
22  import org.htmlunit.WebRequest;
23  import org.htmlunit.WebResponse;
24  
25  /**
26   * Provides a convenient implementation of the {@link WebResponse} interface that can be subclassed
27   * by developers wishing to adapt a particular WebResponse.
28   * This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped
29   * web response object.
30   *
31   * @author Marc Guillemot
32   * @author Ahmed Ashour
33   * @author Ronald Brill
34   * @author Atsushi Nakagawa
35   * @author Lai Quang Duong
36   */
37  public class WebResponseWrapper extends WebResponse {
38  
39      private final WebResponse wrappedWebResponse_;
40  
41      /**
42       * Constructs a WebResponse object wrapping provided WebResponse.
43       * @param webResponse the webResponse that does the real work
44       * @throws IllegalArgumentException if the webResponse is {@code null}
45       */
46      public WebResponseWrapper(final WebResponse webResponse) throws IllegalArgumentException {
47          super(null, null, 0);
48          if (webResponse == null) {
49              throw new IllegalArgumentException("Wrapped WebResponse can't be null");
50          }
51          wrappedWebResponse_ = webResponse;
52      }
53  
54      /**
55       * {@inheritDoc}
56       * The default behavior of this method is to return getContentLength() on the wrapped webResponse object.
57       */
58      @Override
59      public long getContentLength() {
60          return wrappedWebResponse_.getContentLength();
61      }
62  
63      /**
64       * {@inheritDoc}
65       * The default behavior of this method is to return getContentAsStream() on the wrapped webResponse object.
66       */
67      @Override
68      public InputStream getContentAsStream() throws IOException {
69          return wrappedWebResponse_.getContentAsStream();
70      }
71  
72      /**
73       * {@inheritDoc}
74       * The default behavior of this method is to return wasContentCharsetTentative() on the wrapped webResponse object.
75       */
76      @Override
77      public boolean wasContentCharsetTentative() {
78          return wrappedWebResponse_.wasContentCharsetTentative();
79      }
80  
81      /**
82       * {@inheritDoc}
83       * The default behavior of this method is to return getContentAsString() on the wrapped webResponse object.
84       */
85      @Override
86      public String getContentAsString() {
87          return wrappedWebResponse_.getContentAsString(getContentCharset());
88      }
89  
90      /**
91       * {@inheritDoc}
92       * The default behavior of this method is to return getContentAsString(Charset) on the wrapped webResponse object.
93       */
94      @Override
95      public String getContentAsString(final Charset encoding) {
96          return wrappedWebResponse_.getContentAsString(encoding);
97      }
98  
99      /**
100      * {@inheritDoc}
101      * The default behavior of this method is to return getHeaderContentCharset() on the wrapped webResponse object.
102      */
103     @Override
104     public Charset getHeaderContentCharset() {
105         return wrappedWebResponse_.getHeaderContentCharset();
106     }
107 
108     /**
109      * {@inheritDoc}
110      * The default behavior of this method is to return getContentCharset() on the wrapped webResponse object.
111      */
112     @Override
113     public Charset getContentCharset() {
114         return wrappedWebResponse_.getContentCharset();
115     }
116 
117     /**
118      * {@inheritDoc}
119      * The default behavior of this method is to return getContentType() on the wrapped webResponse object.
120      */
121     @Override
122     public String getContentType() {
123         return wrappedWebResponse_.getContentType();
124     }
125 
126     /**
127      * {@inheritDoc}
128      * The default behavior of this method is to return getLoadTime() on the wrapped webResponse object.
129      */
130     @Override
131     public long getLoadTime() {
132         return wrappedWebResponse_.getLoadTime();
133     }
134 
135     /**
136      * {@inheritDoc}
137      * The default behavior of this method is to return getResponseHeaders() on the wrapped webResponse object.
138      */
139     @Override
140     public List<NameValuePair> getResponseHeaders() {
141         return wrappedWebResponse_.getResponseHeaders();
142     }
143 
144     /**
145      * {@inheritDoc}
146      * The default behavior of this method is to return getResponseHeaderValue() on the wrapped webResponse object.
147      */
148     @Override
149     public String getResponseHeaderValue(final String headerName) {
150         return wrappedWebResponse_.getResponseHeaderValue(headerName);
151     }
152 
153     /**
154      * {@inheritDoc}
155      * The default behavior of this method is to return getStatusCode() on the wrapped webResponse object.
156      */
157     @Override
158     public int getStatusCode() {
159         return wrappedWebResponse_.getStatusCode();
160     }
161 
162     /**
163      * {@inheritDoc}
164      * The default behavior of this method is to return getStatusMessage() on the wrapped webResponse object.
165      */
166     @Override
167     public String getStatusMessage() {
168         return wrappedWebResponse_.getStatusMessage();
169     }
170 
171     /**
172      * {@inheritDoc}
173      * The default behavior of this method is to return getWebRequest() on the wrapped webResponse object.
174      */
175     @Override
176     public WebRequest getWebRequest() {
177         return wrappedWebResponse_.getWebRequest();
178     }
179 
180     /**
181      * {@inheritDoc}
182      * The default behavior of this method is to call cleanUp() on the wrapped webResponse object.
183      */
184     @Override
185     public void cleanUp() {
186         wrappedWebResponse_.cleanUp();
187     }
188 
189     @Override
190     public InputStream getContentAsStreamWithBomIfApplicable() throws IOException {
191         return wrappedWebResponse_.getContentAsStreamWithBomIfApplicable();
192     }
193 
194     @Override
195     public boolean isSuccess() {
196         return wrappedWebResponse_.isSuccess();
197     }
198 
199     @Override
200     public boolean isSuccessOrUseProxy() {
201         return wrappedWebResponse_.isSuccessOrUseProxy();
202     }
203 
204     @Override
205     public boolean isSuccessOrUseProxyOrNotModified() {
206         return wrappedWebResponse_.isSuccessOrUseProxyOrNotModified();
207     }
208 }