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

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

Introduction

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

Prototype

public AuthScope(final String host, final int port, final String realm, final String scheme) 

Source Link

Document

Creates a new credentials scope for the given host, port, realm, and authentication scheme.

Usage

From source file:org.ow2.bonita.facade.rest.apachehttpclient.ApacheHttpClientUtil.java

public static HttpClient getHttpClient(String serverAddress, String username, String password)
        throws URISyntaxException {
    URI serverURI = new URI(serverAddress);
    DefaultHttpClient client = new DefaultHttpClient();
    AuthScope authScope = new AuthScope(serverURI.getHost(), serverURI.getPort(), AuthScope.ANY_REALM,
            AuthScope.ANY_SCHEME);//  w w w .  j ava2s . c  o m
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
    client.getCredentialsProvider().setCredentials(authScope, credentials);
    return client;
}

From source file:com.datatorrent.stram.util.WebServicesClientTest.java

public static void checkUserCredentials(String username, String password, AuthScheme authScheme)
        throws NoSuchFieldException, IllegalAccessException {
    CredentialsProvider provider = getCredentialsProvider();
    String httpScheme = AuthScope.ANY_SCHEME;
    if (authScheme == AuthScheme.BASIC) {
        httpScheme = AuthSchemes.BASIC;//from   ww w.  ja v a  2 s  . c  o  m
    } else if (authScheme == AuthScheme.DIGEST) {
        httpScheme = AuthSchemes.DIGEST;
    }
    AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM,
            httpScheme);
    Credentials credentials = provider.getCredentials(authScope);
    Assert.assertNotNull("Credentials", credentials);
    Assert.assertTrue("Credentials type is user",
            UsernamePasswordCredentials.class.isAssignableFrom(credentials.getClass()));
    UsernamePasswordCredentials pwdCredentials = (UsernamePasswordCredentials) credentials;
    Assert.assertEquals("Username", username, pwdCredentials.getUserName());
    Assert.assertEquals("Password", password, pwdCredentials.getPassword());
}

From source file:com.gargoylesoftware.htmlunit.DefaultCredentialsProvider3Test.java

/**
 * @throws Exception if an error occurs//from ww  w . ja v a2 s .co m
 */
@Test
public void serialization() throws Exception {
    final String username = "foo";
    final String password = "password";
    final String host = "my.host";
    final int port = 1234;
    final String realm = "blah";
    final String scheme = "NTLM";

    DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
    provider.addCredentials(username, password, host, port, realm);

    assertNotNull(provider.getCredentials(new AuthScope(host, port, realm, scheme)));
    assertNull(provider.getCredentials(new AuthScope("invalidHost", port, realm, scheme)));
    assertNotNull(provider.getCredentials(new AuthScope(host, port, realm, scheme)));
    assertNull(provider.getCredentials(new AuthScope("invalidHost", port, realm, scheme)));

    provider = SerializationUtils.clone(provider);

    assertNotNull(provider.getCredentials(new AuthScope(host, port, realm, scheme)));
    assertNull(provider.getCredentials(new AuthScope("invalidHost", port, realm, scheme)));
    assertNotNull(provider.getCredentials(new AuthScope(host, port, realm, scheme)));
    assertNull(provider.getCredentials(new AuthScope("invalidHost", port, realm, scheme)));
}

From source file:org.janusgraph.diskstorage.es.rest.util.BasicAuthHttpClientConfigCallback.java

public BasicAuthHttpClientConfigCallback(final String realm, final String username, final String password) {
    Preconditions.checkArgument(StringUtils.isNotEmpty(username),
            "HTTP Basic Authentication: username must be provided");
    Preconditions.checkArgument(StringUtils.isNotEmpty(password),
            "HTTP Basic Authentication: password must be provided");

    credentialsProvider = new BasicCredentialsProvider();

    final AuthScope authScope;
    if (StringUtils.isNotEmpty(realm)) {
        authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, realm, AuthScope.ANY_SCHEME);
    } else {/*  ww  w .  j  a v a  2  s  . co  m*/
        authScope = AuthScope.ANY;
    }
    credentialsProvider.setCredentials(authScope, new UsernamePasswordCredentials(username, password));
}

From source file:com.consol.citrus.http.client.BasicAuthClientRequestFactoryTest.java

@Test
public void testFactory() {
    Assert.assertNotNull(requestFactory);
    Assert.assertNotNull(requestFactory.getHttpClient());
    Assert.assertNotNull(requestFactory.getHttpClient().getParams());

    CredentialsProvider credentialsProvider = ((AbstractHttpClient) requestFactory.getHttpClient())
            .getCredentialsProvider();//  w  ww  .j a va  2s . com
    AuthScope authScope = new AuthScope("localhost", 8088, "", "basic");
    Assert.assertNotNull(credentialsProvider);
    Assert.assertNotNull(credentialsProvider.getCredentials(authScope));
    Assert.assertNotNull(credentialsProvider.getCredentials(authScope).getUserPrincipal().getName(),
            "someUsername");
    Assert.assertNotNull(credentialsProvider.getCredentials(authScope).getPassword(), "somePassword");

    Assert.assertNotNull(requestFactory.getHttpClient().getParams().getParameter("http.socket.timeout"));
    Assert.assertEquals(requestFactory.getHttpClient().getParams().getIntParameter("http.socket.timeout", 0),
            10000);
}

From source file:bad.robot.http.apache.matchers.CredentialsMatcher.java

private CredentialsMatcher(URL url, String username, String password) {
    this.username = username;
    this.password = password;
    this.scope = new AuthScope(url.getHost(), url.getPort(), ANY_REALM, ANY_SCHEME);
}

