Search in sources :

Example 6 with AuthScope

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();
    }
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) RepositoryException(org.apache.sling.ide.transport.RepositoryException) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 7 with AuthScope

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) RepositoryException(org.apache.sling.ide.transport.RepositoryException) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Example 8 with AuthScope

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);
    }
}
Also used : AuthScope(org.apache.commons.httpclient.auth.AuthScope) ProtocolException(org.apache.nutch.protocol.ProtocolException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) NTCredentials(org.apache.commons.httpclient.NTCredentials)

Example 9 with AuthScope

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();
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) AuthScope(org.apache.commons.httpclient.auth.AuthScope) GetMethod(org.apache.commons.httpclient.methods.GetMethod) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 10 with AuthScope

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