Example usage for org.apache.http.impl.client AbstractHttpClient getParams

List of usage examples for org.apache.http.impl.client AbstractHttpClient getParams

Introduction

In this page you can find the example usage for org.apache.http.impl.client AbstractHttpClient getParams.

Prototype

public synchronized final HttpParams getParams() 

Source Link

Usage

From source file:org.eclipse.mylyn.commons.repositories.http.core.HttpUtil.java

public static void configureClient(AbstractHttpClient client, String userAgent) {
    HttpClientParams.setCookiePolicy(client.getParams(), CookiePolicy.BEST_MATCH);

    if (userAgent != null) {
        HttpProtocolParams.setUserAgent(client.getParams(), userAgent);
    }/*from www . j ava 2 s  .  c o m*/
    HttpProtocolParams.setUseExpectContinue(client.getParams(), true);
    client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

    HttpConnectionParams.setConnectionTimeout(client.getParams(), CONNNECT_TIMEOUT);
    HttpConnectionParams.setSoTimeout(client.getParams(), SOCKET_TIMEOUT);

    //AuthParams.setCredentialCharset(client.getParams(), "UTF-8");
}

From source file:org.eclipse.mylyn.commons.http.HttpUtil.java

public static void configureHttpClient(AbstractHttpClient client, String userAgent) {
    client.getParams().setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME,
            SingleConnectionManagerFactory.class.getName());

    client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
    HttpProtocolParams.setUserAgent(client.getParams(), getUserAgent(userAgent));

    //      client.getParams().setLongParameter(AllClientPNames.CONNECTION_TIMEOUT, CONNNECT_TIMEOUT_INTERVAL);

    // TODO consider setting this as the default
    //client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    configureHttpClientConnectionManager(client);
}

From source file:net.adamcin.httpsig.http.apache4.Http4Util.java

public static void enableAuth(final AbstractHttpClient client, final Keychain keychain, final KeyId keyId) {
    if (client == null) {
        throw new NullPointerException("client");
    }/*from w  w w  . ja v a2s.c  om*/

    if (keychain == null) {
        throw new NullPointerException("keychain");
    }

    client.getAuthSchemes().register(Constants.SCHEME, new AuthSchemeFactory() {
        public AuthScheme newInstance(HttpParams params) {
            return new Http4SignatureAuthScheme();
        }
    });

    Signer signer = new Signer(keychain, keyId);
    client.getCredentialsProvider().setCredentials(AuthScope.ANY, new SignerCredentials(signer));
    client.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(Constants.SCHEME));

    HttpClientParams.setAuthenticating(client.getParams(), true);
}

From source file:org.eclipse.mylyn.commons.http.HttpUtil.java

private static void configureHttpClientConnectionManager(AbstractHttpClient client) {

    ClientConnectionManager connectionManager = client.getConnectionManager();

    HttpConnectionParams.setConnectionTimeout(client.getParams(), CONNNECT_TIMEOUT);
    HttpConnectionParams.setSoTimeout(client.getParams(), SOCKET_TIMEOUT);

    if (connectionManager instanceof ThreadSafeClientConnManager) {
        ThreadSafeClientConnManager conMgr = (ThreadSafeClientConnManager) connectionManager;
        // FIXME fix connection leaks
        if (TEST_MODE) {
            conMgr.setDefaultMaxPerRoute(2);
        } else {/*w  w w . j a v  a 2 s.  c o  m*/
            conMgr.setDefaultMaxPerRoute(100);
            conMgr.setMaxTotal(1000);
        }
    }
}

From source file:nl.esciencecenter.octopus.webservice.JobLauncherService.java

/**
 * Adds MAC Access Authentication scheme to http client and registers list of MAC credentials with http client.
 *
 * Http client will use MAC Access Authentication when url is in scope of given MAC credentials.
 *
 * @param httpClient/*from w w  w  .j a  va  2s .  c o m*/
 * @param macs
 * @return httpClient with MAC access authentication and credentials injected.
 */
