1 30 package org.apache.commons.httpclient.contrib.proxy; 31 32 import java.lang.reflect.Array ; 33 import java.lang.reflect.Method ; 34 import java.net.URL ; 35 import java.util.Properties ; 36 37 import org.apache.commons.httpclient.ProxyHost; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 64 public class PluginProxyUtil { 65 66 67 private static final Log LOG = LogFactory.getLog(PluginProxyUtil.class); 68 69 73 private static final ProxyHost NO_PROXY_HOST = new ProxyHost("",80); 74 75 78 private static final String PLUGIN_PROXY_CONFIG_PROP = 79 "javaplugin.proxy.config.list"; 80 81 90 public static ProxyHost detectProxy(URL sampleURL) 91 throws ProxyDetectionException 92 { 93 94 ProxyHost result = null; 95 String javaVers = System.getProperty("java.runtime.version"); 96 97 if (LOG.isDebugEnabled()) { 98 LOG.debug("About to attempt auto proxy detection under Java " + 99 "version:"+javaVers); 100 } 101 102 boolean invokeFailover = false; 105 106 if (javaVers.startsWith("1.3")) { 107 result = detectProxySettingsJDK13(sampleURL); 108 if (result == null) { 109 invokeFailover = true; 110 } 111 } else if (javaVers.startsWith("1.4") || (javaVers.startsWith("1.5") || javaVers.startsWith("1.6"))) { 112 result = detectProxySettingsJDK14_JDK15_JDK16(sampleURL); 113 if (result == null) { 114 invokeFailover = true; 115 } 116 } else { 117 if (LOG.isDebugEnabled()) { 118 LOG.debug("Sun Plugin reported java version not 1.3.X, " + 119 "1.4.X, 1.5.X or 1.6.X - trying failover detection..."); 120 } 121 invokeFailover = true; 122 } 123 if (invokeFailover) { 124 if (LOG.isDebugEnabled()) { 125 LOG.debug("Using failover proxy detection..."); 126 } 127 result = getPluginProxyConfigSettings(); 128 } 129 if (NO_PROXY_HOST.equals(result)) { 130 result = null; 131 } 132 return result; 133 } 134 135 144 private static ProxyHost detectProxySettingsJDK13(URL sampleURL) 145 throws ProxyDetectionException 146 { 147 ProxyHost result = null; 148 try { 149 Class pluginProxyHandler = 152 Class.forName("sun.plugin.protocol.PluginProxyHandler"); 153 Method getDefaultProxyHandlerMethod = 154 pluginProxyHandler.getDeclaredMethod("getDefaultProxyHandler", 155 null); 156 Object proxyHandlerObj = 157 getDefaultProxyHandlerMethod.invoke(null, null); 158 if (proxyHandlerObj != null) { 159 Class proxyHandlerClass = proxyHandlerObj.getClass(); 160 Method getProxyInfoMethod = 161 proxyHandlerClass.getDeclaredMethod("getProxyInfo", 162 new Class []{URL .class}); 163 Object proxyInfoObject = 164 getProxyInfoMethod.invoke(proxyHandlerObj, 165 new Object [] { sampleURL }); 166 if (proxyInfoObject != null) { 167 Class proxyInfoClass = proxyInfoObject.getClass(); 168 Method getProxyMethod = 169 proxyInfoClass.getDeclaredMethod("getProxy", null); 170 boolean useProxy = 171 (getProxyMethod.invoke(proxyInfoObject, null) != null); 172 if (useProxy) { 173 String proxyIP = 174 (String )getProxyMethod.invoke(proxyInfoObject, null); 175 Method getProxyPortMethod = 176 proxyInfoClass.getDeclaredMethod("getPort", null); 177 Integer portInteger = 178 (Integer )getProxyPortMethod.invoke(proxyInfoObject, null); 179 int proxyPort = portInteger.intValue(); 180 if (LOG.isDebugEnabled()) { 181 LOG.debug("1.3.X: proxy=" + proxyIP+ 182 " port=" + proxyPort); 183 } 184 result = new ProxyHost(proxyIP, proxyPort); 185 } else { 186 if (LOG.isDebugEnabled()) { 187 LOG.debug("1.3.X reported NULL for " + 188 "proxyInfo.getProxy (no proxy assumed)"); 189 } 190 result = NO_PROXY_HOST; 191 } 192 } else { 193 if (LOG.isDebugEnabled()) { 194 LOG.debug("NULL proxyInfo in 1.3.X auto proxy " + 195 "detection, (no proxy assumed)"); 196 } 197 result = NO_PROXY_HOST; 198 } 199 } else { 200 throw new ProxyDetectionException( 201 "Sun Plugin 1.3.X failed to provide a default proxy handler"); 202 } 203 } catch (Exception e) { 204 LOG.warn("Sun Plugin 1.3.X proxy detection class not " + 205 "found, will try failover detection, e:"+e); 206 } 207 return result; 208 } 209 210 222 private static ProxyHost detectProxySettingsJDK14_JDK15_JDK16(URL sampleURL) { 223 ProxyHost result = null; 224 try { 225 Class ProxyServiceClass = 228 Class.forName("com.sun.java.browser.net.ProxyService"); 229 Method getProxyInfoMethod = 230 ProxyServiceClass.getDeclaredMethod("getProxyInfo", 231 new Class [] {URL .class}); 232 Object proxyInfoArrayObj = 233 getProxyInfoMethod.invoke(null, new Object [] {sampleURL}); 234 235 if (proxyInfoArrayObj == null 236 || Array.getLength(proxyInfoArrayObj) == 0) { 237 if (LOG.isDebugEnabled()) { 238 LOG.debug("1.4.X reported NULL proxy (no proxy assumed)"); 239 } 240 result = NO_PROXY_HOST; 241 } else { 242 Object proxyInfoObject = Array.get(proxyInfoArrayObj, 0); 243 Class proxyInfoClass = proxyInfoObject.getClass(); 244 Method getHostMethod = 245 proxyInfoClass.getDeclaredMethod("getHost",null); 246 String proxyIP = 247 (String )getHostMethod.invoke(proxyInfoObject, null); 248 Method getPortMethod = 249 proxyInfoClass.getDeclaredMethod("getPort",null); 250 Integer portInteger = 251 (Integer )getPortMethod.invoke(proxyInfoObject, null); 252 int proxyPort = portInteger.intValue(); 253 if (LOG.isDebugEnabled()) { 254 LOG.debug("1.4.X Proxy info geProxy:"+proxyIP+ 255 " get Port:"+proxyPort); 256 } 257 result = new ProxyHost(proxyIP, proxyPort); 258 } 259 } catch (Exception e) { 260 e.printStackTrace(); 261 LOG.warn("Sun Plugin 1.4.X proxy detection class not found, " + 262 "will try failover detection, e:"+e); 263 } 264 return result; 265 } 266 267 276 private static ProxyHost getPluginProxyConfigSettings() 277 throws ProxyDetectionException 278 { 279 ProxyHost result = null; 280 try { 281 Properties properties = System.getProperties(); 282 String proxyList = 283 properties.getProperty("javaplugin.proxy.config.list"); 284 if (LOG.isDebugEnabled()) { 285 LOG.debug("Plugin Proxy Config List Property:"+proxyList); 286 } 287 boolean useProxy = (proxyList != null); 288 if (useProxy) { 289 proxyList = proxyList.toUpperCase(); 290 String proxyIP=""; 297 if (proxyList.indexOf("HTTP=") > -1) { 298 proxyIP = 299 proxyList.substring(proxyList.indexOf("HTTP=")+5, 300 proxyList.indexOf(":")); 301 } else { 302 proxyIP = proxyList.substring(0, proxyList.indexOf(":")); 303 } 304 int endOfPort = proxyList.indexOf(","); 305 if (endOfPort < 1) endOfPort = proxyList.length(); 306 String portString = 307 proxyList.substring(proxyList.indexOf(":")+1,endOfPort); 308 int proxyPort = Integer.parseInt(portString); 309 if (LOG.isDebugEnabled()) { 310 LOG.debug("proxy " + proxyIP+" port " + proxyPort); 311 } 312 result = new ProxyHost(proxyIP, proxyPort); 313 } else { 314 LOG.debug("No configured plugin proxy list"); 315 result = NO_PROXY_HOST; 316 } 317 } catch (Exception e) { 318 if (LOG.isDebugEnabled()) { 319 LOG.debug("Exception during failover auto proxy detection, " + 320 ", e:"+e); 321 throw new ProxyDetectionException( 322 "Encountered unexpected exception while attempting " + 323 "to parse proxy information stored in "+ 324 PLUGIN_PROXY_CONFIG_PROP, e); 325 } 326 } 327 return result; 328 } 329 } 330 | Popular Tags |