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 | /** |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 44 | * 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 Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 48 | * |
| 49 | * @param mimeType the resource response's MIME type, for example text/html |
| 50 | * @param encoding the resource response's encoding |
Paul Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 51 | * @param data the input stream that provides the resource response's data. Must not be a |
| 52 | * StringBufferInputStream. |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 53 | */ |
| 54 | public WebResourceResponse(String mimeType, String encoding, |
| 55 | InputStream data) { |
| 56 | mMimeType = mimeType; |
| 57 | mEncoding = encoding; |
Paul Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 58 | setData(data); |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 62 | * 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 Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 70 | * @param reasonPhrase the phrase describing the status code, for example "OK". Must be |
| 71 | * non-empty. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 72 | * @param responseHeaders the resource response's headers represented as a mapping of header |
| 73 | * name -> header value. |
Paul Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 74 | * @param data the input stream that provides the resource response's data. Must not be a |
| 75 | * StringBufferInputStream. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 76 | */ |
| 77 | public WebResourceResponse(String mimeType, String encoding, int statusCode, |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 78 | @NonNull String reasonPhrase, Map<String, String> responseHeaders, InputStream data) { |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 79 | this(mimeType, encoding, data); |
| 80 | setStatusCodeAndReasonPhrase(statusCode, reasonPhrase); |
| 81 | setResponseHeaders(responseHeaders); |
| 82 | } |
| 83 | |
| 84 | /** |
Mikhail Naganov | 25e8954 | 2015-02-09 17:01:40 +0000 | [diff] [blame] | 85 | * Sets the resource response's MIME type, for example "text/html". |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 86 | * |
Mikhail Naganov | 25e8954 | 2015-02-09 17:01:40 +0000 | [diff] [blame] | 87 | * @param mimeType The resource response's MIME type |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 88 | */ |
| 89 | public void setMimeType(String mimeType) { |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 90 | checkImmutable(); |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 91 | mMimeType = mimeType; |
| 92 | } |
| 93 | |
| 94 | /** |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 95 | * Gets the resource response's MIME type. |
| 96 | * |
| 97 | * @return The resource response's MIME type |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 98 | */ |
| 99 | public String getMimeType() { |
| 100 | return mMimeType; |
| 101 | } |
| 102 | |
| 103 | /** |
Mikhail Naganov | 25e8954 | 2015-02-09 17:01:40 +0000 | [diff] [blame] | 104 | * 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] | 105 | * to decode the data from the input stream. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 106 | * |
Mikhail Naganov | 25e8954 | 2015-02-09 17:01:40 +0000 | [diff] [blame] | 107 | * @param encoding The resource response's encoding |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 108 | */ |
| 109 | public void setEncoding(String encoding) { |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 110 | checkImmutable(); |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 111 | mEncoding = encoding; |
| 112 | } |
| 113 | |
| 114 | /** |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 115 | * Gets the resource response's encoding. |
| 116 | * |
| 117 | * @return The resource response's encoding |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 118 | */ |
| 119 | public String getEncoding() { |
| 120 | return mEncoding; |
| 121 | } |
| 122 | |
| 123 | /** |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 124 | * 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 Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 128 | * @param reasonPhrase the phrase describing the status code, for example "OK". Must be |
| 129 | * non-empty. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 130 | */ |
Nate Fischer | 3442c74 | 2017-09-08 17:02:00 -0700 | [diff] [blame] | 131 | public void setStatusCodeAndReasonPhrase(int statusCode, @NonNull String reasonPhrase) { |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 132 | checkImmutable(); |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 133 | 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 Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 155 | * Gets the resource response's status code. |
| 156 | * |
| 157 | * @return The resource response's status code. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 158 | */ |
| 159 | public int getStatusCode() { |
| 160 | return mStatusCode; |
| 161 | } |
| 162 | |
| 163 | /** |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 164 | * Gets the description of the resource response's status code. |
| 165 | * |
| 166 | * @return The description of the resource response's status code. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 167 | */ |
| 168 | public String getReasonPhrase() { |
| 169 | return mReasonPhrase; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Sets the headers for the resource response. |
| 174 | * |
Mikhail Naganov | 25e8954 | 2015-02-09 17:01:40 +0000 | [diff] [blame] | 175 | * @param headers Mapping of header name -> header value. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 176 | */ |
| 177 | public void setResponseHeaders(Map<String, String> headers) { |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 178 | checkImmutable(); |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 179 | mResponseHeaders = headers; |
| 180 | } |
| 181 | |
| 182 | /** |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 183 | * Gets the headers for the resource response. |
| 184 | * |
| 185 | * @return The headers for the resource response. |
Marcin Kosiba | d72e7ba | 2014-07-15 17:33:47 +0100 | [diff] [blame] | 186 | */ |
| 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 Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 193 | * must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 194 | * |
Paul Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 195 | * @param data the input stream that provides the resource response's data. Must not be a |
| 196 | * StringBufferInputStream. |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 197 | */ |
| 198 | public void setData(InputStream data) { |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 199 | checkImmutable(); |
Paul Miller | 3ff073a | 2014-09-12 13:12:36 -0700 | [diff] [blame] | 200 | // 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 Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 205 | mInputStream = data; |
| 206 | } |
| 207 | |
| 208 | /** |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 209 | * 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 Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 212 | */ |
| 213 | public InputStream getData() { |
| 214 | return mInputStream; |
| 215 | } |
Mikhail Naganov | 8d4f07f | 2015-05-12 17:44:15 -0700 | [diff] [blame] | 216 | |
| 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 Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 237 | } |