Home | History | Annotate | Download | only in util
      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 com.android.quicksearchbox.util;
     18 
     19 import java.io.IOException;
     20 import java.util.HashMap;
     21 import java.util.Map;
     22 
     23 /**
     24  * An interface that can issue HTTP GET / POST requests
     25  * with timeouts.
     26  */
     27 public interface HttpHelper {
     28 
     29     public String get(GetRequest request) throws IOException, HttpException;
     30 
     31     public String get(String url, Map<String,String> requestHeaders)
     32             throws IOException, HttpException;
     33 
     34     public String post(PostRequest request) throws IOException, HttpException;
     35 
     36     public String post(String url, Map<String,String> requestHeaders, String content)
     37             throws IOException, HttpException;
     38 
     39     public void setConnectTimeout(int timeoutMillis);
     40 
     41     public void setReadTimeout(int timeoutMillis);
     42 
     43     public static class GetRequest {
     44         private String mUrl;
     45         private Map<String,String> mHeaders;
     46 
     47         /**
     48          * Creates a new request.
     49          */
     50         public GetRequest() {
     51         }
     52 
     53         /**
     54          * Creates a new request.
     55          *
     56          * @param url Request URI.
     57          */
     58         public GetRequest(String url) {
     59             mUrl = url;
     60         }
     61 
     62         /**
     63          * Gets the request URI.
     64          */
     65         public String getUrl() {
     66             return mUrl;
     67         }
     68         /**
     69          * Sets the request URI.
     70          */
     71         public void setUrl(String url) {
     72             mUrl = url;
     73         }
     74 
     75         /**
     76          * Gets the request headers.
     77          *
     78          * @return The response headers. May return {@code null} if no headers are set.
     79          */
     80         public Map<String, String> getHeaders() {
     81             return mHeaders;
     82         }
     83 
     84         /**
     85          * Sets a request header.
     86          *
     87          * @param name Header name.
     88          * @param value Header value.
     89          */
     90         public void setHeader(String name, String value) {
     91             if (mHeaders == null) {
     92                 mHeaders = new HashMap<String,String>();
     93             }
     94             mHeaders.put(name, value);
     95         }
     96     }
     97 
     98     public static class PostRequest extends GetRequest {
     99 
    100         private String mContent;
    101 
    102         public PostRequest() {
    103         }
    104 
    105         public PostRequest(String url) {
    106             super(url);
    107         }
    108 
    109         public void setContent(String content) {
    110             mContent = content;
    111         }
    112 
    113         public String getContent() {
    114             return mContent;
    115         }
    116     }
    117 
    118     /**
    119      * A HTTP exception.
    120      */
    121     public static class HttpException extends IOException {
    122         private final int mStatusCode;
    123         private final String mReasonPhrase;
    124 
    125         public HttpException(int statusCode, String reasonPhrase) {
    126             super(statusCode + " " + reasonPhrase);
    127             mStatusCode = statusCode;
    128             mReasonPhrase = reasonPhrase;
    129         }
    130 
    131         /**
    132          * Gets the HTTP response status code.
    133          */
    134         public int getStatusCode() {
    135             return mStatusCode;
    136         }
    137 
    138         /**
    139          * Gets the HTTP response reason phrase.
    140          */
    141         public String getReasonPhrase() {
    142             return mReasonPhrase;
    143         }
    144     }
    145 
    146     /**
    147      * An interface for URL rewriting.
    148      */
    149     public static interface UrlRewriter {
    150       public String rewrite(String url);
    151     }
    152 }
    153