From source file:de.e7o.caldav.caldav.Connection.java

public InputStream request(String extUri, String requestMethod, String bodyData, String[]... additionalHeaders)
        throws Exception {
    int i;/*from  w ww .  j  av  a2 s  .c  o m*/

    // Pre
    HttpClient httpclient = new DefaultHttpClient();
    HttpAny ha = new HttpAny(base.baseUri + extUri);
    ha.setMethod(requestMethod);

    // Headers
    for (i = 0; i < additionalHeaders.length; i++) {
        ha.addHeader(additionalHeaders[i][0], additionalHeaders[i][1]);
    }

    // Body
    ha.setEntity(new StringEntity(bodyData));

    // Authentication
    URL url = new URL(base.baseUri);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(this.authInfo.getUserName(),
            this.authInfo.getPassword());
    ((AbstractHttpClient) httpclient).getCredentialsProvider()
            .setCredentials(new AuthScope(url.getHost(), 80, null, "Basic"), credentials);

    // Send request
    HttpResponse httpresp = httpclient.execute(ha);

    // Remember state
    lastCode = httpresp.getStatusLine().getStatusCode();
    lastHeaders = httpresp.getAllHeaders();

    // Done - return InputStream
    HttpEntity httpent = httpresp.getEntity();
    return httpent.getContent();
}

From source file:org.sonatype.nexus.ant.staging.deploy.ZapperImpl.java

@Override
public void deployDirectory(final ZapperRequest zapperRequest) throws IOException {
    try {/*  ww  w . j  av a 2 s. c o  m*/
        HttpHost proxyServer = null;
        BasicCredentialsProvider credentialsProvider = null;
        if (!StringUtils.isBlank(zapperRequest.getProxyProtocol())) {
            proxyServer = new HttpHost(zapperRequest.getProxyHost(), zapperRequest.getProxyPort(),
                    zapperRequest.getProxyProtocol());

            if (!StringUtils.isBlank(zapperRequest.getProxyUsername())) {
                UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(
                        zapperRequest.getProxyUsername(), zapperRequest.getProxyPassword());

                credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(new AuthScope(proxyServer.getHostName(),
                        proxyServer.getPort(), AuthScope.ANY_REALM, proxyServer.getSchemeName()),
                        proxyCredentials);
            }
        }

        if (!StringUtils.isBlank(zapperRequest.getRemoteUsername())) {
            UsernamePasswordCredentials remoteCredentials = new UsernamePasswordCredentials(
                    zapperRequest.getRemoteUsername(), zapperRequest.getRemotePassword());

            if (credentialsProvider == null) {
                credentialsProvider = new BasicCredentialsProvider();
            }

            credentialsProvider.setCredentials(AuthScope.ANY, remoteCredentials);
        }

        final Parameters parameters = ParametersBuilder.defaults().build();
        final Hc4ClientBuilder clientBuilder = new Hc4ClientBuilder(parameters, zapperRequest.getRemoteUrl());
        if (credentialsProvider != null) {
            clientBuilder.withPreemptiveRealm(credentialsProvider);
        }
        if (proxyServer != null) {
            clientBuilder.withProxy(proxyServer);
        }
        final Client client = clientBuilder.build();
        final IOSourceListable deployables = new DirectoryIOSource(zapperRequest.getStageRepository());

        try {
            client.upload(deployables);
        } finally {
            client.close();
        }
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException("Unable to deploy!", e);
    }
}

From source file:org.sonatype.nexus.plugin.deploy.ZapperImpl.java

@Override
public void deployDirectory(final ZapperRequest zapperRequest) throws IOException {
    try {/*ww  w .  ja v  a  2 s. co m*/
        HttpHost proxyServer = null;
        BasicCredentialsProvider credentialsProvider = null;
        if (!StringUtils.isBlank(zapperRequest.getProxyProtocol())) {
            proxyServer = new HttpHost(zapperRequest.getProxyHost(), zapperRequest.getProxyPort(),
                    zapperRequest.getProxyProtocol());

            if (!StringUtils.isBlank(zapperRequest.getProxyUsername())) {
                UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(
                        zapperRequest.getProxyUsername(), zapperRequest.getProxyPassword());

                credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(new AuthScope(proxyServer.getHostName(),
                        proxyServer.getPort(), AuthScope.ANY_REALM, proxyServer.getSchemeName()),
                        proxyCredentials);
            }
        }

        if (!StringUtils.isBlank(zapperRequest.getRemoteUsername())) {
            UsernamePasswordCredentials remoteCredentials = new UsernamePasswordCredentials(
                    zapperRequest.getRemoteUsername(), zapperRequest.getRemotePassword());

            if (credentialsProvider == null) {
                credentialsProvider = new BasicCredentialsProvider();
            }

            credentialsProvider.setCredentials(AuthScope.ANY, remoteCredentials);
        }

        final Parameters parameters = ParametersBuilder.defaults().build();
        final Hc4ClientBuilder clientBuilder = new Hc4ClientBuilder(parameters, zapperRequest.getRemoteUrl());
        if (credentialsProvider != null) {
            clientBuilder.withRealm(credentialsProvider);
        }
        if (proxyServer != null) {
            clientBuilder.withProxy(proxyServer);
        }
        final Client client = clientBuilder.build();
        final IOSourceListable deployables = new DirectoryIOSource(zapperRequest.getStageRepository());

        try {
            client.upload(deployables);
        } finally {
            client.close();
        }
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException("Unable to deploy!", e);
    }
}