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.evendanan.android.thumbremote.network.ReusableHttpClientBlocking.java

ReusableHttpClientBlocking(int timeout, String user, String password) {
    Log.d(TAG, "Creating a new HTTP client");
    mHttpClient = new DefaultHttpClient();
    if (!TextUtils.isEmpty(password)) {
        CredentialsProvider credProvider = new BasicCredentialsProvider();
        credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(user, password));
        mHttpClient.setCredentialsProvider(credProvider);
    }/* w  ww  .  j  a va  2 s.c o m*/
    mRequest = new HttpGet();
    mRequest.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(timeout));
    mRequest.getParams().setParameter(CoreConnectionPNames.SO_LINGER, new Integer(timeout / 2));
    mRequest.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(timeout));
}

From source file:com.ibm.watson.retrieveandrank.app.rest.UtilityFunctions.java

public static HttpClientBuilder createHTTPBuilder(URI uri, String username, String password) {
    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
            new UsernamePasswordCredentials(username, password));

    final HttpClientBuilder builder = HttpClientBuilder.create().setMaxConnTotal(128).setMaxConnPerRoute(32)
            .setDefaultRequestConfig(//from  w ww  .  j a  v a2 s  .  c  om
                    RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build());
    builder.setDefaultCredentialsProvider(credentialsProvider);
    builder.addInterceptorFirst(new PreemptiveAuthInterceptor());
    return builder;
}

From source file:com.webbfontaine.valuewebb.startup.ProxyConfiguration.java

public void addProxy(AbstractHttpClient httpClient) {
    if (ApplicationProperties.isAllowProxy()) {
        LOGGER.debug("Adding PROXY for HTTP Client");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(host, port),
                new UsernamePasswordCredentials(userName, password));
        httpClient.setCredentialsProvider(credsProvider);

        HttpHost proxy = new HttpHost(host, port, protocol);

        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    } else {/*from   w  w w .  j  ava2s  . c  om*/
        LOGGER.warn("HTTP Client has requested for PROXY but it is disabled.");
    }
}

From source file:dtu.ds.warnme.app.ws.client.https.ssl.PreemptiveAuthenticationRequestInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }/*w  w  w .  j a v a2 s . com*/
    }
}

From source file:org.apache.olingo.client.core.http.BasicAuthHttpClientFactory.java

@Override
public DefaultHttpClient create(final HttpMethod method, final URI uri) {
    final DefaultHttpClient httpclient = super.create(method, uri);

    httpclient.getCredentialsProvider().setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
            new UsernamePasswordCredentials(username, password));

    return httpclient;
}

From source file:se.vgregion.util.HTTPUtils.java

public static HttpResponse basicAuthRequest(String url, String username, String password,
        DefaultHttpClient client) throws HttpUtilsException {
    HttpGet get = new HttpGet(url);

    client.getCredentialsProvider().setCredentials(new AuthScope(null, 443),
            new UsernamePasswordCredentials(username, password));

    BasicHttpContext localcontext = new BasicHttpContext();

    // Generate BASIC scheme object and stick it to the local
    // execution context
    BasicScheme basicAuth = new BasicScheme();
    localcontext.setAttribute("preemptive-auth", basicAuth);

    // Add as the first request interceptor
    client.addRequestInterceptor(new PreemptiveAuth(), 0);
    HttpResponse response;/*from w w w  .  j  a  va2  s  .  com*/
    try {
        response = client.execute(get, localcontext);
    } catch (ClientProtocolException e) {
        throw new HttpUtilsException("Invalid http protocol", e);
    } catch (IOException e) {
        throw new HttpUtilsException(e.getMessage(), e);
    }
    return response;
}

From source file:org.megam.deccanplato.http.TransportMachinery.java

public static TransportResponse post(TransportTools nuts) throws ClientProtocolException, IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(nuts.urlString());
    System.out.println("NUTS" + nuts.toString());
    if (nuts.headers() != null) {
        for (Map.Entry<String, String> headerEntry : nuts.headers().entrySet()) {
            //this if condition is set for twilio Rest API to add credentials to DefaultHTTPClient, conditions met twilio work.
            if (headerEntry.getKey().equalsIgnoreCase("provider")
                    & headerEntry.getValue().equalsIgnoreCase(nuts.headers().get("provider"))) {
                httpclient.getCredentialsProvider().setCredentials(
                        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(
                                nuts.headers().get("account_sid"), nuts.headers().get("oauth_token")));
            }/*from   ww  w.  java  2  s.c o m*/
            //this else part statements for other providers
            else {
                httppost.addHeader(headerEntry.getKey(), headerEntry.getValue());
            }
        }
    }
    if (nuts.fileEntity() != null) {
        httppost.setEntity(nuts.fileEntity());
    }
    if (nuts.pairs() != null && (nuts.contentType() == null)) {
        httppost.setEntity(new UrlEncodedFormEntity(nuts.pairs()));
    }

    if (nuts.contentType() != null) {
        httppost.setEntity(new StringEntity(nuts.contentString(), nuts.contentType()));
    }
    TransportResponse transportResp = null;
    System.out.println(httppost.toString());
    try {
        HttpResponse httpResp = httpclient.execute(httppost);
        transportResp = new TransportResponse(httpResp.getStatusLine(), httpResp.getEntity(),
                httpResp.getLocale());
    } finally {
        httppost.releaseConnection();
    }
    return transportResp;

}

From source file:com.msopentech.odatajclient.engine.client.http.AbstractBasicAuthHttpClientFactory.java

@Override
public HttpClient createHttpClient(final HttpMethod method, final URI uri) {
    final DefaultHttpClient httpclient = (DefaultHttpClient) super.createHttpClient(method, uri);

    httpclient.getCredentialsProvider().setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
            new UsernamePasswordCredentials(getUsername(), getPassword()));

    return httpclient;
}

From source file:com.nestorledon.employeedirectory.http.HttpComponentsClientHttpRequestFactoryBasicAuth.java

public HttpComponentsClientHttpRequestFactoryBasicAuth(String user, String password) {
    super();/* ww w .  j  a v  a  2  s .c  o m*/
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(user, password));
    setHttpClient(HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build());
}

From source file:com.threatconnect.app.playbooks.db.tcapi.ConnectionUtil.java

/**
 * Adds proxy information to an http client builder
 * //  w  ww .  j  ava2s .c o m
 * @param builder
 * the HttpClientBuilder builder to add the proxy information to
 * @param proxyHost
 * the host of the proxy server
 * @param proxyPort
 * the port of the proxy server
 * @param proxyUserName
 * the username to authenticate with the proxy server (optional)
 * @param proxyPassword
 * the password to authenticate with the proxy server (optional)
 */
public static void addProxy(final HttpClientBuilder builder, final String proxyHost, final Integer proxyPort,
        final String proxyUserName, final String proxyPassword) {
    // check to see if the the host or port are null
    if (proxyHost == null || proxyPort == null) {
        logger.warn("proxyHost and proxyPort are required to connect to a proxy");
    } else {
        // add the proxy information to the builder
        builder.setProxy(new HttpHost(proxyHost, proxyPort));

        // authentication required
        if (proxyUserName != null && proxyPassword != null) {
            // add the credentials to the proxy information
            Credentials credentials = new UsernamePasswordCredentials(proxyUserName, proxyPassword);
            AuthScope authScope = new AuthScope(proxyHost, proxyPort);
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(authScope, credentials);
            builder.setDefaultCredentialsProvider(credsProvider);
        }
    }
}