Example usage for org.apache.http.auth AuthScope getHost

List of usage examples for org.apache.http.auth AuthScope getHost

Introduction

In this page you can find the example usage for org.apache.http.auth AuthScope getHost.

Prototype

public String getHost() 

Source Link

Usage

From source file:org.jets3t.apps.uploader.Uploader.java

/**
 * Implementation method for the CredentialsProvider interface.
 * <p>/*from w ww .j  a va  2 s .  c o  m*/
 * Based on sample code:
 * <a href="http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/examples/InteractiveAuthenticationExample.java?view=markup">InteractiveAuthenticationExample</a>
 *
 */
public Credentials getCredentials(AuthScope scope) {
    if (scope == null || scope.getScheme() == null) {
        return null;
    }
    Credentials credentials = mCredentialProvider.getCredentials(scope);
    if (credentials != null) {
        return credentials;
    }

    try {
        if (scope.getScheme().equals("ntlm")) {
            //if (authscheme instanceof NTLMScheme) {
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html>Host <b>" + scope.getHost() + ":" + scope.getPort()
                            + "</b> requires Windows authentication</html>",
                    true);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new NTCredentials(pwDialog.getUser(), pwDialog.getPassword(), scope.getHost(),
                        pwDialog.getDomain());
            }
            pwDialog.dispose();
        } else if (scope.getScheme().equals("basic") || scope.getScheme().equals("digest")) {
            //if (authscheme instanceof RFC2617Scheme) {
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html><center>Host <b>" + scope.getHost() + ":" + scope.getPort() + "</b>"
                            + " requires authentication for the realm:<br><b>" + scope.getRealm()
                            + "</b></center></html>",
                    false);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new UsernamePasswordCredentials(pwDialog.getUser(), pwDialog.getPassword());
            }
            pwDialog.dispose();
        } else {
            throw new IllegalArgumentException("Unsupported authentication scheme: " + scope.getScheme());
        }
        if (credentials != null) {
            mCredentialProvider.setCredentials(scope, credentials);
        }
        return credentials;
    } catch (Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}

From source file:org.jets3t.apps.cockpit.Cockpit.java

/**
 * Implementation method for the CredentialsProvider interface.
 * <p>/*from w  w  w . j  av  a  2s.  c o  m*/
 * Based on sample code:
 * <a href="http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/examples/InteractiveAuthenticationExample.java?view=markup">InteractiveAuthenticationExample</a>
 *
 */
public Credentials getCredentials(AuthScope scope) {
    if (scope == null || scope.getScheme() == null) {
        return null;
    }
    Credentials credentials = mCredentialProvider.getCredentials(scope);
    if (credentials != null) {
        return credentials;
    }
    try {
        if (scope.getScheme().equals("ntlm")) {
            //if (authscheme instanceof NTLMScheme) {
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html>Host <b>" + scope.getHost() + ":" + scope.getPort()
                            + "</b> requires Windows authentication</html>",
                    true);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new NTCredentials(pwDialog.getUser(), pwDialog.getPassword(), scope.getHost(),
                        pwDialog.getDomain());
            }
            pwDialog.dispose();
        } else if (scope.getScheme().equals("basic") || scope.getScheme().equals("digest")) {
            //if (authscheme instanceof RFC2617Scheme) {
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html><center>Host <b>" + scope.getHost() + ":" + scope.getPort() + "</b>"
                            + " requires authentication for the realm:<br><b>" + scope.getRealm()
                            + "</b></center></html>",
                    false);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new UsernamePasswordCredentials(pwDialog.getUser(), pwDialog.getPassword());
            }
            pwDialog.dispose();
        } else {
            throw new IllegalArgumentException("Unsupported authentication scheme: " + scope.getScheme());
        }
        if (credentials != null) {
            mCredentialProvider.setCredentials(scope, credentials);
        }
        return credentials;
    } catch (Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}

From source file:org.codelibs.robot.client.http.HcHttpClient.java