public static AbstractHttpClient macifyHttpClient(AbstractHttpClient httpClient,
        ImmutableList<MacCredential> macs) {

    // Add MAC scheme
    httpClient.getAuthSchemes().register(MacScheme.SCHEME_NAME, new MacSchemeFactory());

    // Add configured MAC id/key pairs.
    CredentialsProvider credentialProvider = httpClient.getCredentialsProvider();
    for (MacCredential mac : macs) {
        credentialProvider.setCredentials(mac.getAuthScope(), mac);
    }

    // Add MAC scheme to ordered list of supported authentication schemes
    // See HTTP authentication parameters chapter on
    // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
    List<String> authSchemes = Collections.unmodifiableList(Arrays.asList(new String[] { MacScheme.SCHEME_NAME,
            AuthPolicy.SPNEGO, AuthPolicy.KERBEROS, AuthPolicy.NTLM, AuthPolicy.DIGEST, AuthPolicy.BASIC }));
    httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authSchemes);

    return httpClient;
}

From source file:org.eclipse.mylyn.commons.repositories.http.core.HttpUtil.java

public static void configureProxy(AbstractHttpClient client, RepositoryLocation location) {
    Assert.isNotNull(client);//from www  . ja va  2  s .c o  m
    Assert.isNotNull(location);
    String url = location.getUrl();
    Assert.isNotNull(url, "The location url must not be null"); //$NON-NLS-1$

    String host = NetUtil.getHost(url);
    Proxy proxy;
    if (NetUtil.isUrlHttps(url)) {
        proxy = location.getProxyForHost(host, IProxyData.HTTPS_PROXY_TYPE);
    } else {
        proxy = location.getProxyForHost(host, IProxyData.HTTP_PROXY_TYPE);
    }

    if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
        InetSocketAddress address = (InetSocketAddress) proxy.address();

        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                new HttpHost(address.getHostName(), address.getPort()));

        if (proxy instanceof AuthenticatedProxy) {
            AuthenticatedProxy authProxy = (AuthenticatedProxy) proxy;
            Credentials credentials = getCredentials(authProxy.getUserName(), authProxy.getPassword(),
                    address.getAddress(), false);
            if (credentials instanceof NTCredentials) {
                AuthScope proxyAuthScopeNTLM = new AuthScope(address.getHostName(), address.getPort(),
                        AuthScope.ANY_REALM, AuthPolicy.NTLM);
                client.getCredentialsProvider().setCredentials(proxyAuthScopeNTLM, credentials);

                AuthScope proxyAuthScopeAny = new AuthScope(address.getHostName(), address.getPort(),
                        AuthScope.ANY_REALM);
                Credentials usernamePasswordCredentials = getCredentials(authProxy.getUserName(),
                        authProxy.getPassword(), address.getAddress(), true);
                client.getCredentialsProvider().setCredentials(proxyAuthScopeAny, usernamePasswordCredentials);

            } else {
                AuthScope proxyAuthScope = new AuthScope(address.getHostName(), address.getPort(),
                        AuthScope.ANY_REALM);
                client.getCredentialsProvider().setCredentials(proxyAuthScope, credentials);
            }
        }
    } else {
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
    }
}

From source file:org.eclipse.mylyn.commons.http.HttpUtil.java

