Example usage for java.net Authenticator requestPasswordAuthentication

List of usage examples for java.net Authenticator requestPasswordAuthentication

Introduction

In this page you can find the example usage for java.net Authenticator requestPasswordAuthentication.

Prototype

public static PasswordAuthentication requestPasswordAuthentication(String host, InetAddress addr, int port,
        String protocol, String prompt, String scheme) 

Source Link

Document

Ask the authenticator that has been registered with the system for a password.

Usage

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

private IProxySelectorProvider addProxy(final DefaultHttpClient httpClient, URI requestURI) {
    IProxySelectorProvider proxySelectorProvider = null;
    try {/*from  www.  ja va 2s  . com*/
        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);
        }
    }
}