Example usage for org.apache.http.client.params ClientParamBean ClientParamBean

List of usage examples for org.apache.http.client.params ClientParamBean ClientParamBean

Introduction

In this page you can find the example usage for org.apache.http.client.params ClientParamBean ClientParamBean.

Prototype

public ClientParamBean(final HttpParams params) 

Source Link

Usage

From source file:com.rogiel.httpchannel.http.HttpContext.java

public HttpContext() {
    // default configuration
    params = new ClientParamBean(client.getParams());
    params.setHandleRedirects(true);//from   ww w  .  j av a 2s . co m
    params.setAllowCircularRedirects(true);
    params.setRejectRelativeRedirect(false);
    params.setMaxRedirects(10);

    // browser behavior
    client.setRedirectStrategy(new DefaultRedirectStrategy() {
        @Override
        public boolean isRedirected(HttpRequest request, HttpResponse response,
                org.apache.http.protocol.HttpContext context) throws ProtocolException {
            return response.containsHeader("Location");
        }
    });
}

From source file:com.googlecode.noweco.webmail.portal.bull.BullWebmailPortalConnector.java

public PortalConnection connect(final HttpHost proxy, final String user, final String password)
        throws IOException {
    DefaultHttpClient httpclient = UnsecureHttpClientFactory.INSTANCE.createUnsecureHttpClient(proxy);
    HttpPost httpost;//from  w  w w .  j a v  a  2s . com
    List<NameValuePair> nvps;
    HttpResponse rsp;
    int statusCode;
    HttpEntity entity;

    // mailbox does not appear with no FR language
    // with Mozilla actions are simple
    new ClientParamBean(httpclient.getParams()).setDefaultHeaders(Arrays.asList(
            (Header) new BasicHeader("Accept-Language", "fr-fr,fr;q=0.8,en;q=0.5,en-us;q=0.3"),
            new BasicHeader("User-Agent",
                    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1")));

    String firstEntity = authent(httpclient, user, password);
    Matcher datasMatcher = DATAS.matcher(firstEntity);
    if (!datasMatcher.find()) {
        Matcher matcher = NEW_PASSWD.matcher(firstEntity);
        if (matcher.find()) {
            // try a bad password
            authent(httpclient, user, password + "BAD");
            // and retry good password (expired)
            firstEntity = authent(httpclient, user, password);
            datasMatcher = DATAS.matcher(firstEntity);
            if (!datasMatcher.find()) {
                throw new IOException("Unable to find Datas, after bad password try");
            }
        } else {
            throw new IOException("Unable to find Datas");
        }
    }

    // STEP 2 : WEB-MAIL

    httpost = new HttpPost("https://bullsentry.bull.fr:443/cgi/wway_cookie");
    nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("TdsName", "PILX"));
    nvps.add(new BasicNameValuePair("Proto", "s"));
    nvps.add(new BasicNameValuePair("Port", "443"));
    nvps.add(new BasicNameValuePair("Cgi", "cgi"));
    nvps.add(new BasicNameValuePair("Mode", "set"));
    nvps.add(new BasicNameValuePair("Total", "3"));
    nvps.add(new BasicNameValuePair("Current", "1"));
    nvps.add(new BasicNameValuePair("ProtoR", "s"));
    nvps.add(new BasicNameValuePair("PortR", "443"));
    nvps.add(new BasicNameValuePair("WebAgt", "1"));
    nvps.add(new BasicNameValuePair("UrlConnect", "https://telemail.bull.fr:443/"));
    nvps.add(new BasicNameValuePair("Service", "WEB-MAIL"));
    nvps.add(new BasicNameValuePair("Datas", datasMatcher.group(1)));

    Matcher lastCnxMatcher = LAST_CNX.matcher(firstEntity);
    if (!lastCnxMatcher.find()) {
        throw new IOException("Unable to find lastCnx");
    }
    nvps.add(new BasicNameValuePair("lastCnx", lastCnxMatcher.group(1)));
    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    // send the request
    rsp = httpclient.execute(httpost);
    statusCode = rsp.getStatusLine().getStatusCode();
    if (statusCode != 200) {
        throw new IOException("Unable to connect to bull portail, status code : " + statusCode);
    }

    if (LOGGER.isTraceEnabled()) {
        LOGGER.debug("STEP 2 Apps Cookie : {}", httpclient.getCookieStore().getCookies());
    }

    // free result resources
    entity = rsp.getEntity();
    if (entity != null) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("STEP 2 Entity : {}", EntityUtils.toString(entity));
        }
        EntityUtils.consume(entity);
    }

    // STEP 3 : telemail

    HttpGet httpGet = new HttpGet("https://telemail.bull.fr:443/HomePage.nsf");
    rsp = httpclient.execute(httpGet);

    entity = rsp.getEntity();
    String secondEntity = EntityUtils.toString(entity);
    if (entity != null) {
        LOGGER.trace("STEP 3 Entity : {}", secondEntity);
        EntityUtils.consume(entity);
    }

    Matcher nsfMatcher = PATTERN.matcher(secondEntity);
    if (!nsfMatcher.find()) {
        throw new IOException("Unable to find nsf");
    }

    String pathPrefix = nsfMatcher.group(3).replace('\\', '/');
    LOGGER.debug("pathPrefix : {}", pathPrefix);

    String protocol = nsfMatcher.group(1);
    int port;
    if ("http".equals(protocol)) {
        port = 80;
    } else if ("https".equals(protocol)) {
        port = 443;
    } else {
        throw new IOException("Unknown protocol " + protocol);
    }
    return new DefaultPortalConnection(httpclient, new HttpHost(nsfMatcher.group(2), port, protocol),
            pathPrefix);
}

