Example usage for java.net PasswordAuthentication getPassword

List of usage examples for java.net PasswordAuthentication getPassword

Introduction

In this page you can find the example usage for java.net PasswordAuthentication getPassword.

Prototype

public char[] getPassword() 

Source Link

Document

Returns the user password.

Usage

From source file:org.talend.core.nexus.HttpClientTransport.java

private IProxySelectorProvider addProxy(final DefaultHttpClient httpClient, URI requestURI) {
    IProxySelectorProvider proxySelectorProvider = null;
    try {/*w ww .ja  v a2s .c  o  m*/
        if (Boolean.valueOf(
                System.getProperty(PROP_PROXY_HTTP_CLIENT_USE_DEFAULT_SETTINGS, Boolean.FALSE.toString()))) {
            return proxySelectorProvider;
        }
        final List<Proxy> proxyList = TalendProxySelector.getInstance().getDefaultProxySelector()
                .select(requestURI);
        Proxy usedProxy = null;
        if (proxyList != null && !proxyList.isEmpty()) {
            usedProxy = proxyList.get(0);
        }

        if (usedProxy != null) {
            if (Type.DIRECT.equals(usedProxy.type())) {
                return proxySelectorProvider;
            }
            final Proxy finalProxy = usedProxy;
            InetSocketAddress address = (InetSocketAddress) finalProxy.address();
            String proxyServer = address.getHostName();
            int proxyPort = address.getPort();
            PasswordAuthentication proxyAuthentication = Authenticator.requestPasswordAuthentication(
                    proxyServer, address.getAddress(), proxyPort, "Http Proxy", "Http proxy authentication",
                    null);
            if (proxyAuthentication != null) {
                String proxyUser = proxyAuthentication.getUserName();
                if (StringUtils.isNotBlank(proxyUser)) {
                    String proxyPassword = "";
                    char[] passwordChars = proxyAuthentication.getPassword();
                    if (passwordChars != null) {
                        proxyPassword = new String(passwordChars);
                    }
                    httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyServer, proxyPort),
                            new UsernamePasswordCredentials(proxyUser, proxyPassword));
                }
            }
            HttpHost proxyHost = new HttpHost(proxyServer, proxyPort);
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost);
            proxySelectorProvider = createProxySelectorProvider();
        }
        return proxySelectorProvider;
    } finally {
        if (proxySelectorProvider != null) {
            TalendProxySelector.getInstance().addProxySelectorProvider(proxySelectorProvider);
        }
    }
}