Search in sources :

Example 36 with AuthScope

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

the class Http method configureClient.

/**
 * Configures the HTTP client
 */
private void configureClient() {
    // Set up an HTTPS socket factory that accepts self-signed certs.
    // ProtocolSocketFactory factory = new SSLProtocolSocketFactory();
    ProtocolSocketFactory factory = new DummySSLProtocolSocketFactory();
    Protocol https = new Protocol("https", factory, 443);
    Protocol.registerProtocol("https", https);
    HttpConnectionManagerParams params = connectionManager.getParams();
    params.setConnectionTimeout(timeout);
    params.setSoTimeout(timeout);
    params.setSendBufferSize(BUFFER_SIZE);
    params.setReceiveBufferSize(BUFFER_SIZE);
    // --------------------------------------------------------------------------------
    // NUTCH-1836: Modification to increase the number of available connections
    // for multi-threaded crawls.
    // --------------------------------------------------------------------------------
    params.setMaxTotalConnections(conf.getInt("mapred.tasktracker.map.tasks.maximum", 5) * conf.getInt("fetcher.threads.fetch", maxThreadsTotal));
    // Also set max connections per host to maxThreadsTotal since all threads
    // might be used to fetch from the same host - otherwise timeout errors can
    // occur
    params.setDefaultMaxConnectionsPerHost(conf.getInt("fetcher.threads.fetch", maxThreadsTotal));
    // executeMethod(HttpMethod) seems to ignore the connection timeout on the
    // connection manager.
    // set it explicitly on the HttpClient.
    client.getParams().setConnectionManagerTimeout(timeout);
    HostConfiguration hostConf = client.getHostConfiguration();
    ArrayList<Header> headers = new ArrayList<Header>();
    // Note: some header fields (e.g., "User-Agent") are set per GET request
    if (!acceptLanguage.isEmpty()) {
        headers.add(new Header("Accept-Language", acceptLanguage));
    }
    if (!acceptCharset.isEmpty()) {
        headers.add(new Header("Accept-Charset", acceptCharset));
    }
    if (!accept.isEmpty()) {
        headers.add(new Header("Accept", accept));
    }
    // accept gzipped content
    headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
    hostConf.getParams().setParameter("http.default-headers", headers);
    // HTTP proxy server details
    if (useProxy) {
        hostConf.setProxy(proxyHost, proxyPort);
        if (proxyUsername.length() > 0) {
            AuthScope proxyAuthScope = getAuthScope(this.proxyHost, this.proxyPort, this.proxyRealm);
            NTCredentials proxyCredentials = new NTCredentials(this.proxyUsername, this.proxyPassword, Http.agentHost, this.proxyRealm);
            client.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
        }
    }
}
Also used : ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) Header(org.apache.commons.httpclient.Header) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) ArrayList(java.util.ArrayList) AuthScope(org.apache.commons.httpclient.auth.AuthScope) Protocol(org.apache.commons.httpclient.protocol.Protocol) NTCredentials(org.apache.commons.httpclient.NTCredentials)

Example 37 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project xwiki-platform by xwiki.

the class XWiki method getURLContentAsBytes.

public byte[] getURLContentAsBytes(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.getResponseBody();
    } 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 38 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project alfresco-remote-api by Alfresco.

the class AuthenticatedHttp method executeWithBasicAuthentication.

/**
 * Execute the given method, authenticated as the given user using Basic Authentication.
 * @param method method to execute
 * @param userName name of user to authenticate (note: if null then attempts to run with no authentication - eq. Quick/Shared Link test)
 * @param callback called after http-call is executed. When callback returns, the
 *  response stream is closed, so all respose-related operations should be done in the callback. Can be null.
 * @return result returned by the callback or null if no callback is given.
 */
private <T extends Object> T executeWithBasicAuthentication(HttpMethod method, String userName, String password, HttpRequestCallback<T> callback) {
    try {
        HttpState state = new HttpState();
        if (userName != null) {
            state.setCredentials(new AuthScope(null, AuthScope.ANY_PORT), new UsernamePasswordCredentials(userName, password));
        }
        httpProvider.getHttpClient().executeMethod(null, method, state);
        if (callback != null) {
            return callback.onCallSuccess(method);
        }
        // No callback used, return null
        return null;
    } catch (Throwable t) {
        boolean handled = false;
        // Delegate to callback to handle error. If not available, throw exception
        if (callback != null) {
            handled = callback.onError(method, t);
        }
        if (!handled) {
            throw new RuntimeException("Error while executing HTTP-call (" + method.getPath() + ")", t);
        }
        return null;
    } finally {
        method.releaseConnection();
    }
}
Also used : HttpState(org.apache.commons.httpclient.HttpState) AuthScope(org.apache.commons.httpclient.auth.AuthScope) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 39 with AuthScope

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

the class ServicesConnection method makeClient.

public HttpClient makeClient(CSPRequestCredentials creds, CSPRequestCache cache) {
    // Check request cache
    HttpClient client = (HttpClient) cache.getCached(getClass(), new String[] { "client" });
    if (client != null)
        return client;
    client = new HttpClient(manager);
    client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials((String) creds.getCredential(ServicesStorageGenerator.CRED_USERID), (String) creds.getCredential(ServicesStorageGenerator.CRED_PASSWORD)));
    client.getParams().setAuthenticationPreemptive(true);
    cache.setCached(getClass(), new String[] { "client" }, client);
    return client;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) AuthScope(org.apache.commons.httpclient.auth.AuthScope) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 40 with AuthScope

use of org.apache.commons.httpclient.auth.AuthScope in project teamcity-torrent-plugin by JetBrains.

the class TorrentTransportFactory method createHttpClient.

private HttpClient createHttpClient() {
    String userName = myBuildTracker.getCurrentBuild().getAccessUser();
    String password = myBuildTracker.getCurrentBuild().getAccessCode();
    int connectionTimeout = 60;
    HttpClient client = HttpUtil.createHttpClient(connectionTimeout);
    client.getParams().setAuthenticationPreemptive(true);
    Credentials defaultcreds = new UsernamePasswordCredentials(userName, password);
    client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), defaultcreds);
    String proxyHost = myAgentConfig.getServerProxyHost();
    if (proxyHost != null) {
        HttpUtil.configureProxy(client, proxyHost, myAgentConfig.getServerProxyPort(), myAgentConfig.getServerProxyCredentials());
    }
    return client;
}
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