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) 

Source Link

Document

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

Usage

From source file:net.seedboxer.seedroid.utils.RestClient.java

public void AddBasicAuthentication(String username, String password) {
    credProvider = new BasicCredentialsProvider();
    credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(username, password));
}

From source file:com.impetus.client.couchdb.utils.CouchDBTestUtils.java

/**
 * Initiate http client.//from   ww w  . j av a2s.  co  m
 * 
 * @param kunderaMetadata
 *            the kundera metadata
 * @param persistenceUnit
 *            the persistence unit
 * @return the http client
 */
public static HttpClient initiateHttpClient(final KunderaMetadata kunderaMetadata, String persistenceUnit) {
    PersistenceUnitMetadata pumMetadata = KunderaMetadataManager.getPersistenceUnitMetadata(kunderaMetadata,
            persistenceUnit);

    SchemeSocketFactory ssf = null;
    ssf = PlainSocketFactory.getSocketFactory();
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    int port = Integer.parseInt(pumMetadata.getProperty(PersistenceProperties.KUNDERA_PORT));
    String host = pumMetadata.getProperty(PersistenceProperties.KUNDERA_NODES);
    String userName = pumMetadata.getProperty(PersistenceProperties.KUNDERA_USERNAME);
    String password = pumMetadata.getProperty(PersistenceProperties.KUNDERA_PASSWORD);

    schemeRegistry.register(new Scheme("http", port, ssf));
    PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schemeRegistry);
    HttpClient httpClient = new DefaultHttpClient(ccm);

    try {
        // Http params
        httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");

        // basic authentication
        if (userName != null && password != null) {
            ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(new AuthScope(host, port),
                    new UsernamePasswordCredentials(userName, password));
        }
        // request interceptor
        ((DefaultHttpClient) httpClient).addRequestInterceptor(new HttpRequestInterceptor() {
            public void process(final HttpRequest request, final HttpContext context) throws IOException {

            }
        });
        // response interceptor
        ((DefaultHttpClient) httpClient).addResponseInterceptor(new HttpResponseInterceptor() {
            public void process(final HttpResponse response, final HttpContext context) throws IOException {

            }
        });
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    return httpClient;
}

From source file:com.ibm.connectors.splunklog.SplunkHttpConnection.java

public Properties sendLogEvent(SplunkConnectionData connData, byte[] thePayload, Properties properties)
        throws ConnectorException {
    HttpClient myhClient = HttpClients.custom().setConnectionManager(this.cm).build();

    HttpClientContext myhContext = HttpClientContext.create();

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(connData.getUser(), connData.getPass()));
    AuthCache authCache = new BasicAuthCache();
    authCache.put(new HttpHost(connData.getHost(), Integer.parseInt(connData.getPort()), connData.getScheme()),
            new BasicScheme());

    myhContext.setCredentialsProvider(credsProvider);
    myhContext.setAuthCache(authCache);/* w  w  w .j  a va2 s .c om*/

    HttpPost myhPost = new HttpPost(connData.getSplunkURI());

    ByteArrayEntity payload = new ByteArrayEntity(thePayload);
    try {
        myhPost.setEntity(payload);
        HttpResponse response = myhClient.execute(myhPost, myhContext);
        Integer statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != 200 && !connData.getIgnoreSplunkErrors()) {
            throw new ConnectorException(
                    "Error posting log event to Splunk: " + response.getStatusLine().toString());
        }
        System.out.println(response.getStatusLine().toString());
        properties.setProperty("status", String.valueOf(statusCode));
        Integer leasedConns = Integer
                .valueOf(((PoolingHttpClientConnectionManager) this.cm).getTotalStats().getLeased());
        properties.setProperty("conns_leased", leasedConns.toString());
        Integer availConns = Integer
                .valueOf(((PoolingHttpClientConnectionManager) this.cm).getTotalStats().getAvailable());
        properties.setProperty("conns_available", availConns.toString());
    } catch (IOException e) {
        e.fillInStackTrace();
        throw new ConnectorException(e.toString());
    }
    return properties;
}

From source file:org.openscore.content.httpclient.build.auth.CredentialsProviderBuilder.java

