Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.webkit; |
| 18 | |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.SystemApi; |
Mathew Inwood | 42afea2 | 2018-08-16 19:18:28 +0100 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 22 | |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 23 | import java.io.InputStream; |
Paul Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 24 | import java.io.StringBufferInputStream; |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 25 | import java.util.Map; |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 26 | |
| 27 | /** |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 28 | * 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 Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 31 | */ |
Selim Gurun | 98fe09c | 2015-05-21 17:24:08 -0700 | [diff] [blame] | 32 | public class WebResourceResponse { |
Mathew Inwood | 42afea2 | 2018-08-16 19:18:28 +0100 | [diff] [blame] | 33 | @UnsupportedAppUsage |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 34 | private boolean mImmutable; |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 35 | private String mMimeType; |
| 36 | private String mEncoding; |
Mathew Inwood | 42afea2 | 2018-08-16 19:18:28 +0100 | [diff] [blame] | 37 | @UnsupportedAppUsage |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 38 | private int mStatusCode; |
| 39 | private String mReasonPhrase; |
| 40 | private Map<String, String> mResponseHeaders; |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 41 | private InputStream mInputStream; |
| 42 | |
| 43 | /** |
Torne (Richard Coles) | c848639 | 2018-12-12 11:11:33 -0500 | [diff] [blame] | 44 | * Constructs a resource response with the given MIME type, character encoding, |
| 45 | * and input stream. Callers must implement |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 46 | * {@link InputStream#read(byte[]) InputStream.read(byte[])} for the input |
| 47 | * stream. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 48 | * |
Torne (Richard Coles) | c848639 | 2018-12-12 11:11:33 -0500 | [diff] [blame] | 49 | * <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 Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 59 | * @param data the input stream that provides the resource response's data. Must not be a |
| 60 | * StringBufferInputStream. |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 61 | */ |
| 62 | public WebResourceResponse(String mimeType, String encoding, |
| 63 | InputStream data) { |
| 64 | mMimeType = mimeType; |
| 65 | mEncoding = encoding; |
Paul Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 66 | setData(data); |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 70 | * 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) | c848639 | 2018-12-12 11:11:33 -0500 | [diff] [blame] | 74 | * <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 Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 79 | * @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 Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 81 | * @param reasonPhrase the phrase describing the status code, for example "OK". Must be |
| 82 | * non-empty. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 83 | * @param responseHeaders the resource response's headers represented as a mapping of header |
| 84 | * name -> header value. |
Paul Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 85 | * @param data the input stream that provides the resource response's data. Must not be a |
| 86 | * StringBufferInputStream. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 87 | */ |
| 88 | public WebResourceResponse(String mimeType, String encoding, int statusCode, |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 89 | @NonNull String reasonPhrase, Map<String, String> responseHeaders, InputStream data) { |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 90 | this(mimeType, encoding, data); |
| 91 | setStatusCodeAndReasonPhrase(statusCode, reasonPhrase); |
| 92 | setResponseHeaders(responseHeaders); |
| 93 | } |
| 94 | |
| 95 | /** |
Mikhail Naganov | 25e8954 | 2015-02-09 17:01:40 +0000 | [diff] [blame] | 96 | * Sets the resource response's MIME type, for example "text/html". |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 97 | * |
Mikhail Naganov | 25e8954 | 2015-02-09 17:01:40 +0000 | [diff] [blame] | 98 | * @param mimeType The resource response's MIME type |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 99 | */ |
| 100 | public void setMimeType(String mimeType) { |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 101 | checkImmutable(); |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 102 | mMimeType = mimeType; |
| 103 | } |
| 104 | |
| 105 | /** |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 106 | * Gets the resource response's MIME type. |
| 107 | * |
| 108 | * @return The resource response's MIME type |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 109 | */ |
| 110 | public String getMimeType() { |
| 111 | return mMimeType; |
| 112 | } |
| 113 | |
| 114 | /** |
Mikhail Naganov | 25e8954 | 2015-02-09 17:01:40 +0000 | [diff] [blame] | 115 | * Sets the resource response's encoding, for example "UTF-8". This is used |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 116 | * to decode the data from the input stream. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 117 | * |
Mikhail Naganov | 25e8954 | 2015-02-09 17:01:40 +0000 | [diff] [blame] | 118 | * @param encoding The resource response's encoding |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 119 | */ |
| 120 | public void setEncoding(String encoding) { |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 121 | checkImmutable(); |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 122 | mEncoding = encoding; |
| 123 | } |
| 124 | |
| 125 | /** |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 126 | * Gets the resource response's encoding. |
| 127 | * |
| 128 | * @return The resource response's encoding |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 129 | */ |
| 130 | public String getEncoding() { |
| 131 | return mEncoding; |
| 132 | } |
| 133 | |
| 134 | /** |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 135 | * 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 Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 139 | * @param reasonPhrase the phrase describing the status code, for example "OK". Must be |
| 140 | * non-empty. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 141 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 142 | public void setStatusCodeAndReasonPhrase(int statusCode, @NonNull String reasonPhrase) { |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 143 | checkImmutable(); |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 144 | 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 Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 166 | * Gets the resource response's status code. |
| 167 | * |
| 168 | * @return The resource response's status code. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 169 | */ |
| 170 | public int getStatusCode() { |
| 171 | return mStatusCode; |
| 172 | } |
| 173 | |
| 174 | /** |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 175 | * Gets the description of the resource response's status code. |
| 176 | * |
| 177 | * @return The description of the resource response's status code. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 178 | */ |
| 179 | public String getReasonPhrase() { |
| 180 | return mReasonPhrase; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Sets the headers for the resource response. |
| 185 | * |
Mikhail Naganov | 25e8954 | 2015-02-09 17:01:40 +0000 | [diff] [blame] | 186 | * @param headers Mapping of header name -> header value. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 187 | */ |
| 188 | public void setResponseHeaders(Map<String, String> headers) { |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 189 | checkImmutable(); |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 190 | mResponseHeaders = headers; |
| 191 | } |
| 192 | |
| 193 | /** |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 194 | * Gets the headers for the resource response. |
| 195 | * |
| 196 | * @return The headers for the resource response. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 197 | */ |
| 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 Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 204 | * must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 205 | * |
Paul Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 206 | * @param data the input stream that provides the resource response's data. Must not be a |
| 207 | * StringBufferInputStream. |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 208 | */ |
| 209 | public void setData(InputStream data) { |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 210 | checkImmutable(); |
Paul Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 211 | // 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 Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 216 | mInputStream = data; |
| 217 | } |
| 218 | |
| 219 | /** |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 220 | * 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 Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 223 | */ |
| 224 | public InputStream getData() { |
| 225 | return mInputStream; |
| 226 | } |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 227 | |
| 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 Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 248 | } |