Home | History | Annotate | Download | only in webkit
      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 java.io.InputStream;
     20 import java.util.Map;
     21 
     22 /**
     23  * 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.
     26  */
     27 public class WebResourceResponse {
     28     private String mMimeType;
     29     private String mEncoding;
     30     private int mStatusCode;
     31     private String mReasonPhrase;
     32     private Map<String, String> mResponseHeaders;
     33     private InputStream mInputStream;
     34 
     35     /**
     36      * 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.
     40      *
     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
     44      */
     45     public WebResourceResponse(String mimeType, String encoding,
     46             InputStream data) {
     47         mMimeType = mimeType;
     48         mEncoding = encoding;
     49         mInputStream = data;
     50     }
     51 
     52     /**
     53      * 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     /**
     75      * Sets the resource response's MIME type, for example text/html.
     76      *
     77      * @param mimeType the resource response's MIME type
     78      */
     79     public void setMimeType(String mimeType) {
     80         mMimeType = mimeType;
     81     }
     82 
     83     /**
     84      * Gets the resource response's MIME type.
     85      *
     86      * @return the resource response's MIME type
     87      */
     88     public String getMimeType() {
     89         return mMimeType;
     90     }
     91 
     92     /**
     93      * Sets the resource response's encoding, for example UTF-8. This is used
     94      * to decode the data from the input stream.
     95      *
     96      * @param encoding the resource response's encoding
     97      */
     98     public void setEncoding(String encoding) {
     99         mEncoding = encoding;
    100     }
    101 
    102     /**
    103      * Gets the resource response's encoding.
    104      *
    105      * @return the resource response's encoding
    106      */
    107     public String getEncoding() {
    108         return mEncoding;
    109     }
    110 
    111     /**
    112      * 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
    179      * must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}.
    180      *
    181      * @param data the input stream that provides the resource response's data
    182      */
    183     public void setData(InputStream data) {
    184         mInputStream = data;
    185     }
    186 
    187     /**
    188      * Gets the input stream that provides the resource response's data.
    189      *
    190      * @return the input stream that provides the resource response's data
    191      */
    192     public InputStream getData() {
    193         return mInputStream;
    194     }
    195 }
    196