From source file:com.su.search.client.solrj.PaHttpSolrServer.java

/**
 * HttpClientParams.setRedirecting//from  w  w w . j  a  va  2s  . co m
 * 
 * @see #followRedirects
 */
public void setFollowRedirects(boolean followRedirects) {
    this.followRedirects = followRedirects;
    new ClientParamBean(httpClient.getParams()).setHandleRedirects(followRedirects);
}

From source file:com.heaptrip.util.http.bixo.fetcher.SimpleHttpFetcher.java

private synchronized void init() {
    if (_httpClient == null) {
        // Create and initialize HTTP parameters
        HttpParams params = new BasicHttpParams();

        // TODO KKr - w/4.1, switch to new api (ThreadSafeClientConnManager)
        // cm.setMaxTotalConnections(_maxThreads);
        // cm.setDefaultMaxPerRoute(Math.max(10, _maxThreads/10));
        ConnManagerParams.setMaxTotalConnections(params, _maxThreads);

        // Set the maximum time we'll wait for a spare connection in the
        // connection pool. We
        // shouldn't actually hit this, as we make sure (in FetcherManager) that
        // the max number
        // of active requests doesn't exceed the value returned by getMaxThreads()
        // here./*w w  w.  j  a v a2  s . c  o  m*/
        ConnManagerParams.setTimeout(params, CONNECTION_POOL_TIMEOUT);

        // Set the socket and connection timeout to be something reasonable.
        HttpConnectionParams.setSoTimeout(params, _socketTimeout);
        HttpConnectionParams.setConnectionTimeout(params, _connectionTimeout);

        // Even with stale checking enabled, a connection can "go stale" between
        // the check and the
        // next request. So we still need to handle the case of a closed socket
        // (from the server side),
        // and disabling this check improves performance.
        HttpConnectionParams.setStaleCheckingEnabled(params, false);

        // FUTURE - set this on a per-route (host) basis when we have per-host
        // policies for
        // doing partner crawls. We could define a BixoConnPerRoute class that
        // supports this.
        ConnPerRouteBean connPerRoute = new ConnPerRouteBean(_fetcherPolicy.getMaxConnectionsPerHost());
        ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);

        HttpProtocolParams.setVersion(params, _httpVersion);
        HttpProtocolParams.setUserAgent(params, _userAgent.getUserAgentString());
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        HttpProtocolParams.setHttpElementCharset(params, "UTF-8");
        HttpProtocolParams.setUseExpectContinue(params, true);

        // TODO KKr - set on connection manager params, or client params?
        CookieSpecParamBean cookieParams = new CookieSpecParamBean(params);
        cookieParams.setSingleHeader(true);

        // Create and initialize scheme registry
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        SSLSocketFactory sf = null;

        if (sf != null) {
            sf.setHostnameVerifier(new DummyX509HostnameVerifier());
            schemeRegistry.register(new Scheme("https", sf, 443));
        } else {
            LOGGER.warn("No valid SSLContext found for https");
        }

        // Use ThreadSafeClientConnManager since more than one thread will be
        // using the HttpClient.
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        _httpClient = new DefaultHttpClient(cm, params);
        _httpClient.setHttpRequestRetryHandler(new MyRequestRetryHandler(_maxRetryCount));
        _httpClient.setRedirectHandler(new MyRedirectHandler(_fetcherPolicy.getRedirectMode()));
        _httpClient.addRequestInterceptor(new MyRequestInterceptor());

        params = _httpClient.getParams();
        // FUTURE KKr - support authentication
        HttpClientParams.setAuthenticating(params, false);
        HttpClientParams.setCookiePolicy(params, CookiePolicy.BEST_MATCH);

        ClientParamBean clientParams = new ClientParamBean(params);
        if (_fetcherPolicy.getMaxRedirects() == 0) {
            clientParams.setHandleRedirects(false);
        } else {
            clientParams.setHandleRedirects(true);
            clientParams.setMaxRedirects(_fetcherPolicy.getMaxRedirects());
        }

        // Set up default headers. This helps us get back from servers what we
        // want.
        HashSet<Header> defaultHeaders = new HashSet<Header>();
        defaultHeaders
                .add(new BasicHeader(HttpHeaderNames.ACCEPT_LANGUAGE, _fetcherPolicy.getAcceptLanguage()));
        defaultHeaders.add(new BasicHeader(HttpHeaderNames.ACCEPT_CHARSET, DEFAULT_ACCEPT_CHARSET));
        defaultHeaders.add(new BasicHeader(HttpHeaderNames.ACCEPT, DEFAULT_ACCEPT));

        clientParams.setDefaultHeaders(defaultHeaders);
    }
}

