Example usage for java.net Proxy address

List of usage examples for java.net Proxy address

Introduction

In this page you can find the example usage for java.net Proxy address.

Prototype

public SocketAddress address() 

Source Link

Document

Returns the socket address of the proxy, or null if its a direct connection.

Usage

From source file:org.eclipse.mylyn.commons.net.WebUtil.java

private static void configureHttpClientProxy(HttpClient client, HostConfiguration hostConfiguration,
        AbstractWebLocation location) {//from   w  w  w .j  a v a2s  .  c  o m
    String host = WebUtil.getHost(location.getUrl());

    Proxy proxy;
    if (WebUtil.isRepositoryHttps(location.getUrl())) {
        proxy = location.getProxyForHost(host, IProxyData.HTTPS_PROXY_TYPE);
    } else {
        proxy = location.getProxyForHost(host, IProxyData.HTTP_PROXY_TYPE);
    }

    if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
        InetSocketAddress address = (InetSocketAddress) proxy.address();
        hostConfiguration.setProxy(address.getHostName(), address.getPort());
        if (proxy instanceof AuthenticatedProxy) {
            AuthenticatedProxy authProxy = (AuthenticatedProxy) proxy;
            Credentials credentials = getCredentials(authProxy.getUserName(), authProxy.getPassword(),
                    address.getAddress());
            AuthScope proxyAuthScope = new AuthScope(address.getHostName(), address.getPort(),
                    AuthScope.ANY_REALM);
            client.getState().setProxyCredentials(proxyAuthScope, credentials);
        }
    } else {
        hostConfiguration.setProxyHost(null);
    }
}

From source file:org.ulteo.utils.ProxyManager.java

public void detect(String remoteHost) {
    Logger.debug("Detect proxy");
    List<Proxy> proxyList = null;
    System.setProperty("java.net.useSystemProxies", "true");
    ProxySelector ps = ProxySelector.getDefault();

    if (ps == null) {
        Logger.warn("Unable to detect a proxy: no proxySelector available");
        return;/*w w  w .  ja  va  2s  .  c  o  m*/
    }
    try {
        URI uri = new URI("https://" + remoteHost);
        proxyList = ps.select(uri);
    } catch (URISyntaxException e) {
        Logger.warn("Unable to detect a proxy: bad URL");
        return;
    }
    if (proxyList != null) {
        for (Proxy proxy : proxyList) {
            InetSocketAddress addr = (InetSocketAddress) proxy.address();
            if (addr != null) {
                this.host = addr.getHostName();
                this.port = addr.getPort();
                System.setProperty("https.proxyHost", this.host);
                System.setProperty("https.proxyPort", new Integer(this.port).toString());
                Logger.debug("using proxy[" + this.host + ":" + this.port + "]");
                break;
            }
        }
    }
}

From source file:com.groupon.jenkins.util.HttpPoster.java

private HttpHost getProxy(HttpUriRequest method) throws URIException {

    ProxyConfiguration proxy = Jenkins.getInstance().proxy;
    if (proxy == null)
        return null;

    Proxy p = proxy.createProxy(method.getURI().getHost());
    switch (p.type()) {
    case DIRECT:// w ww  . j a  v a2  s . com
        return null;
    case HTTP:
        InetSocketAddress sa = (InetSocketAddress) p.address();
        return new HttpHost(sa.getHostName(), sa.getPort());
    case SOCKS:
    default:
        return null;
    }
}

From source file:org.sonar.ide.intellij.wsclient.WSClientFactory.java

/**
 * Creates new Sonar web service client, which uses proxy settings from IntelliJ.
 *//*www.  j  a v a 2  s.com*/
private SonarClient createSonarClient(Host host) {
    SonarClient.Builder builder = SonarClient.builder().url(host.getHost()).login(host.getUsername())
            .password(host.getPassword());
    Proxy proxy = getIntelliJProxyFor(host);
    if (proxy != null) {
        InetSocketAddress address = (InetSocketAddress) proxy.address();
        HttpConfigurable proxySettings = HttpConfigurable.getInstance();
        builder.proxy(address.getHostName(), address.getPort());
        if (proxySettings.PROXY_AUTHENTICATION) {
            builder.proxyLogin(proxySettings.PROXY_LOGIN).proxyPassword(proxySettings.getPlainProxyPassword());
        }
    }
    return builder.build();
}

From source file:net.sf.zekr.engine.network.NetworkController.java

public Proxy getProxy(String uri) throws URISyntaxException {
    Proxy proxy;
    if (SYSTEM_PROXY.equalsIgnoreCase(defaultProxy)) {
        List<Proxy> proxyList = proxySelector.select(new URI(uri));
        proxy = (Proxy) proxyList.get(0);
        if (proxy.address() == null) {
            proxy = Proxy.NO_PROXY;
        }// w w  w  . ja v a2  s .  c o m
    } else if (MANUAL_PROXY.equalsIgnoreCase(defaultProxy)) {
        SocketAddress sa = InetSocketAddress.createUnresolved(proxyServer, proxyPort);
        Proxy.Type type = Proxy.Type.HTTP.name().equalsIgnoreCase(proxyType) ? Proxy.Type.HTTP
                : Proxy.Type.SOCKS.name().equalsIgnoreCase(proxyType) ? Proxy.Type.SOCKS : Proxy.Type.DIRECT;
        proxy = new Proxy(type, sa);
    } else {
        proxy = Proxy.NO_PROXY;
    }
    return proxy;
}

From source file:com.marvelution.hudson.plugins.apiv2.client.connectors.HttpClient3Connector.java

/**
 * Create the {@link HttpClient} object//from  w  w  w . j a v  a2s . c  om
 */
