blob: ad6e9aa31382a67c9bc96fe9ca35eee28a36d558 [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
Patrick Scottc12544a2010-11-11 13:16:44 -050019import java.io.InputStream;
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010020import java.util.Map;
Patrick Scottc12544a2010-11-11 13:16:44 -050021
22/**
Steve Block938d2fb2012-02-21 02:10:10 +000023 * Encapsulates a resource response. Applications can return an instance of this
24 * class from {@link WebViewClient#shouldInterceptRequest} to provide a custom
25 * response when the WebView requests a particular resource.
Patrick Scottc12544a2010-11-11 13:16:44 -050026 */
27public class WebResourceResponse {
Patrick Scottc12544a2010-11-11 13:16:44 -050028 private String mMimeType;
29 private String mEncoding;
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010030 private int mStatusCode;
31 private String mReasonPhrase;
32 private Map<String, String> mResponseHeaders;
Patrick Scottc12544a2010-11-11 13:16:44 -050033 private InputStream mInputStream;
34
35 /**
Steve Block938d2fb2012-02-21 02:10:10 +000036 * Constructs a resource response with the given MIME type, encoding, and
37 * input stream. Callers must implement
38 * {@link InputStream#read(byte[]) InputStream.read(byte[])} for the input
39 * stream.
Steve Block4e584df2012-04-24 23:12:47 +010040 *
41 * @param mimeType the resource response's MIME type, for example text/html
42 * @param encoding the resource response's encoding
43 * @param data the input stream that provides the resource response's data
Patrick Scottc12544a2010-11-11 13:16:44 -050044 */
45 public WebResourceResponse(String mimeType, String encoding,
46 InputStream data) {
47 mMimeType = mimeType;
48 mEncoding = encoding;
49 mInputStream = data;
50 }
51
52 /**
Marcin Kosibad72e7ba2014-07-15 17:33:47 +010053 * Constructs a resource response with the given parameters. Callers must
54 * implement {@link InputStream#read(byte[]) InputStream.read(byte[])} for
55 * the input stream.
56 *
57 * @param mimeType the resource response's MIME type, for example text/html
58 * @param encoding the resource response's encoding
59 * @param statusCode the status code needs to be in the ranges [100, 299], [400, 599].
60 * Causing a redirect by specifying a 3xx code is not supported.
61 * @param reasonPhrase the phrase describing the status code, for example "OK". Must be non-null
62 * and not empty.
63 * @param responseHeaders the resource response's headers represented as a mapping of header
64 * name -> header value.
65 * @param data the input stream that provides the resource response's data
66 */
67 public WebResourceResponse(String mimeType, String encoding, int statusCode,
68 String reasonPhrase, Map<String, String> responseHeaders, InputStream data) {
69 this(mimeType, encoding, data);
70 setStatusCodeAndReasonPhrase(statusCode, reasonPhrase);
71 setResponseHeaders(responseHeaders);
72 }
73
74 /**
Steve Block938d2fb2012-02-21 02:10:10 +000075 * Sets the resource response's MIME type, for example text/html.
Steve Block4e584df2012-04-24 23:12:47 +010076 *
77 * @param mimeType the resource response's MIME type
Patrick Scottc12544a2010-11-11 13:16:44 -050078 */
79 public void setMimeType(String mimeType) {
80 mMimeType = mimeType;
81 }
82
83 /**
Steve Block938d2fb2012-02-21 02:10:10 +000084 * Gets the resource response's MIME type.
Steve Block4e584df2012-04-24 23:12:47 +010085 *
86 * @return the resource response's MIME type
Patrick Scottc12544a2010-11-11 13:16:44 -050087 */
88 public String getMimeType() {
89 return mMimeType;
90 }
91
92 /**
Steve Block938d2fb2012-02-21 02:10:10 +000093 * Sets the resource response's encoding, for example UTF-8. This is used
94 * to decode the data from the input stream.
Steve Block4e584df2012-04-24 23:12:47 +010095 *
96 * @param encoding the resource response's encoding
Patrick Scottc12544a2010-11-11 13:16:44 -050097 */
98 public void setEncoding(String encoding) {
99 mEncoding = encoding;
100 }
101
102 /**
Steve Block938d2fb2012-02-21 02:10:10 +0000103 * Gets the resource response's encoding.
Steve Block4e584df2012-04-24 23:12:47 +0100104 *
105 * @return the resource response's encoding
Patrick Scottc12544a2010-11-11 13:16:44 -0500106 */
107 public String getEncoding() {
108 return mEncoding;
109 }
110
111 /**
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100112 * Sets the resource response's status code and reason phrase.
113 *
114 * @param statusCode the status code needs to be in the ranges [100, 299], [400, 599].
115 * Causing a redirect by specifying a 3xx code is not supported.
116 * @param reasonPhrase the phrase describing the status code, for example "OK". Must be non-null
117 * and not empty.
118 */
119 public void setStatusCodeAndReasonPhrase(int statusCode, String reasonPhrase) {
120 if (statusCode < 100)
121 throw new IllegalArgumentException("statusCode can't be less than 100.");
122 if (statusCode > 599)
123 throw new IllegalArgumentException("statusCode can't be greater than 599.");
124 if (statusCode > 299 && statusCode < 400)
125 throw new IllegalArgumentException("statusCode can't be in the [300, 399] range.");
126 if (reasonPhrase == null)
127 throw new IllegalArgumentException("reasonPhrase can't be null.");
128 if (reasonPhrase.trim().isEmpty())
129 throw new IllegalArgumentException("reasonPhrase can't be empty.");
130 for (int i = 0; i < reasonPhrase.length(); i++) {
131 int c = reasonPhrase.charAt(i);
132 if (c > 0x7F) {
133 throw new IllegalArgumentException(
134 "reasonPhrase can't contain non-ASCII characters.");
135 }
136 }
137 mStatusCode = statusCode;
138 mReasonPhrase = reasonPhrase;
139 }
140
141 /**
142 * Gets the resource response's status code.
143 *
144 * @return the resource response's status code.
145 */
146 public int getStatusCode() {
147 return mStatusCode;
148 }
149
150 /**
151 * Gets the description of the resource response's status code.
152 *
153 * @return the description of the resource response's status code.
154 */
155 public String getReasonPhrase() {
156 return mReasonPhrase;
157 }
158
159 /**
160 * Sets the headers for the resource response.
161 *
162 * @param headers mapping of header name -> header value.
163 */
164 public void setResponseHeaders(Map<String, String> headers) {
165 mResponseHeaders = headers;
166 }
167
168 /**
169 * Gets the headers for the resource response.
170 *
171 * @return the headers for the resource response.
172 */
173 public Map<String, String> getResponseHeaders() {
174 return mResponseHeaders;
175 }
176
177 /**
178 * Sets the input stream that provides the resource response's data. Callers
Steve Block938d2fb2012-02-21 02:10:10 +0000179 * must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}.
Steve Block4e584df2012-04-24 23:12:47 +0100180 *
181 * @param data the input stream that provides the resource response's data
Patrick Scottc12544a2010-11-11 13:16:44 -0500182 */
183 public void setData(InputStream data) {
184 mInputStream = data;
185 }
186
187 /**
Marcin Kosibad72e7ba2014-07-15 17:33:47 +0100188 * Gets the input stream that provides the resource response's data.
Steve Block4e584df2012-04-24 23:12:47 +0100189 *
190 * @return the input stream that provides the resource response's data
Patrick Scottc12544a2010-11-11 13:16:44 -0500191 */
192 public InputStream getData() {
193 return mInputStream;
194 }
Patrick Scottc12544a2010-11-11 13:16:44 -0500195}