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

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

Introduction

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

Prototype

public int getPort() 

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 ww . j  ava  2 s . c om
        }

        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: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  .  ja v  a  2s.c  om*/
    return serverCredentialsProvider;
}

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

/**
 * Test AuthScope where original port is -1, which should result in ANY
 *///from  w ww.  j a v a 2s .  c  om
@Test
public void testGetScopeOriginalPortIsNegativeOne() {
    BasicAuthScope scope = new BasicAuthScope();
    AuthScope authScope = scope.getScope("original.host.com", -1);
    Assert.assertEquals(AuthScope.ANY_PORT, authScope.getPort());
}

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  w  w .j a  v  a2s.  com*/
            } catch (CoreException e) {
                LogUtil.log(e.getStatus());
            }
        }
    }
    return super.getCredentials(authscope);
}

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

/**
 * {@inheritDoc}/*w  w  w .j a  v a2 s.  c om*/
 */
@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.// ww  w.j a  v a 2s.  c o 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 w w w  .  java  2s. c  om
@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
 */// w  ww .  ja v a  2s.  c o  m
@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:com.nesscomputing.httpclient.factory.httpclient4.InternalCredentialsProvider.java

@Override
public Credentials getCredentials(final AuthScope authScope) {
    for (final HttpClientAuthProvider authProvider : authProviders) {
        if (authProvider.acceptRequest(authScope.getScheme(), authScope.getHost(), authScope.getPort(),
                authScope.getRealm())) {
            return new Credentials() {

                @Override//from   www  .j  av  a  2 s  . c  om
                public Principal getUserPrincipal() {
                    return new BasicUserPrincipal(authProvider.getUser());
                }

                @Override
                public String getPassword() {
                    return authProvider.getPassword();
                }

            };
        }
    }
    return null;
}

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

/**
 * Test AuthScope override for all values overridden with "ANY"
 *///w w w.ja v a2  s  .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());
}