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 * A WebResourceResponse is return by 25 * {@link WebViewClient#shouldInterceptRequest} and 26 * contains the response information for a particular resource. 27 */ 28 public class WebResourceResponse { 29 30 private class Loader extends StreamLoader { 31 Loader(LoadListener loadListener) { 32 super(loadListener); 33 mDataStream = mInputStream; 34 } 35 @Override 36 protected boolean setupStreamAndSendStatus() { 37 mLoadListener.status(1, 1, mDataStream != null ? 200 : 404, ""); 38 return true; 39 } 40 @Override 41 protected void buildHeaders(Headers headers) { 42 headers.setContentType(mMimeType); 43 headers.setContentEncoding(mEncoding); 44 } 45 } 46 47 // Accessed by jni, do not rename without modifying the jni code. 48 private String mMimeType; 49 private String mEncoding; 50 private InputStream mInputStream; 51 52 /** 53 * Construct a response with the given mime type, encoding, and data. 54 * @param mimeType The mime type of the data (i.e. text/html). 55 * @param encoding The encoding of the bytes read from data. 56 * @param data An InputStream for reading custom data. The implementation 57 * must implement {@link InputStream#read(byte[])}. 58 */ 59 public WebResourceResponse(String mimeType, String encoding, 60 InputStream data) { 61 mMimeType = mimeType; 62 mEncoding = encoding; 63 mInputStream = data; 64 } 65 66 /** 67 * Set the mime type of the response data (i.e. text/html). 68 * @param mimeType 69 */ 70 public void setMimeType(String mimeType) { 71 mMimeType = mimeType; 72 } 73 74 /** 75 * @see #setMimeType 76 */ 77 public String getMimeType() { 78 return mMimeType; 79 } 80 81 /** 82 * Set the encoding of the response data (i.e. utf-8). This will be used to 83 * decode the raw bytes from the input stream. 84 * @param encoding 85 */ 86 public void setEncoding(String encoding) { 87 mEncoding = encoding; 88 } 89 90 /** 91 * @see #setEncoding 92 */ 93 public String getEncoding() { 94 return mEncoding; 95 } 96 97 /** 98 * Set the input stream containing the data for this resource. 99 * @param data An InputStream for reading custom data. The implementation 100 * must implement {@link InputStream#read(byte[])}. 101 */ 102 public void setData(InputStream data) { 103 mInputStream = data; 104 } 105 106 /** 107 * @see #setData 108 */ 109 public InputStream getData() { 110 return mInputStream; 111 } 112 113 StreamLoader loader(LoadListener listener) { 114 return new Loader(listener); 115 } 116 } 117