ProxyInfo.java revision 823675fdbb7f974b8e2fa9fbb71774b32487582d
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
17package android.net;
18
19
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.text.TextUtils;
23
24import org.apache.http.client.HttpClient;
25
26import java.net.InetSocketAddress;
27import java.net.URLConnection;
28import java.util.List;
29import 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
46public 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        } else if (mHost != null) {
264            sb.append("[");
265            sb.append(mHost);
266            sb.append("] ");
267            sb.append(Integer.toString(mPort));
268            if (mExclusionList != null) {
269                    sb.append(" xl=").append(mExclusionList);
270            }
271        } else {
272            sb.append("[ProxyProperties.mHost == null]");
273        }
274        return sb.toString();
275    }
276
277    @Override
278    public boolean equals(Object o) {
279        if (!(o instanceof ProxyInfo)) return false;
280        ProxyInfo p = (ProxyInfo)o;
281        // If PAC URL is present in either then they must be equal.
282        // Other parameters will only be for fall back.
283        if (!Uri.EMPTY.equals(mPacFileUrl)) {
284            return mPacFileUrl.equals(p.getPacFileUrl()) && mPort == p.mPort;
285        }
286        if (!Uri.EMPTY.equals(p.mPacFileUrl)) {
287            return false;
288        }
289        if (mExclusionList != null && !mExclusionList.equals(p.getExclusionListAsString())) {
290            return false;
291        }
292        if (mHost != null && p.getHost() != null && mHost.equals(p.getHost()) == false) {
293            return false;
294        }
295        if (mHost != null && p.mHost == null) return false;
296        if (mHost == null && p.mHost != null) return false;
297        if (mPort != p.mPort) return false;
298        return true;
299    }
300
301    /**
302     * Implement the Parcelable interface
303     * @hide
304     */
305    public int describeContents() {
306        return 0;
307    }
308
309    @Override
310    /*
311     * generate hashcode based on significant fields
312     */
313    public int hashCode() {
314        return ((null == mHost) ? 0 : mHost.hashCode())
315        + ((null == mExclusionList) ? 0 : mExclusionList.hashCode())
316        + mPort;
317    }
318
319    /**
320     * Implement the Parcelable interface.
321     * @hide
322     */
323    public void writeToParcel(Parcel dest, int flags) {
324        if (!Uri.EMPTY.equals(mPacFileUrl)) {
325            dest.writeByte((byte)1);
326            mPacFileUrl.writeToParcel(dest, 0);
327            dest.writeInt(mPort);
328            return;
329        } else {
330            dest.writeByte((byte)0);
331        }
332        if (mHost != null) {
333            dest.writeByte((byte)1);
334            dest.writeString(mHost);
335            dest.writeInt(mPort);
336        } else {
337            dest.writeByte((byte)0);
338        }
339        dest.writeString(mExclusionList);
340        dest.writeStringArray(mParsedExclusionList);
341    }
342
343    public static final Creator<ProxyInfo> CREATOR =
344        new Creator<ProxyInfo>() {
345            public ProxyInfo createFromParcel(Parcel in) {
346                String host = null;
347                int port = 0;
348                if (in.readByte() != 0) {
349                    Uri url = Uri.CREATOR.createFromParcel(in);
350                    int localPort = in.readInt();
351                    return new ProxyInfo(url, localPort);
352                }
353                if (in.readByte() != 0) {
354                    host = in.readString();
355                    port = in.readInt();
356                }
357                String exclList = in.readString();
358                String[] parsedExclList = in.readStringArray();
359                ProxyInfo proxyProperties =
360                        new ProxyInfo(host, port, exclList, parsedExclList);
361                return proxyProperties;
362            }
363
364            public ProxyInfo[] newArray(int size) {
365                return new ProxyInfo[size];
366            }
367        };
368}
369