From source file:bixo.fetcher.SimpleHttpFetcher.java

private synchronized void init() {
    if (_httpClient == null) {
        // Create and initialize HTTP parameters
        HttpParams params = new BasicHttpParams();

        // TODO KKr - w/4.1, switch to new api (ThreadSafeClientConnManager)
        // cm.setMaxTotalConnections(_maxThreads);
        // cm.setDefaultMaxPerRoute(Math.max(10, _maxThreads/10));
        ConnManagerParams.setMaxTotalConnections(params, _maxThreads);

        // Set the maximum time we'll wait for a spare connection in the
        // connection pool. We
        // shouldn't actually hit this, as we make sure (in FetcherManager) that
        // the max number
        // of active requests doesn't exceed the value returned by getMaxThreads()
        // here.//  w  ww.  j  a v a 2  s. c  o  m
        ConnManagerParams.setTimeout(params, CONNECTION_POOL_TIMEOUT);

        // Set the socket and connection timeout to be something reasonable.
        HttpConnectionParams.setSoTimeout(params, _socketTimeout);
        HttpConnectionParams.setConnectionTimeout(params, _connectionTimeout);

        // Even with stale checking enabled, a connection can "go stale" between
        // the check and the
        // next request. So we still need to handle the case of a closed socket
        // (from the server side),
        // and disabling this check improves performance.
        HttpConnectionParams.setStaleCheckingEnabled(params, false);

        // FUTURE - set this on a per-route (host) basis when we have per-host
        // policies for
        // doing partner crawls. We could define a BixoConnPerRoute class that
        // supports this.
        ConnPerRouteBean connPerRoute = new ConnPerRouteBean(_fetcherPolicy.getMaxConnectionsPerHost());
        ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);

        HttpProtocolParams.setVersion(params, _httpVersion);
        HttpProtocolParams.setUserAgent(params, _userAgent.getUserAgentString());
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        HttpProtocolParams.setHttpElementCharset(params, "UTF-8");
        HttpProtocolParams.setUseExpectContinue(params, true);

        // TODO KKr - set on connection manager params, or client params?
        CookieSpecParamBean cookieParams = new CookieSpecParamBean(params);
        cookieParams.setSingleHeader(true);

        // Create and initialize scheme registry
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        SSLSocketFactory sf = null;

        for (String contextName : SSL_CONTEXT_NAMES) {
            try {
                SSLContext sslContext = SSLContext.getInstance(contextName);
                sslContext.init(null, new TrustManager[] { new DummyX509TrustManager(null) }, null);
                sf = new SSLSocketFactory(sslContext);
                break;
            } catch (NoSuchAlgorithmException e) {
                LOGGER.debug("SSLContext algorithm not available: " + contextName);
            } catch (Exception e) {
                LOGGER.debug("SSLContext can't be initialized: " + contextName, e);
            }
        }

        if (sf != null) {
            sf.setHostnameVerifier(new DummyX509HostnameVerifier());
            schemeRegistry.register(new Scheme("https", sf, 443));
        } else {
            LOGGER.warn("No valid SSLContext found for https");
        }

        // Use ThreadSafeClientConnManager since more than one thread will be
        // using the HttpClient.
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        _httpClient = new DefaultHttpClient(cm, params);
        _httpClient.setHttpRequestRetryHandler(new MyRequestRetryHandler(_maxRetryCount));
        _httpClient.setRedirectHandler(new MyRedirectHandler(_fetcherPolicy.getRedirectMode()));
        _httpClient.addRequestInterceptor(new MyRequestInterceptor());

        params = _httpClient.getParams();
        // FUTURE KKr - support authentication
        HttpClientParams.setAuthenticating(params, false);
        HttpClientParams.setCookiePolicy(params, CookiePolicy.BEST_MATCH);

        ClientParamBean clientParams = new ClientParamBean(params);
        if (_fetcherPolicy.getMaxRedirects() == 0) {
            clientParams.setHandleRedirects(false);
        } else {
            clientParams.setHandleRedirects(true);
            clientParams.setMaxRedirects(_fetcherPolicy.getMaxRedirects());
        }

        // Set up default headers. This helps us get back from servers what we
        // want.
        HashSet<Header> defaultHeaders = new HashSet<Header>();
        defaultHeaders
                .add(new BasicHeader(HttpHeaderNames.ACCEPT_LANGUAGE, _fetcherPolicy.getAcceptLanguage()));
        defaultHeaders.add(new BasicHeader(HttpHeaderNames.ACCEPT_CHARSET, DEFAULT_ACCEPT_CHARSET));
        defaultHeaders.add(new BasicHeader(HttpHeaderNames.ACCEPT, DEFAULT_ACCEPT));

        clientParams.setDefaultHeaders(defaultHeaders);
    }
}

