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 19 import android.net.http.Headers; 20 21 import java.io.InputStream; 22 23 /** 24 * Encapsulates a resource response. Applications can return an instance of this 25 * class from {@link WebViewClient#shouldInterceptRequest} to provide a custom 26 * response when the WebView requests a particular resource. 27 */ 28 public class WebResourceResponse { 29 // Accessed by jni, do not rename without modifying the jni code. 30 private String mMimeType; 31 private String mEncoding; 32 private InputStream mInputStream; 33 34 /** 35 * Constructs a resource response with the given MIME type, encoding, and 36 * input stream. Callers must implement 37 * {@link InputStream#read(byte[]) InputStream.read(byte[])} for the input 38 * stream. 39 * 40 * @param mimeType the resource response's MIME type, for example text/html 41 * @param encoding the resource response's encoding 42 * @param data the input stream that provides the resource response's data 43 */ 44 public WebResourceResponse(String mimeType, String encoding, 45 InputStream data) { 46 mMimeType = mimeType; 47 mEncoding = encoding; 48 mInputStream = data; 49 } 50 51 /** 52 * Sets the resource response's MIME type, for example text/html. 53 * 54 * @param mimeType the resource response's MIME type 55 */ 56 public void setMimeType(String mimeType) { 57 mMimeType = mimeType; 58 } 59 60 /** 61 * Gets the resource response's MIME type. 62 * 63 * @return the resource response's MIME type 64 */ 65 public String getMimeType() { 66 return mMimeType; 67 } 68 69 /** 70 * Sets the resource response's encoding, for example UTF-8. This is used 71 * to decode the data from the input stream. 72 * 73 * @param encoding the resource response's encoding 74 */ 75 public void setEncoding(String encoding) { 76 mEncoding = encoding; 77 } 78 79 /** 80 * Gets the resource response's encoding. 81 * 82 * @return the resource response's encoding 83 */ 84 public String getEncoding() { 85 return mEncoding; 86 } 87 88 /** 89 * Sets the input stream that provides the resource respone's data. Callers 90 * must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}. 91 * 92 * @param data the input stream that provides the resource response's data 93 */ 94 public void setData(InputStream data) { 95 mInputStream = data; 96 } 97 98 /** 99 * Gets the input stream that provides the resource respone's data. 100 * 101 * @return the input stream that provides the resource response's data 102 */ 103 public InputStream getData() { 104 return mInputStream; 105 } 106 } 107