Home | History | Annotate | Download | only in net
      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.net;
     18 
     19 
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 import android.text.TextUtils;
     23 
     24 import org.apache.http.client.HttpClient;
     25 
     26 import java.net.InetSocketAddress;
     27 import java.net.URLConnection;
     28 import java.util.List;
     29 import java.util.Locale;
     30 
     31 /**
     32  * Describes a proxy configuration.
     33  *
     34  * Proxy configurations are already integrated within the Apache HTTP stack.
     35  * So {@link URLConnection} and {@link HttpClient} will use them automatically.
     36  *
     37  * Other HTTP stacks will need to obtain the proxy info from
     38  * {@link Proxy#PROXY_CHANGE_ACTION} broadcast as the extra {@link Proxy#EXTRA_PROXY_INFO}.
     39  *
     40  * @deprecated Please use {@link java.net.URL#openConnection}, {@link java.net.Proxy} and
     41  *     friends. The Apache HTTP client is no longer maintained and may be removed in a future
     42  *     release. Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     43  *     for further details.
     44  */
     45 @Deprecated
     46 public class ProxyInfo implements Parcelable {
     47 
     48     private String mHost;
     49     private int mPort;
     50     private String mExclusionList;
     51     private String[] mParsedExclusionList;
     52 
     53     private Uri mPacFileUrl;
     54     /**
     55      *@hide
     56      */
     57     public static final String LOCAL_EXCL_LIST = "";
     58     /**
     59      *@hide
     60      */
     61     public static final int LOCAL_PORT = -1;
     62     /**
     63      *@hide
     64      */
     65     public static final String LOCAL_HOST = "localhost";
     66 
     67     /**
     68      * Constructs a {@link ProxyInfo} object that points at a Direct proxy
     69      * on the specified host and port.
     70      */
     71     public static ProxyInfo buildDirectProxy(String host, int port) {
     72         return new ProxyInfo(host, port, null);
     73     }
     74 
     75     /**
     76      * Constructs a {@link ProxyInfo} object that points at a Direct proxy
     77      * on the specified host and port.
     78      *
     79      * The proxy will not be used to access any host in exclusion list, exclList.
     80      *
     81      * @param exclList Hosts to exclude using the proxy on connections for.  These
     82      *                 hosts can use wildcards such as *.example.com.
     83      */
     84     public static ProxyInfo buildDirectProxy(String host, int port, List<String> exclList) {
     85         String[] array = exclList.toArray(new String[exclList.size()]);
     86         return new ProxyInfo(host, port, TextUtils.join(",", array), array);
     87     }
     88 
     89     /**
     90      * Construct a {@link ProxyInfo} that will download and run the PAC script
     91      * at the specified URL.
     92      */
     93     public static ProxyInfo buildPacProxy(Uri pacUri) {
     94         return new ProxyInfo(pacUri);
     95     }
     96 
     97     /**
     98      * Create a ProxyProperties that points at a HTTP Proxy.
     99      * @hide
    100      */
    101     public ProxyInfo(String host, int port, String exclList) {
    102         mHost = host;
    103         mPort = port;
    104         setExclusionList(exclList);
    105         mPacFileUrl = Uri.EMPTY;
    106     }
    107 
    108     /**
    109      * Create a ProxyProperties that points at a PAC URL.
    110      * @hide
    111      */
    112     public ProxyInfo(Uri pacFileUrl) {
    113         mHost = LOCAL_HOST;
    114         mPort = LOCAL_PORT;
    115         setExclusionList(LOCAL_EXCL_LIST);
    116         if (pacFileUrl == null) {
    117             throw new NullPointerException();
    118         }
    119         mPacFileUrl = pacFileUrl;
    120     }
    121 
    122     /**
    123      * Create a ProxyProperties that points at a PAC URL.
    124      * @hide
    125      */
    126     public ProxyInfo(String pacFileUrl) {
    127         mHost = LOCAL_HOST;
    128         mPort = LOCAL_PORT;
    129         setExclusionList(LOCAL_EXCL_LIST);
    130         mPacFileUrl = Uri.parse(pacFileUrl);
    131     }
    132 
    133     /**
    134      * Only used in PacManager after Local Proxy is bound.
    135      * @hide
    136      */
    137     public ProxyInfo(Uri pacFileUrl, int localProxyPort) {
    138         mHost = LOCAL_HOST;
    139         mPort = localProxyPort;
    140         setExclusionList(LOCAL_EXCL_LIST);
    141         if (pacFileUrl == null) {
    142             throw new NullPointerException();
    143         }
    144         mPacFileUrl = pacFileUrl;
    145     }
    146 
    147     private ProxyInfo(String host, int port, String exclList, String[] parsedExclList) {
    148         mHost = host;
    149         mPort = port;
    150         mExclusionList = exclList;
    151         mParsedExclusionList = parsedExclList;
    152         mPacFileUrl = Uri.EMPTY;
    153     }
    154 
    155     // copy constructor instead of clone
    156     /**
    157      * @hide
    158      */
    159     public ProxyInfo(ProxyInfo source) {
    160         if (source != null) {
    161             mHost = source.getHost();
    162             mPort = source.getPort();
    163             mPacFileUrl = source.mPacFileUrl;
    164             mExclusionList = source.getExclusionListAsString();
    165             mParsedExclusionList = source.mParsedExclusionList;
    166         } else {
    167             mPacFileUrl = Uri.EMPTY;
    168         }
    169     }
    170 
    171     /**
    172      * @hide
    173      */
    174     public InetSocketAddress getSocketAddress() {
    175         InetSocketAddress inetSocketAddress = null;
    176         try {
    177             inetSocketAddress = new InetSocketAddress(mHost, mPort);
    178         } catch (IllegalArgumentException e) { }
    179         return inetSocketAddress;
    180     }
    181 
    182     /**
    183      * Returns the URL of the current PAC script or null if there is
    184      * no PAC script.
    185      */
    186     public Uri getPacFileUrl() {
    187         return mPacFileUrl;
    188     }
    189 
    190     /**
    191      * When configured to use a Direct Proxy this returns the host
    192      * of the proxy.
    193      */
    194     public String getHost() {
    195         return mHost;
    196     }
    197 
    198     /**
    199      * When configured to use a Direct Proxy this returns the port
    200      * of the proxy
    201      */
    202     public int getPort() {
    203         return mPort;
    204     }
    205 
    206     /**
    207      * When configured to use a Direct Proxy this returns the list
    208      * of hosts for which the proxy is ignored.
    209      */
    210     public String[] getExclusionList() {
    211         return mParsedExclusionList;
    212     }
    213 
    214     /**
    215      * comma separated
    216      * @hide
    217      */
    218     public String getExclusionListAsString() {
    219         return mExclusionList;
    220     }
    221 
    222     // comma separated
    223     private void setExclusionList(String exclusionList) {
    224         mExclusionList = exclusionList;
    225         if (mExclusionList == null) {
    226             mParsedExclusionList = new String[0];
    227         } else {
    228             mParsedExclusionList = exclusionList.toLowerCase(Locale.ROOT).split(",");
    229         }
    230     }
    231 
    232     /**
    233      * @hide
    234      */
    235     public boolean isValid() {
    236         if (!Uri.EMPTY.equals(mPacFileUrl)) return true;
    237         return Proxy.PROXY_VALID == Proxy.validate(mHost == null ? "" : mHost,
    238                                                 mPort == 0 ? "" : Integer.toString(mPort),
    239                                                 mExclusionList == null ? "" : mExclusionList);
    240     }
    241 
    242     /**
    243      * @hide
    244      */
    245     public java.net.Proxy makeProxy() {
    246         java.net.Proxy proxy = java.net.Proxy.NO_PROXY;
    247         if (mHost != null) {
    248             try {
    249                 InetSocketAddress inetSocketAddress = new InetSocketAddress(mHost, mPort);
    250                 proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, inetSocketAddress);
    251             } catch (IllegalArgumentException e) {
    252             }
    253         }
    254         return proxy;
    255     }
    256 
    257     @Override
    258     public String toString() {
    259         StringBuilder sb = new StringBuilder();
    260         if (!Uri.EMPTY.equals(mPacFileUrl)) {
    261             sb.append("PAC Script: ");
    262             sb.append(mPacFileUrl);
    263         }
    264         if (mHost != null) {
    265             sb.append("[");
    266             sb.append(mHost);
    267             sb.append("] ");
    268             sb.append(Integer.toString(mPort));
    269             if (mExclusionList != null) {
    270                     sb.append(" xl=").append(mExclusionList);
    271             }
    272         } else {
    273             sb.append("[ProxyProperties.mHost == null]");
    274         }
    275         return sb.toString();
    276     }
    277 
    278     @Override
    279     public boolean equals(Object o) {
    280         if (!(o instanceof ProxyInfo)) return false;
    281         ProxyInfo p = (ProxyInfo)o;
    282         // If PAC URL is present in either then they must be equal.
    283         // Other parameters will only be for fall back.
    284         if (!Uri.EMPTY.equals(mPacFileUrl)) {
    285             return mPacFileUrl.equals(p.getPacFileUrl()) && mPort == p.mPort;
    286         }
    287         if (!Uri.EMPTY.equals(p.mPacFileUrl)) {
    288             return false;
    289         }
    290         if (mExclusionList != null && !mExclusionList.equals(p.getExclusionListAsString())) {
    291             return false;
    292         }
    293         if (mHost != null && p.getHost() != null && mHost.equals(p.getHost()) == false) {
    294             return false;
    295         }
    296         if (mHost != null && p.mHost == null) return false;
    297         if (mHost == null && p.mHost != null) return false;
    298         if (mPort != p.mPort) return false;
    299         return true;
    300     }
    301 
    302     /**
    303      * Implement the Parcelable interface
    304      * @hide
    305      */
    306     public int describeContents() {
    307         return 0;
    308     }
    309 
    310     @Override
    311     /*
    312      * generate hashcode based on significant fields
    313      */
    314     public int hashCode() {
    315         return ((null == mHost) ? 0 : mHost.hashCode())
    316         + ((null == mExclusionList) ? 0 : mExclusionList.hashCode())
    317         + mPort;
    318     }
    319 
    320     /**
    321      * Implement the Parcelable interface.
    322      * @hide
    323      */
    324     public void writeToParcel(Parcel dest, int flags) {
    325         if (!Uri.EMPTY.equals(mPacFileUrl)) {
    326             dest.writeByte((byte)1);
    327             mPacFileUrl.writeToParcel(dest, 0);
    328             dest.writeInt(mPort);
    329             return;
    330         } else {
    331             dest.writeByte((byte)0);
    332         }
    333         if (mHost != null) {
    334             dest.writeByte((byte)1);
    335             dest.writeString(mHost);
    336             dest.writeInt(mPort);
    337         } else {
    338             dest.writeByte((byte)0);
    339         }
    340         dest.writeString(mExclusionList);
    341         dest.writeStringArray(mParsedExclusionList);
    342     }
    343 
    344     public static final Creator<ProxyInfo> CREATOR =
    345         new Creator<ProxyInfo>() {
    346             public ProxyInfo createFromParcel(Parcel in) {
    347                 String host = null;
    348                 int port = 0;
    349                 if (in.readByte() != 0) {
    350                     Uri url = Uri.CREATOR.createFromParcel(in);
    351                     int localPort = in.readInt();
    352                     return new ProxyInfo(url, localPort);
    353                 }
    354                 if (in.readByte() != 0) {
    355                     host = in.readString();
    356                     port = in.readInt();
    357                 }
    358                 String exclList = in.readString();
    359                 String[] parsedExclList = in.readStringArray();
    360                 ProxyInfo proxyProperties =
    361                         new ProxyInfo(host, port, exclList, parsedExclList);
    362                 return proxyProperties;
    363             }
    364 
    365             public ProxyInfo[] newArray(int size) {
    366                 return new ProxyInfo[size];
    367             }
    368         };
    369 }
    370