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

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

Introduction

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

Prototype

public UsernamePasswordCredentials(final String userName, final String password) 

Source Link

Document

The constructor with the username and password arguments.

Usage

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 ww  . j a v a2s  . c  om
    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.gbif.registry.utils.Organizations.java

/**
 * Populate credentials used in ws requests.
 *
 * @param organization Organization/*w  w  w.  jav  a  2s  . c  o m*/
 *
 * @return credentials
 */
public static UsernamePasswordCredentials credentials(Organization organization) {
    return new UsernamePasswordCredentials(organization.getKey().toString(), organization.getPassword());
}

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

public HttpComponentsClientHttpRequestFactoryBasicAuth(String user, String password) {
    super();/*  w  w w  .  ja va  2s .  c om*/
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(user, password));
    setHttpClient(HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build());
}

From source file:org.deegree.maven.utils.HttpUtils.java

public static HttpClient getAuthenticatedHttpClient(ServiceIntegrationTestHelper helper) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 30000);
    HttpConnectionParams.setSoTimeout(client.getParams(), 1200000);
    client.getCredentialsProvider().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("deegree", "deegree"));
    // preemptive authentication used to be easier in pre-4.x httpclient
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    HttpHost host = new HttpHost("localhost", Integer.parseInt(helper.getPort()));
    authCache.put(host, basicAuth);/*from  w  w w . ja  v a2s . c  om*/
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(AUTH_CACHE, authCache);
    return client;
}

From source file:com.cloudhopper.httpclient.util.PreemptiveBasicAuthHttpRequestInterceptor.java

public void setCredentials(String username, String password) {
    this.credentials = new UsernamePasswordCredentials(username, password);
}

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 w  ww.j av a  2  s . co  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:org.apache.camel.component.http4.BasicAuthenticationHttpClientConfigurer.java

public void configureHttpClient(HttpClient client) {
    Credentials defaultcreds;/*from  ww  w  .  j  a va  2s. c  o m*/
    if (domain != null) {
        defaultcreds = new NTCredentials(username, password, host, domain);
    } else {
        defaultcreds = new UsernamePasswordCredentials(username, password);
    }
    ((DefaultHttpClient) client).getCredentialsProvider().setCredentials(AuthScope.ANY, defaultcreds);
}

From source file:org.xwiki.wysiwyg.internal.plugin.alfresco.server.BasicAuthenticator.java

@Override
public void authenticate(HttpRequestBase request) {
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(configuration.getUserName(),
            configuration.getPassword());
    request.addHeader(BasicScheme.authenticate(credentials, "US-ASCII", false));
}