blob: aae3056f61912add63d19442ada0f42a27fd2f65 [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 /**
Steve Block938d2fb2012-02-21 02:10:10 +000044 * Constructs a resource response with the given MIME type, encoding, and
45 * input stream. Callers must implement
46 * {@link InputStream#read(byte[]) InputStream.read(byte[])} for the input
47 * stream.
Steve Block4e584df2012-04-24 23:12:47 +010048 *
49 * @param mimeType the resource response's MIME type, for example text/html
50 * @param encoding the resource response's encoding
Paul Miller3ff073a2014-09-12 13:12:36 -070051 * @param data the input stream that provides the resource response's data. Must not be a
52 * StringBufferInputStream.
Patrick Scottc12544a2010-11-11 13:16:44 -050053 */
54 public WebResourceResponse(String mimeType, String encoding,
55 InputStream data) {
56 mMimeType = mimeType;
57 mEncoding = encoding;
Paul Miller3ff073a2014-09-12 13:12:36 -070058 setData(data);
Patrick Scottc12544a2010-11-11 13:16:44 -050059 }
60
61 /**
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010062 * Constructs a resource response with the given parameters. Callers must
63 * implement {@link InputStream#read(byte[]) InputStream.read(byte[])} for
64 * the input stream.
65 *
66 * @param mimeType the resource response's MIME type, for example text/html
67 * @param encoding the resource response's encoding
68 * @param statusCode the status code needs to be in the ranges [100, 299], [400, 599].
69 * Causing a redirect by specifying a 3xx code is not supported.
Nate Fischer3442c742017-09-08 17:02:00 -070070 * @param reasonPhrase the phrase describing the status code, for example "OK". Must be
71 * non-empty.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010072 * @param responseHeaders the resource response's headers represented as a mapping of header
73 * name -> header value.
Paul Miller3ff073a2014-09-12 13:12:36 -070074 * @param data the input stream that provides the resource response's data. Must not be a
75 * StringBufferInputStream.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010076 */
77 public WebResourceResponse(String mimeType, String encoding, int statusCode,
Nate Fischer3442c742017-09-08 17:02:00 -070078 @NonNull String reasonPhrase, Map<String, String> responseHeaders, InputStream data) {
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010079 this(mimeType, encoding, data);
80 setStatusCodeAndReasonPhrase(statusCode, reasonPhrase);
81 setResponseHeaders(responseHeaders);
82 }
83
84 /**
Mikhail Naganov25e89542015-02-09 17:01:40 +000085 * Sets the resource response's MIME type, for example &quot;text/html&quot;.
Steve Block4e584df2012-04-24 23:12:47 +010086 *
Mikhail Naganov25e89542015-02-09 17:01:40 +000087 * @param mimeType The resource response's MIME type
Patrick Scottc12544a2010-11-11 13:16:44 -050088 */
89 public void setMimeType(String mimeType) {
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -070090 checkImmutable();
Patrick Scottc12544a2010-11-11 13:16:44 -050091 mMimeType = mimeType;
92 }
93
94 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -070095 * Gets the resource response's MIME type.
96 *
97 * @return The resource response's MIME type
Patrick Scottc12544a2010-11-11 13:16:44 -050098 */
99 public String getMimeType() {
100 return mMimeType;
101 }
102
103 /**
Mikhail Naganov25e89542015-02-09 17:01:40 +0000104 * Sets the resource response's encoding, for example &quot;UTF-8&quot;. This is used
Steve Block938d2fb2012-02-21 02:10:10 +0000105 * to decode the data from the input stream.
Steve Block4e584df2012-04-24 23:12:47 +0100106 *
Mikhail Naganov25e89542015-02-09 17:01:40 +0000107 * @param encoding The resource response's encoding
Patrick Scottc12544a2010-11-11 13:16:44 -0500108 */
109 public void setEncoding(String encoding) {
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700110 checkImmutable();
Patrick Scottc12544a2010-11-11 13:16:44 -0500111 mEncoding = encoding;
112 }
113
114 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700115 * Gets the resource response's encoding.
116 *
117 * @return The resource response's encoding
Patrick Scottc12544a2010-11-11 13:16:44 -0500118 */
119 public String getEncoding() {
120 return mEncoding;
121 }
122
123 /**
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100124 * Sets the resource response's status code and reason phrase.
125 *
126 * @param statusCode the status code needs to be in the ranges [100, 299], [400, 599].
127 * Causing a redirect by specifying a 3xx code is not supported.
Nate Fischer3442c742017-09-08 17:02:00 -0700128 * @param reasonPhrase the phrase describing the status code, for example "OK". Must be
129 * non-empty.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100130 */
Nate Fischer3442c742017-09-08 17:02:00 -0700131 public void setStatusCodeAndReasonPhrase(int statusCode, @NonNull String reasonPhrase) {
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700132 checkImmutable();
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100133 if (statusCode < 100)
134 throw new IllegalArgumentException("statusCode can't be less than 100.");
135 if (statusCode > 599)
136 throw new IllegalArgumentException("statusCode can't be greater than 599.");
137 if (statusCode > 299 && statusCode < 400)
138 throw new IllegalArgumentException("statusCode can't be in the [300, 399] range.");
139 if (reasonPhrase == null)
140 throw new IllegalArgumentException("reasonPhrase can't be null.");
141 if (reasonPhrase.trim().isEmpty())
142 throw new IllegalArgumentException("reasonPhrase can't be empty.");
143 for (int i = 0; i < reasonPhrase.length(); i++) {
144 int c = reasonPhrase.charAt(i);
145 if (c > 0x7F) {
146 throw new IllegalArgumentException(
147 "reasonPhrase can't contain non-ASCII characters.");
148 }
149 }
150 mStatusCode = statusCode;
151 mReasonPhrase = reasonPhrase;
152 }
153
154 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700155 * Gets the resource response's status code.
156 *
157 * @return The resource response's status code.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100158 */
159 public int getStatusCode() {
160 return mStatusCode;
161 }
162
163 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700164 * Gets the description of the resource response's status code.
165 *
166 * @return The description of the resource response's status code.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100167 */
168 public String getReasonPhrase() {
169 return mReasonPhrase;
170 }
171
172 /**
173 * Sets the headers for the resource response.
174 *
Mikhail Naganov25e89542015-02-09 17:01:40 +0000175 * @param headers Mapping of header name -> header value.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100176 */
177 public void setResponseHeaders(Map<String, String> headers) {
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700178 checkImmutable();
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100179 mResponseHeaders = headers;
180 }
181
182 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700183 * Gets the headers for the resource response.
184 *
185 * @return The headers for the resource response.
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100186 */
187 public Map<String, String> getResponseHeaders() {
188 return mResponseHeaders;
189 }
190
191 /**
192 * Sets the input stream that provides the resource response's data. Callers
Steve Block938d2fb2012-02-21 02:10:10 +0000193 * must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}.
Steve Block4e584df2012-04-24 23:12:47 +0100194 *
Paul Miller3ff073a2014-09-12 13:12:36 -0700195 * @param data the input stream that provides the resource response's data. Must not be a
196 * StringBufferInputStream.
Patrick Scottc12544a2010-11-11 13:16:44 -0500197 */
198 public void setData(InputStream data) {
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700199 checkImmutable();
Paul Miller3ff073a2014-09-12 13:12:36 -0700200 // If data is (or is a subclass of) StringBufferInputStream
201 if (data != null && StringBufferInputStream.class.isAssignableFrom(data.getClass())) {
202 throw new IllegalArgumentException("StringBufferInputStream is deprecated and must " +
203 "not be passed to a WebResourceResponse");
204 }
Patrick Scottc12544a2010-11-11 13:16:44 -0500205 mInputStream = data;
206 }
207
208 /**
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700209 * Gets the input stream that provides the resource response's data.
210 *
211 * @return The input stream that provides the resource response's data
Patrick Scottc12544a2010-11-11 13:16:44 -0500212 */
213 public InputStream getData() {
214 return mInputStream;
215 }
Mikhail Naganov8d4f07f2015-05-12 17:44:15 -0700216
217 /**
218 * The internal version of the constructor that doesn't perform arguments checks.
219 * @hide
220 */
221 @SystemApi
222 public WebResourceResponse(boolean immutable, String mimeType, String encoding, int statusCode,
223 String reasonPhrase, Map<String, String> responseHeaders, InputStream data) {
224 mImmutable = immutable;
225 mMimeType = mimeType;
226 mEncoding = encoding;
227 mStatusCode = statusCode;
228 mReasonPhrase = reasonPhrase;
229 mResponseHeaders = responseHeaders;
230 mInputStream = data;
231 }
232
233 private void checkImmutable() {
234 if (mImmutable)
235 throw new IllegalStateException("This WebResourceResponse instance is immutable");
236 }
Patrick Scottc12544a2010-11-11 13:16:44 -0500237}