use of org.apache.commons.httpclient.auth.AuthScope in project sling by apache.
the class GetNodeCommand method execute.
@Override
public Result<byte[]> execute() {
GetMethod get = new GetMethod(getPath());
try {
httpClient.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword());
//TODO
httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(), repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds);
int responseStatus = httpClient.executeMethod(get);
if (isSuccessStatus(responseStatus))
return AbstractResult.success(get.getResponseBody());
return failureResultForStatusCode(responseStatus);
} catch (Exception e) {
return AbstractResult.failure(new RepositoryException(e));
} finally {
get.releaseConnection();
}
}
use of org.apache.commons.httpclient.auth.AuthScope in project sling by apache.
the class ListChildrenCommand method execute.
@Override
public Result<ResourceProxy> execute() {
GetMethod get = new GetMethod(getPath());
try {
httpClient.getParams().setAuthenticationPreemptive(true);
Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword());
httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(), repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds);
int responseStatus = httpClient.executeMethod(get);
//return EncodingUtil.getString(rawdata, m.getResponseCharSet());
if (!isSuccessStatus(responseStatus))
return failureResultForStatusCode(responseStatus);
ResourceProxy resource = new ResourceProxy(path);
Gson gson = new Gson();
try (JsonReader jsonReader = new JsonReader(new InputStreamReader(get.getResponseBodyAsStream(), get.getResponseCharSet()))) {
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
JsonToken token = jsonReader.peek();
if (token == JsonToken.BEGIN_OBJECT) {
ResourceProxy child = new ResourceProxy(PathUtil.join(path, name));
ResourceWithPrimaryType resourceWithPrimaryType = gson.fromJson(jsonReader, ResourceWithPrimaryType.class);
// evaluate its jcr:primaryType as well!
child.addProperty(Repository.JCR_PRIMARY_TYPE, resourceWithPrimaryType.getPrimaryType());
resource.addChild(child);
} else if (token == JsonToken.STRING) {
if (Repository.JCR_PRIMARY_TYPE.equals(name)) {
String primaryType = jsonReader.nextString();
if (primaryType != null) {
// TODO - needed?
resource.addProperty(Repository.JCR_PRIMARY_TYPE, primaryType);
}
}
} else {
jsonReader.skipValue();
}
}
jsonReader.endObject();
}
return AbstractResult.success(resource);
} catch (Exception e) {
return AbstractResult.failure(new RepositoryException(e));
} finally {
get.releaseConnection();
}
}
use of org.apache.commons.httpclient.auth.AuthScope in project nutch by apache.
the class Http method resolveCredentials.
/**
* If credentials for the authentication scope determined from the specified
* <code>url</code> is not already set in the HTTP client, then this method
* sets the default credentials to fetch the specified <code>url</code>. If
* credentials are found for the authentication scope, the method returns
* without altering the client.
*
* @param url
* URL to be fetched
*/
private void resolveCredentials(URL url) {
if (formConfigurer != null) {
HttpFormAuthentication formAuther = new HttpFormAuthentication(formConfigurer, client, this);
try {
formAuther.login();
} catch (Exception e) {
throw new RuntimeException(e);
}
return;
}
if (defaultUsername != null && defaultUsername.length() > 0) {
int port = url.getPort();
if (port == -1) {
if ("https".equals(url.getProtocol()))
port = 443;
else
port = 80;
}
AuthScope scope = new AuthScope(url.getHost(), port);
if (client.getState().getCredentials(scope) != null) {
if (LOG.isTraceEnabled())
LOG.trace("Pre-configured credentials with scope - host: " + url.getHost() + "; port: " + port + "; found for url: " + url);
// Credentials are already configured, so do nothing and return
return;
}
if (LOG.isTraceEnabled())
LOG.trace("Pre-configured credentials with scope - host: " + url.getHost() + "; port: " + port + "; not found for url: " + url);
AuthScope serverAuthScope = getAuthScope(url.getHost(), port, defaultRealm, defaultScheme);
NTCredentials serverCredentials = new NTCredentials(defaultUsername, defaultPassword, agentHost, defaultRealm);
client.getState().setCredentials(serverAuthScope, serverCredentials);
}
}
use of org.apache.commons.httpclient.auth.AuthScope in project xwiki-platform by xwiki.
the class XWiki method getURLContent.
public String getURLContent(String surl, String username, String password, int timeout, String userAgent) throws IOException {
HttpClient client = getHttpClient(timeout, userAgent);
// pass our credentials to HttpClient, they will only be used for
// authenticating to servers with realm "realm", to authenticate agains
// an arbitrary realm change this to null.
client.getState().setCredentials(new AuthScope(null, -1, null), new UsernamePasswordCredentials(username, password));
// create a GET method that reads a file over HTTPS, we're assuming
// that this file requires basic authentication using the realm above.
GetMethod get = new GetMethod(surl);
try {
// Tell the GET method to automatically handle authentication. The
// method will use any appropriate credentials to handle basic
// authentication requests. Setting this value to false will cause
// any request for authentication to return with a status of 401.
// It will then be up to the client to handle the authentication.
get.setDoAuthentication(true);
// execute the GET
client.executeMethod(get);
// print the status and response
return get.getResponseBodyAsString();
} finally {
// release any connection resources used by the method
get.releaseConnection();
}
}
use of org.apache.commons.httpclient.auth.AuthScope in project ecf by eclipse.
the class TestHttpState method testWrongHostCredentials.
public void testWrongHostCredentials() throws Exception {
HttpState state = new HttpState();
Credentials expected = new UsernamePasswordCredentials("name", "pass");
state.setCredentials(new AuthScope("host1", AuthScope.ANY_PORT, "realm"), expected);
Credentials got = state.getCredentials(new AuthScope("host2", AuthScope.ANY_PORT, "realm"));
assertNotSame(expected, got);
}
Aggregations