private static void configureHttpClientProxy(AbstractHttpClient client, HttpContext context,
        AbstractWebLocation location) {/*from ww w . j  a v a2s .  c  o  m*/
    String host = getHost(location.getUrl());

    Proxy proxy;
    if (isRepositoryHttps(location.getUrl())) {
        proxy = location.getProxyForHost(host, IProxyData.HTTPS_PROXY_TYPE);
    } else {
        proxy = location.getProxyForHost(host, IProxyData.HTTP_PROXY_TYPE);
    }

    if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
        InetSocketAddress address = (InetSocketAddress) proxy.address();

        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                new HttpHost(address.getHostName(), address.getPort()));

        if (proxy instanceof AuthenticatedProxy) {
            AuthenticatedProxy authProxy = (AuthenticatedProxy) proxy;
            Credentials credentials = getCredentials(authProxy.getUserName(), authProxy.getPassword(),
                    address.getAddress());
            if (credentials instanceof NTCredentials) {
                List<String> authpref = new ArrayList<String>();
                authpref.add(AuthPolicy.NTLM);
                authpref.add(AuthPolicy.BASIC);
                authpref.add(AuthPolicy.DIGEST);
                client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);
            } else {
                List<String> authpref = new ArrayList<String>();
                authpref.add(AuthPolicy.BASIC);
                authpref.add(AuthPolicy.DIGEST);
                authpref.add(AuthPolicy.NTLM);
                client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref);
            }
            AuthScope proxyAuthScope = new AuthScope(address.getHostName(), address.getPort(),
                    AuthScope.ANY_REALM);
            client.getCredentialsProvider().setCredentials(proxyAuthScope, credentials);
        }
    } else {
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
    }
}

From source file:org.jasig.portlet.proxy.service.web.MultiRequestHttpClientServiceImpl.java

private AbstractHttpClient setHttpClientTimeouts(PortletRequest request, AbstractHttpClient client) {
    PortletPreferences prefs = request.getPreferences();
    HttpParams params = client.getParams();
    if (params == null) {
        params = new BasicHttpParams();
        client.setParams(params);//from  www.java  2  s.  c o  m
    }
    /*
     * The connection is attempted 5 times prior to stopping
     * so the actual time before failure will be 5 times this setting.
     * Suggested way of testing Connection Timeout is by hitting a
     * domain with a port that is firewalled:
     * ie. http://www.google.com:81
     */
    int httpClientConnectionTimeout = Integer.parseInt(prefs.getValue(HTTP_CLIENT_CONNECTION_TIMEOUT,
            String.valueOf(DEFAULT_HTTP_CLIENT_CONNECTION_TIMEOUT)));
    /*
     * Suggested way of testing Socket Timeout is by using a tool locally to connect
     * but not respond.  Example tool: bane
     * http://blog.danielwellman.com/2010/09/introducing-bane-a-test-harness-for-server-connections.html
     * usage: $bane 10010 NeverRespond
     * ie. http://localhost:10010
     */
    int httpClientSocketTimeout = Integer.parseInt(
            prefs.getValue(HTTP_CLIENT_SOCKET_TIMEOUT, String.valueOf(DEFAULT_HTTP_CLIENT_SOCKET_TIMEOUT)));
    HttpConnectionParams.setConnectionTimeout(params, httpClientConnectionTimeout);
    HttpConnectionParams.setSoTimeout(params, httpClientSocketTimeout);
    return client;
}

From source file:org.springframework.data.solr.server.support.HttpSolrServerFactory.java

private void appendAuthentication(Credentials credentials, String authPolicy, SolrServer solrServer) {
    if (isHttpSolrServer(solrServer)) {
        HttpSolrServer httpSolrServer = (HttpSolrServer) solrServer;

        if (credentials != null && StringUtils.isNotBlank(authPolicy)
                && assertHttpClientInstance(httpSolrServer.getHttpClient())) {
            AbstractHttpClient httpClient = (AbstractHttpClient) httpSolrServer.getHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials);
            httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy));
        }//w  w w. j a  v a 2 s.  c  om
    }
}

From source file:com.frank.search.solr.server.support.HttpSolrClientFactory.java

private void appendAuthentication(Credentials credentials, String authPolicy, SolrClient solrClient) {
    if (isHttpSolrClient(solrClient)) {
        HttpSolrClient httpSolrClient = (HttpSolrClient) solrClient;

        if (credentials != null && StringUtils.isNotBlank(authPolicy)
                && assertHttpClientInstance(httpSolrClient.getHttpClient())) {
            AbstractHttpClient httpClient = (AbstractHttpClient) httpSolrClient.getHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials);
            httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy));
        }//from  w  w w  .j  a v a  2s .  c  om
    }
}