private void createClient() {
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setConnectionTimeout(TIMEOUT_MS);
    params.setDefaultMaxConnectionsPerHost(MAX_HOST_CONNECTIONS);
    params.setMaxTotalConnections(MAX_TOTAL_CONNECTIONS);
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    configureCredentials();
    if (StringUtils.isNotBlank(System.getProperty("http.proxyHost"))) {
        log.debug("A HTTP Proxy is configured");
        System.setProperty("java.net.useSystemProxies", "true");
        Proxy proxy = chooseProxy();
        if (proxy.type() == Type.HTTP && proxy.address() instanceof InetSocketAddress) {
            // convert the socket address to an ProxyHost
            final InetSocketAddress isa = (InetSocketAddress) proxy.address();
            // assume default scheme (http)
            ProxyHost proxyHost = new ProxyHost(getHost(isa), isa.getPort());
            httpClient.getHostConfiguration().setProxyHost(proxyHost);
            if (StringUtils.isNotBlank(System.getProperty("http.proxyUser"))) {
                String user = System.getProperty("http.proxyUser");
                String password = System.getProperty("http.proxyPassword");
                httpClient.getState().setProxyCredentials(new AuthScope(getHost(isa), isa.getPort()),
                        new UsernamePasswordCredentials(user, password));
            }
        }
    }
}

From source file:io.github.bonigarcia.wdm.WdmHttpClient.java

private final HttpHost createProxyHttpHost(String proxyUrl) {
    Proxy proxy = createProxy(proxyUrl);
    if (proxy == null || proxy.address() == null) {
        return null;
    }//from  ww w .j av  a  2 s. c o  m
    if (!(proxy.address() instanceof InetSocketAddress)) {
        throw new RuntimeException(
                "Detect an unsupported subclass of SocketAddress. Please use the InetSocketAddress or subclass. Actual:"
                        + proxy.address().getClass());
    }
    InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
    return new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort());
}

From source file:org.sonar.ide.intellij.wsclient.WSClientFactory.java

/**
 * Workaround for http://jira.codehaus.org/browse/SONAR-1586
 *///from  w  ww  .  jav a2  s . c  o  m
private void configureProxy(DefaultHttpClient httpClient, Host server) {
    try {
        Proxy proxyData = getIntelliJProxyFor(server);
        if (proxyData != null) {
            InetSocketAddress address = (InetSocketAddress) proxyData.address();
            HttpConfigurable proxySettings = HttpConfigurable.getInstance();
            LOG.debug("Proxy for [" + address.getHostName() + "] - [" + address + "]");
            HttpHost proxy = new HttpHost(address.getHostName(), address.getPort());
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            if (proxySettings.PROXY_AUTHENTICATION) {
                httpClient.getCredentialsProvider().setCredentials(
                        new AuthScope(address.getHostName(), address.getPort()),
                        new UsernamePasswordCredentials(proxySettings.PROXY_LOGIN,
                                proxySettings.getPlainProxyPassword()));
            }
        } else {
            LOG.debug("No proxy for [" + server.getHost() + "]");
        }
    } catch (Exception e) {
        LOG.error("Unable to configure proxy for sonar-ws-client", e);
    }
}

From source file:com.googlecode.fascinator.common.BasicHttpClient.java

/**
 * Gets an HTTP client. If authentication is required, the authenticate()
 * method must be called prior to this method.
 * /*from w  w  w  .j a  v a2 s  .  c  o  m*/
 * @param auth set true to use authentication, false to skip authentication
 * @return an HTTP client
 */
public HttpClient getHttpClient(boolean auth) {
    HttpClient client = new HttpClient();
    try {
        URL url = new URL(baseUrl);
        Proxy proxy = ProxySelector.getDefault().select(url.toURI()).get(0);
        if (!proxy.type().equals(Proxy.Type.DIRECT)) {
            InetSocketAddress address = (InetSocketAddress) proxy.address();
            String proxyHost = address.getHostName();
            int proxyPort = address.getPort();
            client.getHostConfiguration().setProxy(proxyHost, proxyPort);
            log.trace("Using proxy {}:{}", proxyHost, proxyPort);
        }
    } catch (Exception e) {
        log.warn("Failed to get proxy settings: " + e.getMessage());
    }
    if (auth && credentials != null) {
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(AuthScope.ANY, credentials);
        log.trace("Credentials: username={}", credentials.getUserName());
    }
    return client;
}

From source file:au.com.redboxresearchdata.harvester.httpclient.BasicHttpClient.java

/**
 * Gets an HTTP client. If authentication is required, the authenticate()
 * method must be called prior to this method.
 * //from ww w  .  ja va 2s  . c  om
 * @param auth set true to use authentication, false to skip authentication
 * @return an HTTP client
 */
public HttpClient getHttpClient(boolean auth) {
    HttpClient client = new HttpClient();
    try {
        URL url = new URL(baseUrl);
        //        log.info(baseUrl + "----------------------------1111------------");
        Proxy proxy = ProxySelector.getDefault().select(url.toURI()).get(0);
        if (!proxy.type().equals(Proxy.Type.DIRECT)) {
            InetSocketAddress address = (InetSocketAddress) proxy.address();
            String proxyHost = address.getHostName();
            int proxyPort = address.getPort();
            client.getHostConfiguration().setProxy(proxyHost, proxyPort);
            //          log.trace("Using proxy {}:{}", proxyHost, proxyPort);
        }
    } catch (Exception e) {
        //    log.warn("Failed to get proxy settings: " + e.getMessage());
    }
    if (auth && credentials != null) {
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(AuthScope.ANY, credentials);
        //  log.trace("Credentials: username={}", credentials.getUserName());
    }
    return client;
}