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:io.n7.calendar.caldav.BaseTestCase.java

public static HttpClient createHttpClient(CaldavCredential caldavCredential) {
    HttpClient http = new HttpClient();

    Credentials credentials = new UsernamePasswordCredentials(caldavCredential.user, caldavCredential.password);
    http.getState().setCredentials(new AuthScope(caldavCredential.host, caldavCredential.port), credentials);
    http.getParams().setAuthenticationPreemptive(true);
    return http;/*  w w w. java 2  s. c  om*/
}

From source file:com.streamreduce.util.PingdomClient.java

public PingdomClient(Connection connection) {
    super(connection);
    this.connection = connection;

    ConnectionCredentials credentials = connection.getCredentials();
    if (credentials == null) {
        throw new IllegalArgumentException("Connection must have username/password credentials.");
    }//from   w ww.j  a  v a2s . c  o m

    apiKey = credentials.getApiKey();
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(PINGDOM_HOST, 443),
            new UsernamePasswordCredentials(getConnectionCredentials().getIdentity(),
                    getConnectionCredentials().getCredential()));
}

From source file:com.sap.core.odata.testutil.tool.core.AbstractTestClient.java

private DefaultHttpClient createHttpClient(final CallerConfig config) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    if (config.isProxySet()) {
        initProxy(httpClient, config);/*  w ww .  ja  v  a  2 s.com*/
    }

    if (config.isBasicAuthCredentialsSet()) {
        final URI baseUri = config.getBaseUri();
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(baseUri.getHost(), baseUri.getPort()),
                new UsernamePasswordCredentials(config.getBasicAuthCredentials()));
    }
    return httpClient;
}

From source file:org.apiwatch.util.IO.java

public static APIScope getAPIData(String source, String format, String encoding, String username,
        String password) throws IOException, SerializationError, HttpException {
    File file = new File(source);
    APIScope scope = null;//  ww  w .j a va 2s  . c  o m
    if (file.isFile()) {
        if (format == null) {
            /* get format from file extension */
            format = source.substring(source.lastIndexOf('.') + 1);
        }
        InputStream in = new FileInputStream(file);
        Reader reader = new InputStreamReader(in, encoding);
        scope = Serializers.loadAPIScope(reader, format);
        reader.close();
        in.close();
    } else {
        /* maybe source is a URL */
        DefaultHttpClient client = new DefaultHttpClient();
        if (username != null && password != null) {
            client.getCredentialsProvider().setCredentials(new AuthScope(null, -1),
                    new UsernamePasswordCredentials(username, password));
        }
        HttpResponse response = client.execute(new HttpGet(source));
        if (response.getStatusLine().getStatusCode() >= 400) {
            throw new HttpException(response.getStatusLine().getReasonPhrase());
        }
        HttpEntity entity = response.getEntity();
        ContentType contentType = ContentType.fromHeader(entity.getContentType().getValue());
        if (entity.getContentEncoding() != null) {
            encoding = entity.getContentEncoding().getValue();
        } else if (contentType.charset != null) {
            encoding = contentType.charset;
        }
        if (format == null) {
            format = contentType.type;
        }
        InputStream in = entity.getContent();
        Reader reader = new InputStreamReader(in, encoding);
        scope = Serializers.loadAPIScope(reader, format);
        reader.close();
        in.close();
        client.getConnectionManager().shutdown();
    }
    return scope;
}

From source file:com.smartling.api.sdk.util.HttpProxyUtils.java

/**
 * Get an HttpClient given a proxy config if any
 * @param proxyConfiguration configuration of proxy to use
 * @return org.apache.http.impl.client.CloseableHttpClient
 *///from  ww w . ja v a  2  s .c  o m
public CloseableHttpClient getHttpClient(final ProxyConfiguration proxyConfiguration) {
    HttpClientBuilder httpClientBuilder = getHttpClientBuilder();

    if (proxyAuthenticationRequired(proxyConfiguration)) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort()),
                new UsernamePasswordCredentials(proxyConfiguration.getUsername(),
                        proxyConfiguration.getPassword()));

        httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }

    return httpClientBuilder.build();
}

From source file:com.unispezi.cpanelremotebackup.http.HTTPClient.java

public HTTPClient(String hostName, int port, boolean secure, String userName, String password) {
    this.httpClient = new DefaultHttpClient();
    host = new HttpHost(hostName, port, secure ? "https" : null);
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(hostName, port),
            new UsernamePasswordCredentials(userName, password));

    //        HttpHost proxy = new HttpHost("localhost", 8888);
    //        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

    // Set up HTTP basic auth (without challenge) or "preemptive auth"
    // Taken from http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html#d5e1031

    // Create AuthCache instance
    BasicAuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);//from w w  w.  ja  va2  s .  c om

    // Add AuthCache to the execution context
    localcontext = new BasicHttpContext();
    localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}

From source file:com.cisco.cta.taxii.adapter.httpclient.CredentialsProviderFactoryTest.java

@Test
public void proxyWithoutAuthentication() throws MalformedURLException {
    ProxySettings proxySettings = new ProxySettings();
    proxySettings.setAuthenticationType(ProxyAuthenticationType.NONE);
    proxySettings.setUrl(new URL("http://localhost:8001/"));
    CredentialsProviderFactory factory = new CredentialsProviderFactory(taxiiSettings, proxySettings);
    CredentialsProvider credsProvider = factory.build();
    assertNotNull(credsProvider);/*from w  ww  . j av a2s .co  m*/
    Credentials creds = credsProvider.getCredentials(new AuthScope("localhost", 8001));
    assertNull(creds);
}

From source file:de.escidoc.core.common.util.security.PreemptiveAuthInterceptor.java

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

    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        final AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        final CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        final HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            final Credentials creds = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication.");
            }//from  ww  w.ja v  a 2 s  .  co  m
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
        }
    }

}

From source file:org.apache.sling.launchpad.SmokeIT.java

private CloseableHttpClient newClient() {

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
    credsProvider.setCredentials(new AuthScope("localhost", LAUNCHPAD_PORT), creds);

    return HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
}

From source file:org.openlmis.UiUtils.HttpClient.java

public ResponseEntity SendJSON(String json, String url, String commMethod, String username, String password) {

    HttpHost targetHost = new HttpHost(HOST, PORT, PROTOCOL);
    AuthScope localhost = new AuthScope(HOST, PORT);

    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
    httpClient.getCredentialsProvider().setCredentials(localhost, credentials);

    AuthCache authCache = new BasicAuthCache();
    authCache.put(targetHost, new BasicScheme());

    httpContext.setAttribute(AUTH_CACHE, authCache);

    try {/* ww  w.j  av  a 2s  .co  m*/
        return handleRequest(commMethod, json, url, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}