Example usage for org.apache.http.params CoreConnectionPNames CONNECTION_TIMEOUT

List of usage examples for org.apache.http.params CoreConnectionPNames CONNECTION_TIMEOUT

Introduction

In this page you can find the example usage for org.apache.http.params CoreConnectionPNames CONNECTION_TIMEOUT.

Prototype

String CONNECTION_TIMEOUT

To view the source code for org.apache.http.params CoreConnectionPNames CONNECTION_TIMEOUT.

Click Source Link

Usage

From source file:yin.autoflowcontrol.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    params.setBooleanParameter("http.protocol.handle-redirects", false);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isIncludeHttpsPages()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }// w  ww .  ja va2  s  . c om

    connectionManager = new ThreadSafeClientConnManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}

From source file:com.github.seratch.signedrequest4j.SignedRequestApacheHCImpl.java

@Override
public HttpResponse doRequest(String url, HttpMethod method, RequestBody body, String charset)
        throws IOException {

    HttpClient httpClient = new DefaultHttpClient();
    HttpUriRequest request = getRequest(method, url);
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeoutMillis);
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeoutMillis);
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT);
    httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset);

    for (String name : headersToOverwrite.keySet()) {
        request.setHeader(name, headersToOverwrite.get(name));
    }//from w ww  .ja  v a  2  s  .  c  o m

    String oAuthNonce = String.valueOf(new SecureRandom().nextLong());
    Long oAuthTimestamp = System.currentTimeMillis() / 1000;
    String signature = getSignature(url, method, oAuthNonce, oAuthTimestamp);
    String authorizationHeader = getAuthorizationHeader(signature, oAuthNonce, oAuthTimestamp);
    request.setHeader("Authorization", authorizationHeader);

    if (method == HttpMethod.POST) {
        HttpPost postRequest = (HttpPost) request;
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(new ByteArrayInputStream(body.getBody()));
        entity.setContentType(body.getContentType());
        postRequest.setEntity(entity);
    } else if (method == HttpMethod.PUT) {
        HttpPut putRequest = (HttpPut) request;
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(new ByteArrayInputStream(body.getBody()));
        entity.setContentType(body.getContentType());
        putRequest.setEntity(entity);
    }

    org.apache.http.HttpResponse apacheHCResponse = httpClient.execute(request);
    if (apacheHCResponse.getStatusLine().getStatusCode() >= 400) {
        HttpResponse httpResponse = toReturnValue(apacheHCResponse, charset);
        throw new HttpException(apacheHCResponse.getStatusLine().getReasonPhrase(), httpResponse);
    }
    return toReturnValue(apacheHCResponse, charset);
}

From source file:com.appdynamics.openstack.nova.RestClient.java

static HttpClient httpClientWithTrustManager() throws KeyManagementException, NoSuchAlgorithmException {
    HttpClient httpClient = new DefaultHttpClient();

    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);

    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);

    httpClient.getParams().setParameter("http.connection-manager.max-per-host", 1);

    X509TrustManager tm = new X509TrustManager() {

        @Override/*from w ww .  ja v  a 2 s  .  co m*/
        public X509Certificate[] getAcceptedIssuers() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // TODO Auto-generated method stub

        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // TODO Auto-generated method stub

        }
    };

    SSLContext ctx = SSLContext.getInstance("TLS");

    ctx.init(null, new TrustManager[] { tm }, null);

    SSLSocketFactory ssf = new SSLSocketFactory(ctx);

    ClientConnectionManager ccm = httpClient.getConnectionManager();

    SchemeRegistry sr = ccm.getSchemeRegistry();

    sr.register(new Scheme("https", ssf, 443)); // Scheme("https", ssf, 443));

    return new DefaultHttpClient(ccm, httpClient.getParams());

}

From source file:se.inera.certificate.proxy.filter.ProxyFilter.java

