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:be.benvd.mvforandroid.data.MVDataHelper.java

/**
 * Returns the GET response of the given url.
 * /*w  w  w .  ja v  a 2  s.c  o m*/
 * @throws IOException
 * @return The response of the given URL. If no response was found, null is
 *         returned.
 */
public static String getResponse(String username, String password, String url) throws IOException {
    /*      DefaultHttpClient httpclient = new DefaultHttpClient();
          httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(username + ":" + password));
          HttpGet httpget = new HttpGet(url);
          HttpResponse response = httpclient.execute(httpget);
    */
    DefaultHttpClient httpclient = new DefaultHttpClient();
    Credentials creds = new UsernamePasswordCredentials(username, password);
    httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), creds);

    String auth = android.util.Base64.encodeToString((username + ":" + password).getBytes("UTF-8"),
            android.util.Base64.NO_WRAP);

    HttpGet httpget = new HttpGet(url);
    httpget.addHeader("Authorization", "Basic " + auth);
    HttpResponse response = httpclient.execute(httpget);

    if (response.getEntity() != null) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        Log.v(MVDataHelper.class.getSimpleName(), "Response:" + sb.toString());
        return sb.toString();
    }

    return null;
}

From source file:no.norrs.projects.andronary.utils.HttpUtil.java

public static HttpResponse GET(URL url, UsernamePasswordCredentials creds)
        throws IOException, URISyntaxException {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    httpClient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), creds);

    HttpGet httpGet = new HttpGet(url.toURI());

    httpGet.addHeader("Accept", "application/json");
    httpGet.addHeader("User-Agent", "Andronary/0.1");
    HttpResponse response;//from  w ww  . java2s  .  c om

    return httpClient.execute(httpGet);
}

From source file:com.ibm.CloudResourceBundle.java

private static Rows getServerResponse(CloudDataConnection connect) throws Exception {
    Rows rows = null;/*from   w w w  . j  av  a  2 s  .c  o m*/

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("provide.castiron.com", 443),
            new UsernamePasswordCredentials(connect.getUserid(), connect.getPassword()));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

    try {
        // Call the service and get all the strings for all the languages
        HttpGet httpget = new HttpGet(connect.getURL());
        httpget.addHeader("API_SECRET", connect.getSecret());
        CloseableHttpResponse response = httpclient.execute(httpget);

        try {
            InputStream in = response.getEntity().getContent();
            ObjectMapper mapper = new ObjectMapper();
            rows = mapper.readValue(new InputStreamReader(in, "UTF-8"), Rows.class);
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
    return rows;
}

From source file:de.cuseb.bilderbuch.helpers.HttpClientProviderService.java

public HttpClient getHttpClientWithBasicAuth(String username, String password) {

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(username, password));

    return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
}

From source file:org.wso2.developerstudio.appfactory.core.client.HttpsJenkinsClient.java

public static HttpResponse getBulildinfo(String applicationId, String version, String builderBaseUrl,
        int lastBuildNo) {

    DefaultHttpClient client = new DefaultHttpClient();
    client = (DefaultHttpClient) HttpsJaggeryClient.wrapClient(client, builderBaseUrl);
    client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(Authenticator.getInstance().getCredentials().getUser(),
                    Authenticator.getInstance().getCredentials().getPassword()));

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

    // Add as the first (because of the zero) request interceptor
    // It will first intercept the request and preemptively initialize the authentication scheme if there is not
    client.addRequestInterceptor(new PreemptiveAuth(), 0);
    String getUrl = builderBaseUrl + "/job/" + applicationId + "-" + version + "-default/" + lastBuildNo
            + "/consoleText";
    HttpGet get = new HttpGet(getUrl);

    try {/*from w w  w.ja  v  a2 s.  c o m*/
        // Execute your request with the given context
        HttpResponse response = client.execute(get, context);
        return response;
    } catch (Exception e) {
        log.error("Jenkins Client err", e);
    }
    return null;
}

From source file:kuona.client.PreemptiveAuth.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);

    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            Credentials creds = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }// w  w w.j a  v a  2s .  c om
            authState.update(authScheme, creds);
        }
    }
}

From source file:org.commonjava.indy.client.core.auth.BasicAuthenticator.java

public HttpClientContext decoratePrototypeContext(final URL url, final HttpClientContext ctx) {
    final AuthScope as = new AuthScope(url.getHost(), url.getPort() < 0 ? url.getDefaultPort() : url.getPort());
    return decoratePrototypeContext(as, null, null, ctx);
}

From source file:de.simu.decomap.component.polling.impl.helper.RestClient.java

/**
 * /*from w w  w.  j  av  a2s  . c  om*/
 * @param username
 *            The username for the authentification
 * @param password
 *            The password for the authentification
 */
public RestClient(String username, String password) {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(username, password));
    //      HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    //setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}

From source file:com.mycompany.projecta.JenkinsScraper.java

public String scrape(String urlString, String username, String password)
        throws ClientProtocolException, IOException {
    URI uri = URI.create(urlString);
    HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.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(host, basicAuth);//from ww  w .j  a  va 2  s  .c  o m
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    HttpGet httpGet = new HttpGet(uri);
    // Add AuthCache to the execution context
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);

    HttpResponse response = httpClient.execute(host, httpGet, localContext);

    String resp = response.toString();

    return EntityUtils.toString(response.getEntity());
}

From source file:io.cloudslang.content.httpclient.build.auth.CredentialsProviderBuilderTest.java

@Test
public void createNtlmCredentialsProvider() {
    CredentialsProvider credentialsProvider = getCredentialsProvider(AuthSchemes.NTLM);
    Credentials credentials = credentialsProvider.getCredentials(new AuthScope("host", 80));

    assertThat(credentials, instanceOf(NTCredentials.class));
    NTCredentials ntCredentials = (NTCredentials) credentials;
    assertEquals("DOMAIN", ntCredentials.getDomain());
    assertEquals("HOST", ntCredentials.getWorkstation());
    assertEquals("pass", ntCredentials.getPassword());
    Credentials proxyCredentials = credentialsProvider.getCredentials(new AuthScope("proxy", 8080));
    assertThat(proxyCredentials, instanceOf(UsernamePasswordCredentials.class));
    UsernamePasswordCredentials userCredentials = (UsernamePasswordCredentials) proxyCredentials;
    assertEquals("proxyUsername", userCredentials.getUserName());
}