Example usage for org.apache.commons.httpclient.contrib.proxy ProxyDetectionException ProxyDetectionException

List of usage examples for org.apache.commons.httpclient.contrib.proxy ProxyDetectionException ProxyDetectionException

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.contrib.proxy ProxyDetectionException ProxyDetectionException.

Prototype

public ProxyDetectionException(String message) 

Source Link

Document

Creates a new ProxyDetectionException with the specified detail message.

Usage

From source file:org.apache.commons.httpclient.contrib.proxy.PluginProxyUtil.java

/**
 * Use Sun-specific internal plugin proxy classes for 1.3.X
 * Look around for the 1.3.X plugin proxy detection class. Without it,
 * cannot autodetect...//from   ww  w . j a v  a 2s .co  m
 *
 * @param sampleURL the URL to check proxy settings for
 * @return ProxyHost the host and port of the proxy that should be used
 * @throws ProxyDetectionException if detection failed
 */
private static HttpHost detectProxySettingsJDK13(URL sampleURL) throws ProxyDetectionException {
    HttpHost result = null;
    try {
        // Attempt to discover proxy info by asking internal plugin
        // code to locate proxy path to server sampleURL...
        Class<?> pluginProxyHandler = Class.forName("sun.plugin.protocol.PluginProxyHandler");
        Method getDefaultProxyHandlerMethod = pluginProxyHandler.getDeclaredMethod("getDefaultProxyHandler",
                (Class[]) null);
        Object proxyHandlerObj = getDefaultProxyHandlerMethod.invoke(null, (Object[]) null);
        if (proxyHandlerObj != null) {
            Class<?> proxyHandlerClass = proxyHandlerObj.getClass();
            Method getProxyInfoMethod = proxyHandlerClass.getDeclaredMethod("getProxyInfo",
                    new Class[] { URL.class });
            Object proxyInfoObject = getProxyInfoMethod.invoke(proxyHandlerObj, new Object[] { sampleURL });
            if (proxyInfoObject != null) {
                Class<?> proxyInfoClass = proxyInfoObject.getClass();
                Method getProxyMethod = proxyInfoClass.getDeclaredMethod("getProxy", (Class[]) null);
                boolean useProxy = (getProxyMethod.invoke(proxyInfoObject, (Object[]) null) != null);
                if (useProxy) {
                    String proxyIP = (String) getProxyMethod.invoke(proxyInfoObject, (Object[]) null);
                    Method getProxyPortMethod = proxyInfoClass.getDeclaredMethod("getPort", (Class[]) null);
                    Integer portInteger = (Integer) getProxyPortMethod.invoke(proxyInfoObject, (Object[]) null);
                    int proxyPort = portInteger.intValue();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("1.3.X: proxy=" + proxyIP + " port=" + proxyPort);
                    }
                    result = new HttpHost(proxyIP, proxyPort);
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("1.3.X reported NULL for " + "proxyInfo.getProxy (no proxy assumed)");
                    }
                    result = NO_PROXY_HOST;
                }
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("NULL proxyInfo in 1.3.X auto proxy " + "detection, (no proxy assumed)");
                }
                result = NO_PROXY_HOST;
            }
        } else {
            throw new ProxyDetectionException("Sun Plugin 1.3.X failed to provide a default proxy handler");
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        LOG.debug("Sun Plugin 1.3.X proxy detection class not " + "found, will try failover detection" /*, e*/);
    }
    return result;
}