blob: e66596b5c0e94e1da95145b3559f31196a61b0d3 [file] [log] [blame]
Patrick Scottc12544a2010-11-11 13:16:44 -05001/*
2 * Copyright (C) 2010 The Android Open Source Project
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 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit;
18
Nate Fischer3442c742017-09-08 17:02:00 -070019import android.annotation.NonNull;
20import android.annotation.SystemApi;
Mathew Inwood42afea22018-08-16 19:18:28 +010021import android.annotation.UnsupportedAppUsage;
Nate Fischer3442c742017-09-08 17:02:00 -070022
Patrick Scottc12544a2010-11-11 13:16:44 -050023import java.io.InputStream;
Paul Miller3ff073a2014-09-12 13:12:36 -070024import java.io.StringBufferInputStream;
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010025import java.util.Map;
Patrick Scottc12544a2010-11-11 13:16:44 -050026
27/**
Steve Block938d2fb2012-02-21 02:10:10 +000028 * Encapsulates a resource response. Applications can return an instance of this
29 * class from {@link WebViewClient#shouldInterceptRequest} to provide a custom
30 * response when the WebView requests a particular resource.
Patrick Scottc12544a2010-11-11 13:16:44 -050031 */
Selim Gurun98fe09c2015-05-21 17:24:08 -070032public class WebResourceResponse {
Mathew Inwood42afea22018-08-16 19:18:28 +010033 @UnsupportedAppUsage
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -070034 private boolean mImmutable;
Patrick Scottc12544a2010-11-11 13:16:44 -050035 private String mMimeType;
36 private String mEncoding;
Mathew Inwood42afea22018-08-16 19:18:28 +010037 @UnsupportedAppUsage
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010038 private int mStatusCode;
39 private String mReasonPhrase;
40 private Map<String, String> mResponseHeaders;
Patrick Scottc12544a2010-11-11 13:16:44 -050041 private InputStream mInputStream;
42
43 /**
Torne (Richard Coles)c8486392018-12-12 11:11:33 -050044 * Constructs a resource response with the given MIME type, character encoding,
45 * and input stream. Callers must implement
Steve Block938d2fb2012-02-21 02:10:10 +000046 * {@link InputStream#read(byte[]) InputStream.read(byte[])} for the input
47 * stream.
Steve Block4e584df2012-04-24 23:12:47 +010048 *
Torne (Richard Coles)c8486392018-12-12 11:11:33 -050049 * <p class="note"><b>Note:</b> The MIME type and character encoding must
50 * be specified as separate parameters (for example {@code "text/html"} and
51 * {@code "utf-8"}), not a single value like the {@code "text/html; charset=utf-8"}
52 * format used in the HTTP Content-Type header. Do not use the value of a HTTP
53 * Content-Encoding header for {@code encoding}, as that header does not specify a
54 * character encoding. Content without a defined character encoding (for example
55 * image resources) should pass {@code null} for {@code encoding}.
56 *
57 * @param mimeType the resource response's MIME type, for example {@code "text/html"}.
58 * @param encoding the resource response's character encoding, for example {@code "utf-8"}.
Paul Miller3ff073a2014-09-12 13:12:36 -070059 * @param data the input stream that provides the resource response's data. Must not be a
60 * StringBufferInputStream.
Patrick Scottc12544a2010-11-11 13:16:44 -050061 */
62 public WebResourceResponse(String mimeType, String encoding,
63 InputStream data) {
64 mMimeType = mimeType;
65 mEncoding = encoding;
Paul Miller3ff073a2014-09-12 13:12:36 -070066 setData(data);
Patrick Scottc12544a2010-11-11 13:16:44 -050067 }
68
69 /**
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010070 * Constructs a resource response with the given parameters. Callers must
71 * implement {@link InputStream#read(byte[]) InputStream.read(byte[])} for
72 * the input stream.
73 *
Torne (Richard Coles)c8486392018-12-12 11:11:33 -050074 * <p class="note"><b>Note:</b> See {@link #WebResourceResponse(String,String,InputStream)}
75 * for details on what should be specified for {@code mimeType} and {@code encoding}.
76 *
77 * @param mimeType the resource response's MIME type, for example {@code "text/html"}.
78 * @param encoding the resource response's character encoding, for example {@code "utf-8"}.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010079 * @param statusCode the status code needs to be in the ranges [100, 299], [400, 599].
80 * Causing a redirect by specifying a 3xx code is not supported.
Nate Fischer3442c742017-09-08 17:02:00 -070081 * @param reasonPhrase the phrase describing the status code, for example "OK". Must be
82 * non-empty.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010083 * @param responseHeaders the resource response's headers represented as a mapping of header
84 * name -> header value.
Paul Miller3ff073a2014-09-12 13:12:36 -070085 * @param data the input stream that provides the resource response's data. Must not be a
86 * StringBufferInputStream.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010087 */
88 public WebResourceResponse(String mimeType, String encoding, int statusCode,
Nate Fischer3442c742017-09-08 17:02:00 -070089 @NonNull String reasonPhrase, Map<String, String> responseHeaders, InputStream data) {
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010090 this(mimeType, encoding, data);
91 setStatusCodeAndReasonPhrase(statusCode, reasonPhrase);
92 setResponseHeaders(responseHeaders);
93 }
94
95 /**
Mikhail Naganov25e89542015-02-09 17:01:40 +000096 * Sets the resource response's MIME type, for example &quot;text/html&quot;.
Steve Block4e584df2012-04-24 23:12:47 +010097 *
Mikhail Naganov25e89542015-02-09 17:01:40 +000098 * @param mimeType The resource response's MIME type
Patrick Scottc12544a2010-11-11 13:16:44 -050099 */
100 public void setMimeType(String mimeType) {
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700101 checkImmutable();
Patrick Scottc12544a2010-11-11 13:16:44 -0500102 mMimeType = mimeType;
103 }
104
105 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700106 * Gets the resource response's MIME type.
107 *
108 * @return The resource response's MIME type
Patrick Scottc12544a2010-11-11 13:16:44 -0500109 */
110 public String getMimeType() {
111 return mMimeType;
112 }
113
114 /**
Mikhail Naganov25e89542015-02-09 17:01:40 +0000115 * Sets the resource response's encoding, for example &quot;UTF-8&quot;. This is used
Steve Block938d2fb2012-02-21 02:10:10 +0000116 * to decode the data from the input stream.
Steve Block4e584df2012-04-24 23:12:47 +0100117 *
Mikhail Naganov25e89542015-02-09 17:01:40 +0000118 * @param encoding The resource response's encoding
Patrick Scottc12544a2010-11-11 13:16:44 -0500119 */
120 public void setEncoding(String encoding) {
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700121 checkImmutable();
Patrick Scottc12544a2010-11-11 13:16:44 -0500122 mEncoding = encoding;
123 }
124
125 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700126 * Gets the resource response's encoding.
127 *
128 * @return The resource response's encoding
Patrick Scottc12544a2010-11-11 13:16:44 -0500129 */
130 public String getEncoding() {
131 return mEncoding;
132 }
133
134 /**
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100135 * Sets the resource response's status code and reason phrase.
136 *
137 * @param statusCode the status code needs to be in the ranges [100, 299], [400, 599].
138 * Causing a redirect by specifying a 3xx code is not supported.
Nate Fischer3442c742017-09-08 17:02:00 -0700139 * @param reasonPhrase the phrase describing the status code, for example "OK". Must be
140 * non-empty.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100141 */
Nate Fischer3442c742017-09-08 17:02:00 -0700142 public void setStatusCodeAndReasonPhrase(int statusCode, @NonNull String reasonPhrase) {
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700143 checkImmutable();
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100144 if (statusCode < 100)
145 throw new IllegalArgumentException("statusCode can't be less than 100.");
146 if (statusCode > 599)
147 throw new IllegalArgumentException("statusCode can't be greater than 599.");
148 if (statusCode > 299 && statusCode < 400)
149 throw new IllegalArgumentException("statusCode can't be in the [300, 399] range.");
150 if (reasonPhrase == null)
151 throw new IllegalArgumentException("reasonPhrase can't be null.");
152 if (reasonPhrase.trim().isEmpty())
153 throw new IllegalArgumentException("reasonPhrase can't be empty.");
154 for (int i = 0; i < reasonPhrase.length(); i++) {
155 int c = reasonPhrase.charAt(i);
156 if (c > 0x7F) {
157 throw new IllegalArgumentException(
158 "reasonPhrase can't contain non-ASCII characters.");
159 }
160 }
161 mStatusCode = statusCode;
162 mReasonPhrase = reasonPhrase;
163 }
164
165 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700166 * Gets the resource response's status code.
167 *
168 * @return The resource response's status code.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100169 */
170 public int getStatusCode() {
171 return mStatusCode;
172 }
173
174 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700175 * Gets the description of the resource response's status code.
176 *
177 * @return The description of the resource response's status code.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100178 */
179 public String getReasonPhrase() {
180 return mReasonPhrase;
181 }
182
183 /**
184 * Sets the headers for the resource response.
185 *
Mikhail Naganov25e89542015-02-09 17:01:40 +0000186 * @param headers Mapping of header name -> header value.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100187 */
188 public void setResponseHeaders(Map<String, String> headers) {
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700189 checkImmutable();
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100190 mResponseHeaders = headers;
191 }
192
193 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700194 * Gets the headers for the resource response.
195 *
196 * @return The headers for the resource response.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100197 */
198 public Map<String, String> getResponseHeaders() {
199 return mResponseHeaders;
200 }
201
202 /**
203 * Sets the input stream that provides the resource response's data. Callers
Steve Block938d2fb2012-02-21 02:10:10 +0000204 * must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}.
Steve Block4e584df2012-04-24 23:12:47 +0100205 *
Paul Miller3ff073a2014-09-12 13:12:36 -0700206 * @param data the input stream that provides the resource response's data. Must not be a
207 * StringBufferInputStream.
Patrick Scottc12544a2010-11-11 13:16:44 -0500208 */
209 public void setData(InputStream data) {
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700210 checkImmutable();
Paul Miller3ff073a2014-09-12 13:12:36 -0700211 // If data is (or is a subclass of) StringBufferInputStream
212 if (data != null && StringBufferInputStream.class.isAssignableFrom(data.getClass())) {
213 throw new IllegalArgumentException("StringBufferInputStream is deprecated and must " +
214 "not be passed to a WebResourceResponse");
215 }
Patrick Scottc12544a2010-11-11 13:16:44 -0500216 mInputStream = data;
217 }
218
219 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700220 * Gets the input stream that provides the resource response's data.
221 *
222 * @return The input stream that provides the resource response's data
Patrick Scottc12544a2010-11-11 13:16:44 -0500223 */
224 public InputStream getData() {
225 return mInputStream;
226 }
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700227
228 /**
229 * The internal version of the constructor that doesn't perform arguments checks.
230 * @hide
231 */
232 @SystemApi
233 public WebResourceResponse(boolean immutable, String mimeType, String encoding, int statusCode,
234 String reasonPhrase, Map<String, String> responseHeaders, InputStream data) {
235 mImmutable = immutable;
236 mMimeType = mimeType;
237 mEncoding = encoding;
238 mStatusCode = statusCode;
239 mReasonPhrase = reasonPhrase;
240 mResponseHeaders = responseHeaders;
241 mInputStream = data;
242 }
243
244 private void checkImmutable() {
245 if (mImmutable)
246 throw new IllegalStateException("This WebResourceResponse instance is immutable");
247 }
Patrick Scottc12544a2010-11-11 13:16:44 -0500248}