ProxyProperties.java revision 434203a277cd2f237a71508a3d5a7d1602126cd5
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 /** 167 * Implement the Parcelable interface. 168 * @hide 169 */ 170 public void writeToParcel(Parcel dest, int flags) { 171 if (mHost != null) { 172 dest.writeByte((byte)1); 173 dest.writeString(mHost); 174 dest.writeInt(mPort); 175 } else { 176 dest.writeByte((byte)0); 177 } 178 dest.writeString(mExclusionList); 179 dest.writeStringArray(mParsedExclusionList); 180 } 181 182 /** 183 * Implement the Parcelable interface. 184 * @hide 185 */ 186 public static final Creator<ProxyProperties> CREATOR = 187 new Creator<ProxyProperties>() { 188 public ProxyProperties createFromParcel(Parcel in) { 189 String host = null; 190 int port = 0; 191 if (in.readByte() == 1) { 192 host = in.readString(); 193 port = in.readInt(); 194 } 195 String exclList = in.readString(); 196 String[] parsedExclList = in.readStringArray(); 197 ProxyProperties proxyProperties = 198 new ProxyProperties(host, port, exclList, parsedExclList); 199 return proxyProperties; 200 } 201 202 public ProxyProperties[] newArray(int size) { 203 return new ProxyProperties[size]; 204 } 205 }; 206} 207