private void doInit() {
    if (!isInitiated) {
        connectionManager = new PoolingClientConnectionManager(SchemeRegistryFactory.createDefault());
        connectionManager.setMaxTotal(maxConnections);
        connectionManager.setDefaultMaxPerRoute(maxDefaultPerRoute);

        // TODO: Hardcoded for now... (PW)
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT_MS);
        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, DEFAULT_SO_TIMEOUT_MS);
        params.setParameter(CoreConnectionPNames.SO_LINGER, DEFAULT_SO_LINGER_S);

        httpClient = new HttpProxyClient(connectionManager, params);

        isInitiated = true;//from w w  w  . j  a v a  2s. c  o m
    }
}

From source file:com.pagecrumb.proxy.ProxyServlet.java

@SuppressWarnings("unchecked")
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp, boolean isPost)
        throws ServletException, IOException {

    log.info("contextPath=" + req.getContextPath());

    // Setup the headless browser
    WebClient webClient = new WebClient();

    webClient.setWebConnection(new UrlFetchWebConnection(webClient));

    HttpParams httpParams = new BasicHttpParams();
    httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); // GAE constraint 

    // Originally, the design was to use HttpClient to execute methods
    // Now, it uses WebClient
    ClientConnectionManager connectionManager = new GAEConnectionManager();
    HttpClient httpclient = new DefaultHttpClient(connectionManager, httpParams);

    StringBuffer sb = new StringBuffer();

    log.info(getClass().toString() + " " + "URI Component=" + req.getRequestURI());
    log.info(getClass().toString() + " " + "URL Component=" + req.getRequestURL());

    if (req.getQueryString() != null) {
        //sb.append("?" + req.getQueryString());

        String s1 = Util.decodeURIComponent(req.getQueryString());
        log.info(getClass().toString() + " " + "QueryString=" + req.getQueryString());

        target = req.getQueryString();//from  w  ww .j  av  a  2  s  .  c  o  m
        int port = req.getServerPort();
        String domain = req.getServerName();

        URL url = new URL(SCHEME, domain, port, target);
        /*
        HtmlPage page = webClient.getPage(target);
                
        //gae hack because its single threaded
         webClient.getJavaScriptEngine().pumpEventLoop(PUMP_TIME);
                 
         pageString = page.asXml();
        */
    }

    sb.append(target);

    log.info(getClass().toString() + " " + "sb=" + sb.toString());

    HttpRequestBase targetRequest = null;

    if (isPost) {
        HttpPost post = new HttpPost(sb.toString());

        Enumeration<String> paramNames = req.getParameterNames();

        String paramName = null;

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        while (paramNames.hasMoreElements()) {
            paramName = paramNames.nextElement();
            params.add(new BasicNameValuePair(paramName, req.getParameterValues(paramName)[0]));
        }

        post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        targetRequest = post;
    } else {
        HttpGet get = new HttpGet(sb.toString());
        targetRequest = get;
    }

    //        log.info("Request Headers");
    //        Enumeration<String> headerNames = req.getHeaderNames();
    //        String headerName = null;
    //        while(headerNames.hasMoreElements()){
    //            headerName = headerNames.nextElement();
    //            targetRequest.addHeader(headerName, req.getHeader(headerName));
    //            log.info(headerName + " : " + req.getHeader(headerName));
    //        }

    HttpResponse targetResponse = httpclient.execute(targetRequest);
    HttpEntity entity = targetResponse.getEntity();

    // Send the Response
    InputStream input = entity.getContent();
    OutputStream output = resp.getOutputStream();

    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
    String line = reader.readLine();

    /*
     * To use the HttpClient instead the HtmlUnit's WebClient:
     */
    while (line != null) {
        writer.write(line + "\n");
        line = reader.readLine();
    }

    //        Scanner scanner = new Scanner(pageString);
    //        while (scanner.hasNextLine()) {
    //          String s = scanner.nextLine();
    //          writer.write(s + "\n");
    //        }
    if (webClient != null) {
        webClient.closeAllWindows();
    }

    reader.close();
    writer.close();

    httpclient.getConnectionManager().shutdown();
}

From source file:com.autonomousturk.crawler.fetcher.PageFetcher.java

