WebResourceResponse.java revision 4e584df4cee8334bc371c04a67bcd0a32e2f9480
1c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott/*
2c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott * Copyright (C) 2010 The Android Open Source Project
3c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott *
4c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott * Licensed under the Apache License, Version 2.0 (the "License");
5c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott * you may not use this file except in compliance with the License.
6c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott * You may obtain a copy of the License at
7c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott *
8c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott *      http://www.apache.org/licenses/LICENSE-2.0
9c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott *
10c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott * Unless required by applicable law or agreed to in writing, software
11c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott * distributed under the License is distributed on an "AS IS" BASIS,
12c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott * See the License for the specific language governing permissions and
14c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott * limitations under the License.
15c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott */
16c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
17c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scottpackage android.webkit;
18c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
19c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scottimport android.net.http.Headers;
20c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
21c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scottimport java.io.InputStream;
22c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
23c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott/**
24938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block * Encapsulates a resource response. Applications can return an instance of this
25938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block * class from {@link WebViewClient#shouldInterceptRequest} to provide a custom
26938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block * response when the WebView requests a particular resource.
27c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott */
28c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scottpublic class WebResourceResponse {
29c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    // Accessed by jni, do not rename without modifying the jni code.
30c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    private String mMimeType;
31c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    private String mEncoding;
32c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    private InputStream mInputStream;
33c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
34c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    /**
35938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * Constructs a resource response with the given MIME type, encoding, and
36938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * input stream. Callers must implement
37938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * {@link InputStream#read(byte[]) InputStream.read(byte[])} for the input
38938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * stream.
394e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     *
404e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * @param mimeType the resource response's MIME type, for example text/html
414e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * @param encoding the resource response's encoding
424e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * @param data the input stream that provides the resource response's data
43c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott     */
44c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    public WebResourceResponse(String mimeType, String encoding,
45c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott            InputStream data) {
46c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott        mMimeType = mimeType;
47c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott        mEncoding = encoding;
48c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott        mInputStream = data;
49c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    }
50c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
51c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    /**
52938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * Sets the resource response's MIME type, for example text/html.
534e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     *
544e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * @param mimeType the resource response's MIME type
55c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott     */
56c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    public void setMimeType(String mimeType) {
57c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott        mMimeType = mimeType;
58c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    }
59c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
60c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    /**
61938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * Gets the resource response's MIME type.
624e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     *
634e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * @return the resource response's MIME type
64c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott     */
65c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    public String getMimeType() {
66c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott        return mMimeType;
67c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    }
68c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
69c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    /**
70938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * Sets the resource response's encoding, for example UTF-8. This is used
71938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * to decode the data from the input stream.
724e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     *
734e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * @param encoding the resource response's encoding
74c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott     */
75c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    public void setEncoding(String encoding) {
76c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott        mEncoding = encoding;
77c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    }
78c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
79c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    /**
80938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * Gets the resource response's encoding.
814e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     *
824e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * @return the resource response's encoding
83c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott     */
84c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    public String getEncoding() {
85c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott        return mEncoding;
86c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    }
87c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
88c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    /**
89938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * Sets the input stream that provides the resource respone's data. Callers
90938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}.
914e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     *
924e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * @param data the input stream that provides the resource response's data
93c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott     */
94c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    public void setData(InputStream data) {
95c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott        mInputStream = data;
96c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    }
97c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott
98c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    /**
99938d2fbc57f05cc9bab9b551d7a454644208ee07Steve Block     * Gets the input stream that provides the resource respone's data.
1004e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     *
1014e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * @return the input stream that provides the resource response's data
102c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott     */
103c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    public InputStream getData() {
104c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott        return mInputStream;
105c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott    }
106c12544a201667383bc3dfb4bd3ad62d98cacd24fPatrick Scott}
107