From source file:crawlercommons.fetcher.SimpleHttpFetcher.java

private synchronized void init() {
    if (_httpClient == null) {
        // Create and initialize HTTP parameters
        HttpParams params = new BasicHttpParams();

        // TODO KKr - w/4.1, switch to new api (ThreadSafeClientConnManager)
        // cm.setMaxTotalConnections(_maxThreads);
        // cm.setDefaultMaxPerRoute(Math.max(10, _maxThreads/10));
        ConnManagerParams.setMaxTotalConnections(params, _maxThreads);

        // Set the maximum time we'll wait for a spare connection in the connection pool. We
        // shouldn't actually hit this, as we make sure (in FetcherManager) that the max number
        // of active requests doesn't exceed the value returned by getMaxThreads() here.
        ConnManagerParams.setTimeout(params, CONNECTION_POOL_TIMEOUT);

        // Set the socket and connection timeout to be something reasonable.
        HttpConnectionParams.setSoTimeout(params, _socketTimeout);
        HttpConnectionParams.setConnectionTimeout(params, _connectionTimeout);

        // Even with stale checking enabled, a connection can "go stale" between the check and the
        // next request. So we still need to handle the case of a closed socket (from the server side),
        // and disabling this check improves performance.
        HttpConnectionParams.setStaleCheckingEnabled(params, false);

        // FUTURE - set this on a per-route (host) basis when we have per-host policies for
        // doing partner crawls. We could define a BixoConnPerRoute class that supports this.
        ConnPerRouteBean connPerRoute = new ConnPerRouteBean(getMaxConnectionsPerHost());
        ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);

        HttpProtocolParams.setVersion(params, _httpVersion);
        HttpProtocolParams.setUserAgent(params, _userAgent.getUserAgentString());
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        HttpProtocolParams.setHttpElementCharset(params, "UTF-8");
        HttpProtocolParams.setUseExpectContinue(params, true);

        // TODO KKr - set on connection manager params, or client params?
        CookieSpecParamBean cookieParams = new CookieSpecParamBean(params);
        cookieParams.setSingleHeader(true);

        // Create and initialize scheme registry
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        SSLSocketFactory sf = null;

        for (String contextName : SSL_CONTEXT_NAMES) {
            try {
                SSLContext sslContext = SSLContext.getInstance(contextName);
                sslContext.init(null, new TrustManager[] { new DummyX509TrustManager(null) }, null);
                sf = new SSLSocketFactory(sslContext);
                break;
            } catch (NoSuchAlgorithmException e) {
                LOGGER.debug("SSLContext algorithm not available: " + contextName);
            } catch (Exception e) {
                LOGGER.debug("SSLContext can't be initialized: " + contextName, e);
            }/*from w ww  . j av a  2  s  .  c  o  m*/
        }

        if (sf != null) {
            sf.setHostnameVerifier(new DummyX509HostnameVerifier());
            schemeRegistry.register(new Scheme("https", sf, 443));
        } else {
            LOGGER.warn("No valid SSLContext found for https");
        }

        // Use ThreadSafeClientConnManager since more than one thread will be using the HttpClient.
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
        _httpClient = new DefaultHttpClient(cm, params);
        _httpClient.setHttpRequestRetryHandler(new MyRequestRetryHandler(_maxRetryCount));
        _httpClient.setRedirectHandler(new MyRedirectHandler(getRedirectMode()));
        _httpClient.addRequestInterceptor(new MyRequestInterceptor());

        params = _httpClient.getParams();
        // FUTURE KKr - support authentication
        HttpClientParams.setAuthenticating(params, false);
        HttpClientParams.setCookiePolicy(params, CookiePolicy.BEST_MATCH);

        ClientParamBean clientParams = new ClientParamBean(params);
        if (getMaxRedirects() == 0) {
            clientParams.setHandleRedirects(false);
        } else {
            clientParams.setHandleRedirects(true);
            clientParams.setMaxRedirects(getMaxRedirects());
        }

        // Set up default headers. This helps us get back from servers what we want.
        HashSet<Header> defaultHeaders = new HashSet<Header>();
        defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, getAcceptLanguage()));
        defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, DEFAULT_ACCEPT_CHARSET));
        defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, DEFAULT_ACCEPT_ENCODING));
        defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT, DEFAULT_ACCEPT));

        clientParams.setDefaultHeaders(defaultHeaders);
    }
}