public PageFetcher(CrawlConfig config) {
    super(config);

    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgentString());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeout());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    params.setBooleanParameter("http.protocol.handle-redirects", false);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isIncludeHttpsPages()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }//from  w w  w .  j av  a2 s . c  om

    connectionManager = new PoolingClientConnectionManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.SNIHttpClientConnection.java

private HttpClient getClient() {
    if (client == null)
        client = new DefaultHttpClient();
    HttpParams params = client.getParams();
    if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) {
        isUsingProxy = true;//from w w w.  j av  a  2  s  .c o m
        InetSocketAddress adr = (InetSocketAddress) proxy.address();
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(adr.getHostName(), adr.getPort()));
    }
    if (timeout != null)
        params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout.intValue());
    if (readTimeout != null)
        params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout.intValue());
    if (followRedirects != null)
        params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects.booleanValue());
    SSLSocketFactory sf = hostnameverifier != null ? new SNISSLSocketFactory(getSSLContext(), hostnameverifier)
            : new SNISSLSocketFactory(getSSLContext());
    Scheme https = new Scheme("https", 443, sf); //$NON-NLS-1$
    client.getConnectionManager().getSchemeRegistry().register(https);
    return client;
}

From source file:com.almende.eve.transport.http.ApacheHttpClient.java

/**
 * Instantiates a new apache http client.
 *
 *///  w w  w  .java  2 s.com
private ApacheHttpClient() {

    // Allow self-signed SSL certificates:
    final TrustStrategy trustStrategy = new TrustSelfSignedStrategy();
    final X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier();
    final SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();

    SSLSocketFactory sslSf;
    try {
        sslSf = new SSLSocketFactory(trustStrategy, hostnameVerifier);
        final Scheme https = new Scheme("https", 443, sslSf);
        schemeRegistry.register(https);
    } catch (Exception e) {
        LOG.warning("Couldn't init SSL socket, https not supported!");
    }

    // Work with PoolingClientConnectionManager
    final ClientConnectionManager connection = new PoolingClientConnectionManager(schemeRegistry);

    // Provide eviction thread to clear out stale threads.
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (true) {
                    synchronized (this) {
                        wait(5000);
                        connection.closeExpiredConnections();
                        connection.closeIdleConnections(30, TimeUnit.SECONDS);
                    }
                }
            } catch (final InterruptedException ex) {
            }
        }
    }).start();

    // generate httpclient
    httpClient = new DefaultHttpClient(connection);

    // Set cookie policy and persistent cookieStore
    try {
        httpClient.setCookieStore(new MyCookieStore());
    } catch (final Exception e) {
        LOG.log(Level.WARNING, "Failed to initialize persistent cookieStore!", e);
    }
    final HttpParams params = httpClient.getParams();

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
    params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
    params.setParameter(CoreConnectionPNames.TCP_NODELAY, true);
    httpClient.setParams(params);
}

From source file:org.sbs.goodcrawler.fetcher.PageFetcher.java

public PageFetcher(FetchConfig config) {
    super(config);
    HttpParams params = new BasicHttpParams();
    HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
    paramsBean.setVersion(HttpVersion.HTTP_1_1);
    paramsBean.setContentCharset("UTF-8");
    paramsBean.setUseExpectContinue(false);

    params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getAgent());
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSocketTimeoutMilliseconds());
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout());

    params.setBooleanParameter("http.protocol.handle-redirects", false);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));

    if (config.isHttps()) {
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    }/*  ww  w  . j a va  2  s .  c  o  m*/

    connectionManager = new PoolingClientConnectionManager(schemeRegistry);
    connectionManager.setMaxTotal(config.getMaxTotalConnections());
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
    httpClient = new DefaultHttpClient(connectionManager, params);

    if (config.getProxyHost() != null) {

        if (config.getProxyUsername() != null) {
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));
        }

        HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    }

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        @Override
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header contentEncoding = entity.getContentEncoding();
            if (contentEncoding != null) {
                HeaderElement[] codecs = contentEncoding.getElements();
                for (HeaderElement codec : codecs) {
                    if (codec.getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }

    });

    if (connectionMonitorThread == null) {
        connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
    }
    connectionMonitorThread.start();

}