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:httpclient.client.ClientInteractiveAuthentication.java

public static void main(String[] args) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();

    // Create local execution context
    HttpContext localContext = new BasicHttpContext();

    HttpGet httpget = new HttpGet("http://localhost/test");

    boolean trying = true;
    while (trying) {
        System.out.println("executing request " + httpget.getRequestLine());
        HttpResponse response = httpclient.execute(httpget, localContext);

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());

        // Consume response content
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            entity.consumeContent();/*  w  w  w .  j  ava2  s .c  o  m*/
        }

        int sc = response.getStatusLine().getStatusCode();

        AuthState authState = null;
        if (sc == HttpStatus.SC_UNAUTHORIZED) {
            // Target host authentication required
            authState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
        }
        if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
            // Proxy authentication required
            authState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE);
        }

        if (authState != null) {
            System.out.println("----------------------------------------");
            AuthScope authScope = authState.getAuthScope();
            System.out.println("Please provide credentials");
            System.out.println(" Host: " + authScope.getHost() + ":" + authScope.getPort());
            System.out.println(" Realm: " + authScope.getRealm());

            BufferedReader console = new BufferedReader(new InputStreamReader(System.in));

            System.out.print("Enter username: ");
            String user = console.readLine();
            System.out.print("Enter password: ");
            String password = console.readLine();

            if (user != null && user.length() > 0) {
                Credentials creds = new UsernamePasswordCredentials(user, password);
                httpclient.getCredentialsProvider().setCredentials(authScope, creds);
                trying = true;
            } else {
                trying = false;
            }
        } else {
            trying = false;
        }
    }

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
}

From source file:com.asakusafw.shafu.internal.core.net.ShafuCredentialsProvider.java

@Override
public Credentials getCredentials(AuthScope authscope) {
    String host = authscope.getHost();
    if (host != AuthScope.ANY_HOST) {
        Scope scope = new Scope(authscope.getScheme(), host, authscope.getPort(), authscope.getRealm());
        for (IHttpCredentialsProvider provider : Activator.getExtensions().createHttpCredentialsProvider()) {
            try {
                IHttpCredentials creds = provider.find(scope);
                if (creds != null) {
                    return new UsernamePasswordCredentials(creds.getUserName(), creds.getPassword());
                }//from   w ww.  jav a2 s.  c  om
            } catch (CoreException e) {
                LogUtil.log(e.getStatus());
            }
        }
    }
    return super.getCredentials(authscope);
}

From source file:org.eclipse.aether.transport.http.DemuxCredentialsProvider.java

private CredentialsProvider getDelegate(AuthScope authScope) {
    if (proxy.getPort() == authScope.getPort() && proxy.getHostName().equalsIgnoreCase(authScope.getHost())) {
        return proxyCredentialsProvider;
    }/*from  w  w  w.  j av  a  2s . c o m*/
    return serverCredentialsProvider;
}

From source file:com.machinepublishers.jbrowserdriver.ProxyAuth.java

/**
 * {@inheritDoc}//from w  w w.  j a  v  a2 s .  c o  m
 */
@Override
public Credentials getCredentials(AuthScope authScope) {
    synchronized (lock) {
        return proxies.get(new StringBuilder().append(authScope.getHost()).append(":")
                .append(authScope.getPort()).toString());
    }
}

From source file:org.apache.maven.wagon.providers.http.BasicAuthScopeTest.java

/**
 * Test AuthScope override with no overriding values set. Nothing should
 * change in original host/port./*from   ww  w.j  a v  a 2  s. co  m*/
 */
@Test
public void testGetScopeNothingOverridden() {
    BasicAuthScope scope = new BasicAuthScope();

    AuthScope authScope = scope.getScope("original.host.com", 3456);
    Assert.assertEquals("original.host.com", authScope.getHost());
    Assert.assertEquals(3456, authScope.getPort());
    Assert.assertEquals(AuthScope.ANY_REALM, authScope.getRealm());
}

From source file:org.apache.maven.wagon.providers.http.BasicAuthScopeTest.java

/**
 * Test AuthScope override for realm value overridden
 *//*from  ww  w.j ava  2 s  . c  o m*/
@Test
public void testGetScopeRealmOverridden() {
    BasicAuthScope scope = new BasicAuthScope();
    scope.setRealm("override-realm");
    AuthScope authScope = scope.getScope("original.host.com", 3456);
    Assert.assertEquals("original.host.com", authScope.getHost());
    Assert.assertEquals(3456, authScope.getPort());
    Assert.assertEquals("override-realm", authScope.getRealm());
}

From source file:org.apache.maven.wagon.providers.http.BasicAuthScopeTest.java

/**
 * Test AuthScope override for all values overridden with "ANY"
 *///from  ww  w.  j a va2s  .co m
@Test
public void testGetScopeAllAny() {
    BasicAuthScope scope = new BasicAuthScope();
    scope.setHost("ANY");
    scope.setPort("ANY");
    scope.setRealm("ANY");
    AuthScope authScope = scope.getScope("original.host.com", 3456);
    Assert.assertEquals(AuthScope.ANY_HOST, authScope.getHost());
    Assert.assertEquals(AuthScope.ANY_PORT, authScope.getPort());
    Assert.assertEquals(AuthScope.ANY_REALM, authScope.getRealm());
}

From source file:org.apache.maven.wagon.providers.http.BasicAuthScopeTest.java

/**
 * Test AuthScope override for all values overridden
 *///  w w w.j  a v  a 2s  .c om
@Test
public void testGetScopeAllOverridden() {
    BasicAuthScope scope = new BasicAuthScope();
    scope.setHost("override.host.com");
    scope.setPort("1234");
    scope.setRealm("override-realm");
    AuthScope authScope = scope.getScope("original.host.com", 3456);
    Assert.assertEquals("override.host.com", authScope.getHost());
    Assert.assertEquals(1234, authScope.getPort());
    Assert.assertEquals("override-realm", authScope.getRealm());
}

From source file:org.eclipse.ecf.internal.provider.filetransfer.httpclient4.HttpClientProxyCredentialProvider.java

private boolean matchAuthScopeAndProxy(AuthScope authscope, Proxy proxy) {
    ProxyAddress proxyAddress = proxy.getAddress();
    return (authscope.getHost().equals(proxyAddress.getHostName())
            && (authscope.getPort() == proxyAddress.getPort()));
}

From source file:com.mgmtp.jfunk.web.JFunkHtmlUnitDriverImpl.java

@Override
public Credentials getCredentials(final AuthScope authscope) {
    String host = authscope.getHost();
    log.debug("Retrieving credentials for host " + host);

    CredentialsProvider credentialsProvider = credentialsProviderMap.get(host);
    checkState(credentialsProvider != null, "No credentials provider found for host " + host);

    return credentialsProvider.getCredentials(authscope);
}