From source file:org.apache.camel.component.http4.HttpComponent.java

protected HttpParams configureHttpParams(Map<String, Object> parameters) throws Exception {
    HttpParams clientParams = new BasicHttpParams();

    AuthParamBean authParamBean = new AuthParamBean(clientParams);
    IntrospectionSupport.setProperties(authParamBean, parameters, "httpClient.");

    ClientParamBean clientParamBean = new ClientParamBean(clientParams);
    IntrospectionSupport.setProperties(clientParamBean, parameters, "httpClient.");

    ConnConnectionParamBean connConnectionParamBean = new ConnConnectionParamBean(clientParams);
    IntrospectionSupport.setProperties(connConnectionParamBean, parameters, "httpClient.");

    ConnManagerParamBean connManagerParamBean = new ConnManagerParamBean(clientParams);
    IntrospectionSupport.setProperties(connManagerParamBean, parameters, "httpClient.");

    ConnRouteParamBean connRouteParamBean = new ConnRouteParamBean(clientParams);
    IntrospectionSupport.setProperties(connRouteParamBean, parameters, "httpClient.");

    CookieSpecParamBean cookieSpecParamBean = new CookieSpecParamBean(clientParams);
    IntrospectionSupport.setProperties(cookieSpecParamBean, parameters, "httpClient.");

    HttpConnectionParamBean httpConnectionParamBean = new HttpConnectionParamBean(clientParams);
    IntrospectionSupport.setProperties(httpConnectionParamBean, parameters, "httpClient.");

    HttpProtocolParamBean httpProtocolParamBean = new HttpProtocolParamBean(clientParams);
    IntrospectionSupport.setProperties(httpProtocolParamBean, parameters, "httpClient.");

    return clientParams;
}