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