public synchronized void init() {
    if (httpClient != null) {
        return;//from  w  ww  .j  a  v  a 2  s.  c  om
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Initializing " + HcHttpClient.class.getName());
    }

    // access timeout
    final Integer accessTimeoutParam = getInitParameter(ACCESS_TIMEOUT_PROPERTY, accessTimeout);
    if (accessTimeoutParam != null) {
        accessTimeout = accessTimeoutParam;
    }

    // robots.txt parser
    final Boolean robotsTxtEnabled = getInitParameter(ROBOTS_TXT_ENABLED_PROPERTY, Boolean.TRUE);
    if (robotsTxtHelper != null) {
        robotsTxtHelper.setEnabled(robotsTxtEnabled.booleanValue());
    }

    // httpclient
    final org.apache.http.client.config.RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    final Integer connectionTimeoutParam = getInitParameter(CONNECTION_TIMEOUT_PROPERTY, connectionTimeout);
    if (connectionTimeoutParam != null) {
        requestConfigBuilder.setConnectTimeout(connectionTimeoutParam);

    }
    final Boolean staleCheckingEnabledParam = getInitParameter(STALE_CHECKING_ENABLED_PROPERTY,
            staleCheckingEnabled);
    if (staleCheckingEnabledParam != null) {
        requestConfigBuilder.setStaleConnectionCheckEnabled(staleCheckingEnabledParam);
    }
    final Integer soTimeoutParam = getInitParameter(SO_TIMEOUT_PROPERTY, soTimeout);
    if (soTimeoutParam != null) {
        requestConfigBuilder.setSocketTimeout(soTimeoutParam);
    }

    // AuthSchemeFactory
    final RegistryBuilder<AuthSchemeProvider> authSchemeProviderBuilder = RegistryBuilder.create();
    final Map<String, AuthSchemeProvider> factoryMap = getInitParameter(AUTH_SCHEME_PROVIDERS_PROPERTY,
            authSchemeProviderMap);
    if (factoryMap != null) {
        for (final Map.Entry<String, AuthSchemeProvider> entry : factoryMap.entrySet()) {
            authSchemeProviderBuilder.register(entry.getKey(), entry.getValue());
        }
    }

    // user agent
    userAgent = getInitParameter(USER_AGENT_PROPERTY, userAgent);
    if (StringUtil.isNotBlank(userAgent)) {
        httpClientBuilder.setUserAgent(userAgent);
    }

    final HttpRoutePlanner planner = buildRoutePlanner();
    if (planner != null) {
        httpClientBuilder.setRoutePlanner(planner);
    }

    // Authentication
    final Authentication[] siteCredentialList = getInitParameter(BASIC_AUTHENTICATIONS_PROPERTY,
            new Authentication[0]);
    for (final Authentication authentication : siteCredentialList) {
        final AuthScope authScope = authentication.getAuthScope();
        credentialsProvider.setCredentials(authScope, authentication.getCredentials());
        final AuthScheme authScheme = authentication.getAuthScheme();
        if (authScope.getHost() != null && authScheme != null) {
            final HttpHost targetHost = new HttpHost(authScope.getHost(), authScope.getPort());
            authCache.put(targetHost, authScheme);
        }
    }

    httpClientContext.setAuthCache(authCache);
    httpClientContext.setCredentialsProvider(credentialsProvider);

    // Request Header
    final RequestHeader[] requestHeaders = getInitParameter(REQUERT_HEADERS_PROPERTY, new RequestHeader[0]);
    for (final RequestHeader requestHeader : requestHeaders) {
        if (requestHeader.isValid()) {
            requestHeaderList.add(new BasicHeader(requestHeader.getName(), requestHeader.getValue()));
        }
    }

    // do not redirect
    requestConfigBuilder.setRedirectsEnabled(false);

    // cookie
    if (cookieSpec != null) {
        requestConfigBuilder.setCookieSpec(cookieSpec);
    }

    // cookie store
    httpClientBuilder.setDefaultCookieStore(cookieStore);
    if (cookieStore != null) {
        final Cookie[] cookies = getInitParameter(COOKIES_PROPERTY, new Cookie[0]);
        for (final Cookie cookie : cookies) {
            cookieStore.addCookie(cookie);
        }
    }

    connectionMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(
            new HcConnectionMonitorTarget(clientConnectionManager, idleConnectionTimeout),
            connectionCheckInterval, true);

    final CloseableHttpClient closeableHttpClient = httpClientBuilder
            .setConnectionManager(clientConnectionManager).setDefaultRequestConfig(requestConfigBuilder.build())
            .build();
    if (!httpClientPropertyMap.isEmpty()) {
        final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(closeableHttpClient.getClass());
        for (final Map.Entry<String, Object> entry : httpClientPropertyMap.entrySet()) {
            final String propertyName = entry.getKey();
            if (beanDesc.hasPropertyDesc(propertyName)) {
                final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(propertyName);
                propertyDesc.setValue(closeableHttpClient, entry.getValue());
            } else {
                logger.warn("DefaultHttpClient does not have " + propertyName + ".");
            }
        }
    }

    httpClient = closeableHttpClient;
}

From source file:org.jets3t.apps.cockpitlite.CockpitLite.java

/**
 * Implementation method for the CredentialsProvider interface.
 * <p>/*from  www  .j a  va2  s.c o  m*/
 * Based on sample code:
 * <a href="http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/examples/InteractiveAuthenticationExample.java?view=markup">InteractiveAuthenticationExample</a>
 *
 */
public Credentials getCredentials(AuthScope scope) {
    if (scope == null || scope.getScheme() == null) {
        return null;
    }
    Credentials credentials = mCredentialProvider.getCredentials(scope);
    if (credentials != null) {
        return credentials;
    }
    try {
        if (scope.getScheme().equals("ntlm")) {
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html>Host <b>" + scope.getHost() + ":" + scope.getPort()
                            + "</b> requires Windows authentication</html>",
                    true);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new NTCredentials(pwDialog.getUser(), pwDialog.getPassword(), scope.getHost(),
                        pwDialog.getDomain());
            }
            pwDialog.dispose();
        } else if (scope.getScheme().equals("basic") || scope.getScheme().equals("digest")) {
            //authscheme instanceof RFC2617Scheme
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html><center>Host <b>" + scope.getHost() + ":" + scope.getPort() + "</b>"
                            + " requires authentication for the realm:<br><b>" + scope.getRealm()
                            + "</b></center></html>",
                    false);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new UsernamePasswordCredentials(pwDialog.getUser(), pwDialog.getPassword());
            }
            pwDialog.dispose();
        } else {
            throw new InvalidCredentialsException("Unsupported authentication scheme: " + scope.getScheme());
        }
        if (credentials != null) {
            mCredentialProvider.setCredentials(scope, credentials);
        }
        return credentials;
    } catch (Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}