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);
}
}
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;
}
}
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));
}
}
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);
}
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);
}
Aggregations