Search in sources :

Example 41 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project openmeetings by apache.

the class AppointmentManager method provideCredentials.

/**
 * Adds the Credentials provided to the given client on the Calendar's URL.
 *
 * @param client      Client which makes the connection.
 * @param calendar    Calendar whose Host the Credentials are for.
 * @param credentials Credentials to add
 */
public void provideCredentials(HttpClient client, OmCalendar calendar, Credentials credentials) {
    if (!Strings.isEmpty(calendar.getHref()) && credentials != null) {
        URI temp = URI.create(calendar.getHref());
        client.getHostConfiguration().setHost(temp.getHost(), temp.getPort(), temp.getScheme());
        client.getState().setCredentials(new AuthScope(temp.getHost(), temp.getPort()), credentials);
    }
}
Also used : AuthScope(org.apache.commons.httpclient.auth.AuthScope) URI(java.net.URI)

Example 42 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project cosmic by MissionCriticalCloud.

the class UriUtils method getInputStreamFromUrl.

public static InputStream getInputStreamFromUrl(final String url, final String user, final String password) {
    try {
        final Pair<String, Integer> hostAndPort = validateUrl(url);
        final HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager());
        if ((user != null) && (password != null)) {
            httpclient.getParams().setAuthenticationPreemptive(true);
            final Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
            httpclient.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds);
            s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second());
        }
        // Execute the method.
        final GetMethod method = new GetMethod(url);
        final int statusCode = httpclient.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            s_logger.error("Failed to read from URL: " + url);
            return null;
        }
        return method.getResponseBodyAsStream();
    } catch (final Exception ex) {
        s_logger.error("Failed to read from URL: " + url);
        return null;
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) AuthScope(org.apache.commons.httpclient.auth.AuthScope) GetMethod(org.apache.commons.httpclient.methods.GetMethod) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) URISyntaxException(java.net.URISyntaxException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 43 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project cosmic by MissionCriticalCloud.

the class HTTPUtils method setCredentials.

/**
 * @param username
 * @param password
 * @param httpClient
 */
public static void setCredentials(final String username, final String password, final HttpClient httpClient) {
    if (username != null && password != null && httpClient != null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Setting credentials with username " + username + " for host " + httpClient.getHostConfiguration().getHost() + ":" + httpClient.getHostConfiguration().getPort());
        }
        httpClient.getParams().setAuthenticationPreemptive(true);
        httpClient.getState().setCredentials(new AuthScope(httpClient.getHostConfiguration().getHost(), httpClient.getHostConfiguration().getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password));
    }
}
Also used : AuthScope(org.apache.commons.httpclient.auth.AuthScope) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 44 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project ecf by eclipse.

the class TestHttpState method testScopeMatching.

// ------------------------------- Test Methods for matching Credentials
public void testScopeMatching() {
    AuthScope authscope1 = new AuthScope("somehost", 80, "somerealm", "somescheme");
    AuthScope authscope2 = new AuthScope("someotherhost", 80, "somerealm", "somescheme");
    assertTrue(authscope1.match(authscope2) < 0);
    int m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
    int m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
    assertTrue(m2 > m1);
    m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
    m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
    assertTrue(m2 > m1);
    m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm", "somescheme"));
    m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, 80, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
    assertTrue(m2 > m1);
    m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, 80, "somerealm", "somescheme"));
    m2 = authscope1.match(new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
    assertTrue(m2 > m1);
    m1 = authscope1.match(AuthScope.ANY);
    m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
    assertTrue(m2 > m1);
}
Also used : AuthScope(org.apache.commons.httpclient.auth.AuthScope)

Example 45 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project ecf by eclipse.

the class TestHttpState method testCredentialsMatching.

public void testCredentialsMatching() {
    Credentials creds1 = new UsernamePasswordCredentials("name1", "pass1");
    Credentials creds2 = new UsernamePasswordCredentials("name2", "pass2");
    Credentials creds3 = new UsernamePasswordCredentials("name3", "pass3");
    AuthScope scope1 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
    AuthScope scope2 = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "somerealm");
    AuthScope scope3 = new AuthScope("somehost", AuthScope.ANY_PORT, AuthScope.ANY_REALM);
    HttpState state = new HttpState();
    state.setCredentials(scope1, creds1);
    state.setCredentials(scope2, creds2);
    state.setCredentials(scope3, creds3);
    Credentials got = state.getCredentials(new AuthScope("someotherhost", 80, "someotherrealm", "basic"));
    Credentials expected = creds1;
    assertEquals(expected, got);
    got = state.getCredentials(new AuthScope("someotherhost", 80, "somerealm", "basic"));
    expected = creds2;
    assertEquals(expected, got);
    got = state.getCredentials(new AuthScope("somehost", 80, "someotherrealm", "basic"));
    expected = creds3;
    assertEquals(expected, got);
}
Also used : AuthScope(org.apache.commons.httpclient.auth.AuthScope)

Aggregations

AuthScope (org.apache.commons.httpclient.auth.AuthScope)52 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)34 Credentials (org.apache.commons.httpclient.Credentials)19 GetMethod (org.apache.commons.httpclient.methods.GetMethod)12 HttpClient (org.apache.commons.httpclient.HttpClient)11 URL (java.net.URL)10 IOException (java.io.IOException)5 NTCredentials (org.apache.commons.httpclient.NTCredentials)5 Protocol (org.apache.commons.httpclient.protocol.Protocol)5 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)5 Header (org.apache.commons.httpclient.Header)4 AuthScheme (org.apache.commons.httpclient.auth.AuthScheme)4 AuthState (org.apache.commons.httpclient.auth.AuthState)4 EasySSLProtocolSocketFactory (org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory)4 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)3 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3