public CredentialsProvider buildCredentialsProvider() {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

    if (!StringUtils.isEmpty(username)) {
        Credentials credentials;//from  ww  w  . j av a  2  s  .c o  m
        if (authTypes.contains(AuthTypes.NTLM)) {
            String[] domainAndUsername = getDomainUsername(username);
            credentials = new NTCredentials(domainAndUsername[1], password, host, domainAndUsername[0]);
        } else {
            credentials = new UsernamePasswordCredentials(username, password);
        }
        credentialsProvider.setCredentials(new AuthScope(host, Integer.parseInt(port)), credentials);
    } else if (authTypes.contains(AuthTypes.KERBEROS)) {
        credentialsProvider.setCredentials(new AuthScope(host, Integer.parseInt(port)), new Credentials() {
            @Override
            public Principal getUserPrincipal() {
                return null;
            }

            @Override
            public String getPassword() {
                return null;
            }
        });
    }

    if (!StringUtils.isEmpty(proxyUsername)) {
        int intProxyPort = 8080;
        if (!StringUtils.isEmpty(proxyPort)) {
            intProxyPort = Integer.parseInt(proxyPort);
        }
        credentialsProvider.setCredentials(new AuthScope(proxyHost, intProxyPort),
                new UsernamePasswordCredentials(proxyUsername, proxyPassword));
    }

    return credentialsProvider;
}

From source file:org.hawkular.client.RestFactory.java

public T createAPI(URI uri, String userName, String password) {
    HttpClient httpclient = null;/*from www. java  2s.  c  om*/
    if (uri.toString().startsWith("https")) {
        httpclient = getHttpClient();
    } else {
        httpclient = HttpClientBuilder.create().build();
    }
    ResteasyClient client = null;
    if (userName != null) {
        HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort());
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials(userName, password));
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);
        // Add AuthCache to the execution context
        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);
        context.setAuthCache(authCache);
        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpclient, context);

        client = new ResteasyClientBuilder().httpEngine(engine).build();
    } else {
        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(getHttpClient());
        client = new ResteasyClientBuilder().httpEngine(engine).build();
    }

    client.register(JacksonJaxbJsonProvider.class);
    client.register(JacksonObjectMapperProvider.class);
    client.register(RestRequestFilter.class);
    client.register(RestResponseFilter.class);
    client.register(HCJacksonJson2Provider.class);
    ProxyBuilder<T> proxyBuilder = client.target(uri).proxyBuilder(apiClassType);
    if (classLoader != null) {
        proxyBuilder = proxyBuilder.classloader(classLoader);
    }
    return proxyBuilder.build();
}

From source file:io.adventurous.android.api.ApiHttpClient.java

private static void setApiCredentials(ApiCredentials credentials, HttpRequestBase request) {
    if (credentials == null || credentials instanceof NullApiCredentials) {
        mClient.getCredentialsProvider().clear();
    } else {//from   w  w  w  .ja  v  a2  s . c  o  m
        String host = request.getURI().getHost();
        int authPort = request.getURI().getPort();

        mClient.getCredentialsProvider().setCredentials(new AuthScope(host, authPort),
                new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword()));
    }
}

From source file:org.droidparts.http.wrapper.DefaultHttpClientWrapper.java

@Override
public void authenticateBasic(String authUser, String authPassword) {
    AuthScope authScope = new AuthScope(ANY_HOST, ANY_PORT);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(authUser, authPassword);
    httpClient.getCredentialsProvider().setCredentials(authScope, credentials);
}

From source file:ca.ubc.ece.netsyslab.tagvalue.system.TaggingSystem.java

/**
 * A convenience constructor that allows initializing the base URL
 *///from   ww w . j a  v a2 s  .c o  m
public TaggingSystem(String hostname, int port, String username, String password) {
    host = new HttpHost(hostname, port, "https");
    userId = username;
    userPassword = password;
    httpClient = new DefaultHttpClient();
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(host.getHostName(), host.getPort()),
            new UsernamePasswordCredentials(userId, userPassword));
}

From source file:com.jayway.restassured.internal.http.AuthConfig.java

/**
 * Set authentication credentials to be used for the given host and port.
 *
 * @param host/*from w w  w . j  a  v a2  s .c om*/
 * @param port
 * @param user
 * @param pass
 */
public void basic(String host, int port, String user, String pass) {
    builder.getClient().getCredentialsProvider().setCredentials(new AuthScope(host, port),
            new UsernamePasswordCredentials(user, pass));
}