ProxyProperties.java revision 4e900091c4da26eb1c9f0d232ee0e50f4522cc69
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; 23import android.util.Log; 24 25import java.net.InetAddress; 26import java.net.InetSocketAddress; 27import java.net.UnknownHostException; 28 29/** 30 * A container class for the http proxy info 31 * @hide 32 */ 33public class ProxyProperties implements Parcelable { 34 35 private String mHost; 36 private int mPort; 37 private String mExclusionList; 38 private String[] mParsedExclusionList; 39 40 public ProxyProperties(String host, int port, String exclList) { 41 mHost = host; 42 mPort = port; 43 setExclusionList(exclList); 44 } 45 46 private ProxyProperties(String host, int port, String exclList, String[] parsedExclList) { 47 mHost = host; 48 mPort = port; 49 mExclusionList = exclList; 50 mParsedExclusionList = parsedExclList; 51 } 52 53 // copy constructor instead of clone 54 public ProxyProperties(ProxyProperties source) { 55 if (source != null) { 56 mHost = source.getHost(); 57 mPort = source.getPort(); 58 mExclusionList = source.getExclusionList(); 59 mParsedExclusionList = source.mParsedExclusionList; 60 } 61 } 62 63 public InetSocketAddress getSocketAddress() { 64 InetSocketAddress inetSocketAddress = null; 65 try { 66 inetSocketAddress = new InetSocketAddress(mHost, mPort); 67 } catch (IllegalArgumentException e) { } 68 return inetSocketAddress; 69 } 70 71 public String getHost() { 72 return mHost; 73 } 74 75 public int getPort() { 76 return mPort; 77 } 78 79 // comma separated 80 public String getExclusionList() { 81 return mExclusionList; 82 } 83 84 // comma separated 85 private void setExclusionList(String exclusionList) { 86 mExclusionList = exclusionList; 87 if (mExclusionList == null) { 88 mParsedExclusionList = new String[0]; 89 } else { 90 String splitExclusionList[] = exclusionList.toLowerCase().split(","); 91 mParsedExclusionList = new String[splitExclusionList.length * 2]; 92 for (int i = 0; i < splitExclusionList.length; i++) { 93 String s = splitExclusionList[i].trim(); 94 if (s.startsWith(".")) s = s.substring(1); 95 mParsedExclusionList[i*2] = s; 96 mParsedExclusionList[(i*2)+1] = "." + s; 97 } 98 } 99 } 100 101 public boolean isExcluded(String url) { 102 if (TextUtils.isEmpty(url) || mParsedExclusionList == null || 103 mParsedExclusionList.length == 0) return false; 104 105 Uri u = Uri.parse(url); 106 String urlDomain = u.getHost(); 107 if (urlDomain == null) return false; 108 for (int i = 0; i< mParsedExclusionList.length; i+=2) { 109 if (urlDomain.equals(mParsedExclusionList[i]) || 110 urlDomain.endsWith(mParsedExclusionList[i+1])) { 111 return true; 112 } 113 } 114 return false; 115 } 116 117 public java.net.Proxy makeProxy() { 118 java.net.Proxy proxy = java.net.Proxy.NO_PROXY; 119 if (mHost != null) { 120 try { 121 InetSocketAddress inetSocketAddress = new InetSocketAddress(mHost, mPort); 122 proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, inetSocketAddress); 123 } catch (IllegalArgumentException e) { 124 } 125 } 126 return proxy; 127 } 128 129 @Override 130 public String toString() { 131 StringBuilder sb = new StringBuilder(); 132 if (mHost != null) { 133 sb.append("["); 134 sb.append(mHost); 135 sb.append("] "); 136 sb.append(Integer.toString(mPort)); 137 if (mExclusionList != null) { 138 sb.append(" xl=").append(mExclusionList); 139 } 140 } 141 return sb.toString(); 142 } 143 144 @Override 145 public boolean equals(Object o) { 146 if (!(o instanceof ProxyProperties)) return false; 147 ProxyProperties p = (ProxyProperties)o; 148 if (mExclusionList != null && !mExclusionList.equals(p.getExclusionList())) return false; 149 if (mHost != null && p.getHost() != null && mHost.equals(p.getHost()) == false) { 150 return false; 151 } 152 if (mHost != null && p.mHost == null) return false; 153 if (mHost == null && p.mHost != null) return false; 154 if (mPort != p.mPort) return false; 155 return true; 156 } 157 158 /** 159 * Implement the Parcelable interface 160 * @hide 161 */ 162 public int describeContents() { 163 return 0; 164 } 165 166 @Override 167 /* 168 * generate hashcode based on significant fields 169 */ 170 public int hashCode() { 171 return ((null == mHost) ? 0 : mHost.hashCode()) 172 + ((null == mExclusionList) ? 0 : mExclusionList.hashCode()) 173 + mPort; 174 } 175 176 /** 177 * Implement the Parcelable interface. 178 * @hide 179 */ 180 public void writeToParcel(Parcel dest, int flags) { 181 if (mHost != null) { 182 dest.writeByte((byte)1); 183 dest.writeString(mHost); 184 dest.writeInt(mPort); 185 } else { 186 dest.writeByte((byte)0); 187 } 188 dest.writeString(mExclusionList); 189 dest.writeStringArray(mParsedExclusionList); 190 } 191 192 /** 193 * Implement the Parcelable interface. 194 * @hide 195 */ 196 public static final Creator<ProxyProperties> CREATOR = 197 new Creator<ProxyProperties>() { 198 public ProxyProperties createFromParcel(Parcel in) { 199 String host = null; 200 int port = 0; 201 if (in.readByte() == 1) { 202 host = in.readString(); 203 port = in.readInt(); 204 } 205 String exclList = in.readString(); 206 String[] parsedExclList = in.readStringArray(); 207 ProxyProperties proxyProperties = 208 new ProxyProperties(host, port, exclList, parsedExclList); 209 return proxyProperties; 210 } 211 212 public ProxyProperties[] newArray(int size) { 213 return new ProxyProperties[size]; 214 } 215 }; 216} 217