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

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

Introduction

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

Prototype

String SO_TIMEOUT

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

Click Source Link

Usage

From source file:org.cytoscape.app.internal.net.server.CyHttpdFactoryImpl.java

public CyHttpdImpl(final ServerSocketFactory serverSocketFactory) {
    if (serverSocketFactory == null)
        throw new IllegalArgumentException("serverSocketFactory == null");
    this.serverSocketFactory = serverSocketFactory;

    // Setup params

    params = (new SyncBasicHttpParams()).setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");

    // Setup service

    final HttpProcessor proc = new ImmutableHttpProcessor(new HttpResponseInterceptor[] { new ResponseDate(),
            new ResponseServer(), new ResponseContent(), new ResponseConnControl() });

    final HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
    registry.register("*", new RequestHandlerDispatcher());

    service = new HttpService(proc, new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory(),
            registry, params);/*from  w w  w  .  java 2  s . com*/
}

From source file:at.newmedialab.ldclient.service.LDClient.java

public LDClient() {
    log.info("Initialising Linked Data Client Service ...");

    ldEndpoints = new LDEndpoints();
    try {/*from  w  w w .  jav a 2 s .  co m*/
        config = new PropertiesConfiguration("ldclient.properties");
    } catch (ConfigurationException e) {
        log.warn(
                "could not load configuration file ldclient.properties from current directory, home directory, or classpath");
    }

    httpParams = new BasicHttpParams();
    httpParams.setParameter(CoreProtocolPNames.USER_AGENT, "Salzburg NewMediaLab Linked Data Client");

    httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getInt("so_timeout", 60000));
    httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            config.getInt("connection_timeout", 10000));

    httpParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
    httpParams.setIntParameter(ClientPNames.MAX_REDIRECTS, 3);
}

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

public void init(File fetchConfFile) {
    FetchConfig fetchConfig = new FetchConfig();
    Document document;/*  w w w. ja  v  a 2 s  .c om*/
    try {
        document = Jsoup.parse(fetchConfFile, "utf-8");
        Fetcher.config = fetchConfig.loadConfig(document);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ConfigurationException e) {
        e.printStackTrace();
    }

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

    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:com.jigarmjoshi.service.RestService.java

private void executeRequest(HttpUriRequest request, String url) {

    HttpClient client = new DefaultHttpClient();

    // no response within 120 seconds
    client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 120 * 1000);

    // this one causes a timeout if no connection is established within 5min
    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 300 * 1000);

    HttpResponse httpResponse;//  w w w . j  av a  2 s  .  co m

    try {
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStreamToString(instream);

            // Closing the input stream will trigger connection release
            instream.close();
        }

    } catch (ClientProtocolException ex) {
        client.getConnectionManager().shutdown();
        Log.e(RestService.class.getSimpleName(), "Failed to execute rest call ", ex);
    } catch (IOException ex) {
        client.getConnectionManager().shutdown();
        Log.e(RestService.class.getSimpleName(), "Failed to execute rest call ", ex);
    }
}

From source file:com.intel.cosbench.api.httpauth.HttpAuth.java

@Override
public AuthContext login() {
    super.login();

    //        HttpHost host = new HttpHost();
    //        HttpHost targetHost = new HttpHost(host, port, protocol);

    URI uri;//from   ww  w .j a v  a 2  s  . c  o  m

    try {
        uri = new URI(auth_url);
    } catch (URISyntaxException use) {
        throw new AuthException(use);
    }

    HttpGet method = new HttpGet(auth_url);
    method.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);

    client.getCredentialsProvider().setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
            new UsernamePasswordCredentials(this.username, this.password));

    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(AuthPNames.TARGET_AUTH_PREF,
            Arrays.asList(new String[] { AuthPolicy.BASIC, AuthPolicy.DIGEST }));

    HttpResponse response = null;

    try {
        dumpClientSettings();
        response = client.execute(method, localContext);

        dumpClientSettings();
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            return createContext();
        }
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            throw new AuthException(response.getStatusLine().getReasonPhrase());
        }
    } catch (SocketTimeoutException ste) {
        throw new AuthTimeoutException(ste);
    } catch (ConnectTimeoutException cte) {
        throw new AuthTimeoutException(cte);
    } catch (InterruptedIOException ie) {
        throw new AuthInterruptedException(ie);
    } catch (Exception e) {
        throw new AuthException(e);
    } finally {
        if (response != null)
            try {
                dumpResponse(response);
                EntityUtils.consume(response.getEntity());
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }

        if (method != null)
            method.abort();
    }

    return createContext();
}

From source file:com.cloudant.client.org.lightcouch.CouchDbClientAndroid.java

/**
 * @return {@link DefaultHttpClient} instance.
 *///ww w. ja  v  a  2 s . co  m
@Override
HttpClient createHttpClient(CouchDbProperties props) {
    DefaultHttpClient httpclient = null;
    try {
        final SchemeRegistry schemeRegistry = createRegistry(props);
        // Http params
        final HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, props.getSocketTimeout());
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, props.getConnectionTimeout());
        final ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(params, schemeRegistry);
        httpclient = new DefaultHttpClient(ccm, params);
        if (props.getProxyHost() != null) {
            HttpHost proxy = new HttpHost(props.getProxyHost(), props.getProxyPort());
            httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }
        // basic authentication
        if (props.getUsername() != null && props.getPassword() != null) {
            httpclient.getCredentialsProvider().setCredentials(new AuthScope(props.getHost(), props.getPort()),
                    new UsernamePasswordCredentials(props.getUsername(), props.getPassword()));
            props.clearPassword();
        }
        registerInterceptors(httpclient);
    } catch (Exception e) {
        throw new IllegalStateException("Error Creating HTTP client. ", e);
    }
    return httpclient;
}

From source file:org.lol.reddit.cache.CacheManager.java

private CacheManager(final Context context) {

    if (!isAlreadyInitialized.compareAndSet(false, true)) {
        throw new RuntimeException("Attempt to initialize the cache twice.");
    }/*  w  w w  . j  ava  2 s  . c o m*/

    this.context = context;

    dbManager = new CacheDbManager(context);

    RequestHandlerThread requestHandler = new RequestHandlerThread();

    // TODo put somewhere else -- make request specific, no restart needed on prefs change!
    final HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.USER_AGENT, Constants.ua(context));
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); // TODO remove hardcoded params, put in network prefs
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
    params.setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 100);
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, true);
    params.setParameter(ClientPNames.MAX_REDIRECTS, 5);
    params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 50);
    params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() {
        public int getMaxForRoute(HttpRoute route) {
            return 25;
        }
    });

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

    final ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(params, schemeRegistry);

    final DefaultHttpClient defaultHttpClient = new DefaultHttpClient(connManager, params);
    defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));

    defaultHttpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {

            final HttpEntity entity = response.getEntity();
            final Header encHeader = entity.getContentEncoding();

            if (encHeader == null)
                return;

            for (final HeaderElement elem : encHeader.getElements()) {
                if ("gzip".equalsIgnoreCase(elem.getName())) {
                    response.setEntity(new GzipDecompressingEntity(entity));
                    return;
                }
            }
        }
    });

    downloadQueue = new PrioritisedDownloadQueue(defaultHttpClient);

    requestHandler.start();
}

From source file:com.dgwave.osrs.OsrsClient.java

/**
 * Create HttpParams from configuration//from ww w  . ja  v a2 s. c  om
 * @return HttpParams
 */
private HttpParams getHttpParams() {
    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
    return params;
}