Example usage for org.apache.http.config RegistryBuilder register

List of usage examples for org.apache.http.config RegistryBuilder register

Introduction

In this page you can find the example usage for org.apache.http.config RegistryBuilder register.

Prototype

public RegistryBuilder<I> register(String str, I i) 

Source Link

Usage

From source file:com.mirth.connect.client.core.ServerConnection.java

public ServerConnection(int timeout, String[] httpsProtocols, String[] httpsCipherSuites, boolean allowHTTP) {
    SSLContext sslContext = null;
    try {/*from   w  w w.ja va 2s  . com*/
        sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
    } catch (Exception e) {
        logger.error("Unable to build SSL context.", e);
    }

    String[] enabledProtocols = MirthSSLUtil.getEnabledHttpsProtocols(httpsProtocols);
    String[] enabledCipherSuites = MirthSSLUtil.getEnabledHttpsCipherSuites(httpsCipherSuites);
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
            enabledProtocols, enabledCipherSuites, NoopHostnameVerifier.INSTANCE);
    RegistryBuilder<ConnectionSocketFactory> builder = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslConnectionSocketFactory);
    if (allowHTTP) {
        builder.register("http", PlainConnectionSocketFactory.getSocketFactory());
    }
    Registry<ConnectionSocketFactory> socketFactoryRegistry = builder.build();

    PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(
            socketFactoryRegistry);
    httpClientConnectionManager.setDefaultMaxPerRoute(5);
    httpClientConnectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build());
    // MIRTH-3962: The stale connection settings has been deprecated, and this is recommended instead
    httpClientConnectionManager.setValidateAfterInactivity(5000);

    HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(httpClientConnectionManager);
    HttpUtil.configureClientBuilder(clientBuilder);

    client = clientBuilder.build();
    requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT)
            .setConnectionRequestTimeout(CONNECT_TIMEOUT).setSocketTimeout(timeout).build();
}

From source file:eu.europa.ec.markt.dss.validation102853.https.CommonsDataLoader.java

private RegistryBuilder<ConnectionSocketFactory> setConnectionManagerSchemeHttps(
        RegistryBuilder<ConnectionSocketFactory> socketFactoryRegistryBuilder) throws DSSException {

    try {/* w  w  w  . j av a  2  s .c o  m*/
        // TODO: (Bob: 2013 Dec 03) To be replaced: SSLSocketFactory deprecated!!!
        SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new OptimistTrustStrategy(),
                new OptimistX509HostnameVerifier());
        return socketFactoryRegistryBuilder.register("https", sslSocketFactory);
    } catch (Exception e) {
        throw new DSSException(e);
    }
}

From source file:com.mirth.connect.connectors.ws.WebServiceDispatcher.java

/**
 * Returns the URL for the passed in String. If the URL requires authentication, then the WSDL
 * is saved as a temp file and the URL for that file is returned.
 * // w w  w . j a  v  a2 s.com
 * @param wsdlUrl
 * @param username
 * @param password
 * @return
 * @throws Exception
 */
private URL getWsdlUrl(DispatchContainer dispatchContainer) throws Exception {
    URI uri = new URI(dispatchContainer.getCurrentWsdlUrl());

    // If the URL points to file, just return it
    if (!uri.getScheme().equalsIgnoreCase("file")) {
        BasicHttpClientConnectionManager httpClientConnectionManager = new BasicHttpClientConnectionManager(
                socketFactoryRegistry.build());
        httpClientConnectionManager.setSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build());
        HttpClientBuilder clientBuilder = HttpClients.custom()
                .setConnectionManager(httpClientConnectionManager);
        HttpUtil.configureClientBuilder(clientBuilder);
        CloseableHttpClient client = clientBuilder.build();

        try {
            clients.add(client);
            HttpClientContext context = HttpClientContext.create();

            if (dispatchContainer.getCurrentUsername() != null
                    && dispatchContainer.getCurrentPassword() != null) {
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
                AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT,
                        AuthScope.ANY_REALM);
                Credentials credentials = new UsernamePasswordCredentials(
                        dispatchContainer.getCurrentUsername(), dispatchContainer.getCurrentPassword());
                credsProvider.setCredentials(authScope, credentials);
                AuthCache authCache = new BasicAuthCache();
                RegistryBuilder<AuthSchemeProvider> registryBuilder = RegistryBuilder
                        .<AuthSchemeProvider>create();
                registryBuilder.register(AuthSchemes.BASIC, new BasicSchemeFactory());

                context.setCredentialsProvider(credsProvider);
                context.setAuthSchemeRegistry(registryBuilder.build());
                context.setAuthCache(authCache);
            }

            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout)
                    .setSocketTimeout(timeout).setStaleConnectionCheckEnabled(true).build();
            context.setRequestConfig(requestConfig);

            return getWsdl(client, context, dispatchContainer, new HashMap<String, File>(),
                    dispatchContainer.getCurrentWsdlUrl()).toURI().toURL();
        } finally {
            HttpClientUtils.closeQuietly(client);
            clients.remove(client);
        }
    }

    return uri.toURL();
}

From source file:com.liferay.petra.json.web.service.client.BaseJSONWebServiceClientImpl.java

protected Registry<SchemeIOSessionStrategy> getSchemeIOSessionStrategyRegistry() {

    RegistryBuilder<SchemeIOSessionStrategy> registryBuilder = RegistryBuilder
            .<SchemeIOSessionStrategy>create();

    registryBuilder.register("http", NoopIOSessionStrategy.INSTANCE);
    registryBuilder.register("https", getSSLIOSessionStrategy());

    return registryBuilder.build();
}

From source file:com.mirth.connect.connectors.http.HttpDispatcher.java

@Override
public Response send(ConnectorProperties connectorProperties, ConnectorMessage connectorMessage) {
    HttpDispatcherProperties httpDispatcherProperties = (HttpDispatcherProperties) connectorProperties;
    eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(),
            getDestinationName(), ConnectionStatusEventType.WRITING));

    String responseData = null;/*from  w  w w  .j  a v  a  2  s .c  o m*/
    String responseError = null;
    String responseStatusMessage = null;
    Status responseStatus = Status.QUEUED;
    boolean validateResponse = false;

    CloseableHttpClient client = null;
    HttpRequestBase httpMethod = null;
    CloseableHttpResponse httpResponse = null;
    File tempFile = null;
    int socketTimeout = NumberUtils.toInt(httpDispatcherProperties.getSocketTimeout(), 30000);

    try {
        configuration.configureDispatcher(this, httpDispatcherProperties);

        long dispatcherId = getDispatcherId();
        client = clients.get(dispatcherId);
        if (client == null) {
            BasicHttpClientConnectionManager httpClientConnectionManager = new BasicHttpClientConnectionManager(
                    socketFactoryRegistry.build());
            httpClientConnectionManager
                    .setSocketConfig(SocketConfig.custom().setSoTimeout(socketTimeout).build());
            HttpClientBuilder clientBuilder = HttpClients.custom()
                    .setConnectionManager(httpClientConnectionManager);
            HttpUtil.configureClientBuilder(clientBuilder);

            if (httpDispatcherProperties.isUseProxyServer()) {
                clientBuilder.setRoutePlanner(new DynamicProxyRoutePlanner());
            }

            client = clientBuilder.build();
            clients.put(dispatcherId, client);
        }

        URI hostURI = new URI(httpDispatcherProperties.getHost());
        String host = hostURI.getHost();
        String scheme = hostURI.getScheme();
        int port = hostURI.getPort();
        if (port == -1) {
            if (scheme.equalsIgnoreCase("https")) {
                port = 443;
            } else {
                port = 80;
            }
        }

        // Parse the content type field first, and then add the charset if needed
        ContentType contentType = ContentType.parse(httpDispatcherProperties.getContentType());
        Charset charset = null;
        if (contentType.getCharset() == null) {
            charset = Charset.forName(CharsetUtils.getEncoding(httpDispatcherProperties.getCharset()));
        } else {
            charset = contentType.getCharset();
        }

        if (httpDispatcherProperties.isMultipart()) {
            tempFile = File.createTempFile(UUID.randomUUID().toString(), ".tmp");
        }

        HttpHost target = new HttpHost(host, port, scheme);

        httpMethod = buildHttpRequest(hostURI, httpDispatcherProperties, connectorMessage, tempFile,
                contentType, charset);

        HttpClientContext context = HttpClientContext.create();

        // authentication
        if (httpDispatcherProperties.isUseAuthentication()) {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
            Credentials credentials = new UsernamePasswordCredentials(httpDispatcherProperties.getUsername(),
                    httpDispatcherProperties.getPassword());
            credsProvider.setCredentials(authScope, credentials);
            AuthCache authCache = new BasicAuthCache();
            RegistryBuilder<AuthSchemeProvider> registryBuilder = RegistryBuilder.<AuthSchemeProvider>create();

            if (AuthSchemes.DIGEST.equalsIgnoreCase(httpDispatcherProperties.getAuthenticationType())) {
                logger.debug("using Digest authentication");
                registryBuilder.register(AuthSchemes.DIGEST, new DigestSchemeFactory(charset));

                if (httpDispatcherProperties.isUsePreemptiveAuthentication()) {
                    processDigestChallenge(authCache, target, credentials, httpMethod, context);
                }
            } else {
                logger.debug("using Basic authentication");
                registryBuilder.register(AuthSchemes.BASIC, new BasicSchemeFactory(charset));

                if (httpDispatcherProperties.isUsePreemptiveAuthentication()) {
                    authCache.put(target, new BasicScheme());
                }
            }

            context.setCredentialsProvider(credsProvider);
            context.setAuthSchemeRegistry(registryBuilder.build());
            context.setAuthCache(authCache);

            logger.debug("using authentication with credentials: " + credentials);
        }

        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(socketTimeout)
                .setSocketTimeout(socketTimeout).setStaleConnectionCheckEnabled(true).build();
        context.setRequestConfig(requestConfig);

        // Set proxy information
        if (httpDispatcherProperties.isUseProxyServer()) {
            context.setAttribute(PROXY_CONTEXT_KEY, new HttpHost(httpDispatcherProperties.getProxyAddress(),
                    Integer.parseInt(httpDispatcherProperties.getProxyPort())));
        }

        // execute the method
        logger.debug(
                "executing method: type=" + httpMethod.getMethod() + ", uri=" + httpMethod.getURI().toString());
        httpResponse = client.execute(target, httpMethod, context);
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        logger.debug("received status code: " + statusCode);

        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        for (Header header : httpResponse.getAllHeaders()) {
            List<String> list = headers.get(header.getName());

            if (list == null) {
                list = new ArrayList<String>();
                headers.put(header.getName(), list);
            }

            list.add(header.getValue());
        }

        connectorMessage.getConnectorMap().put("responseStatusLine", statusLine.toString());
        connectorMessage.getConnectorMap().put("responseHeaders",
                new MessageHeaders(new CaseInsensitiveMap(headers)));

        ContentType responseContentType = ContentType.get(httpResponse.getEntity());
        if (responseContentType == null) {
            responseContentType = ContentType.TEXT_PLAIN;
        }

        Charset responseCharset = charset;
        if (responseContentType.getCharset() != null) {
            responseCharset = responseContentType.getCharset();
        }

        final String responseBinaryMimeTypes = httpDispatcherProperties.getResponseBinaryMimeTypes();
        BinaryContentTypeResolver binaryContentTypeResolver = new BinaryContentTypeResolver() {
            @Override
            public boolean isBinaryContentType(ContentType contentType) {
                return HttpDispatcher.this.isBinaryContentType(responseBinaryMimeTypes, contentType);
            }
        };

        /*
         * First parse out the body of the HTTP response. Depending on the connector settings,
         * this could end up being a string encoded with the response charset, a byte array
         * representing the raw response payload, or a MimeMultipart object.
         */
        Object responseBody = "";

        // The entity could be null in certain cases such as 204 responses
        if (httpResponse.getEntity() != null) {
            // Only parse multipart if XML Body is selected and Parse Multipart is enabled
            if (httpDispatcherProperties.isResponseXmlBody()
                    && httpDispatcherProperties.isResponseParseMultipart()
                    && responseContentType.getMimeType().startsWith(FileUploadBase.MULTIPART)) {
                responseBody = new MimeMultipart(new ByteArrayDataSource(httpResponse.getEntity().getContent(),
                        responseContentType.toString()));
            } else if (binaryContentTypeResolver.isBinaryContentType(responseContentType)) {
                responseBody = IOUtils.toByteArray(httpResponse.getEntity().getContent());
            } else {
                responseBody = IOUtils.toString(httpResponse.getEntity().getContent(), responseCharset);
            }
        }

        /*
         * Now that we have the response body, we need to create the actual Response message
         * data. Depending on the connector settings this could be our custom serialized XML, a
         * Base64 string encoded from the raw response payload, or a string encoded from the
         * payload with the request charset.
         */
        if (httpDispatcherProperties.isResponseXmlBody()) {
            responseData = HttpMessageConverter.httpResponseToXml(statusLine.toString(), headers, responseBody,
                    responseContentType, httpDispatcherProperties.isResponseParseMultipart(),
                    httpDispatcherProperties.isResponseIncludeMetadata(), binaryContentTypeResolver);
        } else if (responseBody instanceof byte[]) {
            responseData = new String(Base64Util.encodeBase64((byte[]) responseBody), "US-ASCII");
        } else {
            responseData = (String) responseBody;
        }

        validateResponse = httpDispatcherProperties.getDestinationConnectorProperties().isValidateResponse();

        if (statusCode < HttpStatus.SC_BAD_REQUEST) {
            responseStatus = Status.SENT;
        } else {
            eventController.dispatchEvent(new ErrorEvent(getChannelId(), getMetaDataId(),
                    connectorMessage.getMessageId(), ErrorEventType.DESTINATION_CONNECTOR, getDestinationName(),
                    connectorProperties.getName(), "Received error response from HTTP server.", null));
            responseStatusMessage = ErrorMessageBuilder
                    .buildErrorResponse("Received error response from HTTP server.", null);
            responseError = ErrorMessageBuilder.buildErrorMessage(connectorProperties.getName(), responseData,
                    null);
        }
    } catch (Exception e) {
        eventController.dispatchEvent(new ErrorEvent(getChannelId(), getMetaDataId(),
                connectorMessage.getMessageId(), ErrorEventType.DESTINATION_CONNECTOR, getDestinationName(),
                connectorProperties.getName(), "Error connecting to HTTP server.", e));
        responseStatusMessage = ErrorMessageBuilder.buildErrorResponse("Error connecting to HTTP server", e);
        responseError = ErrorMessageBuilder.buildErrorMessage(connectorProperties.getName(),
                "Error connecting to HTTP server", e);
    } finally {
        try {
            HttpClientUtils.closeQuietly(httpResponse);

            // Delete temp files if we created them
            if (tempFile != null) {
                tempFile.delete();
                tempFile = null;
            }
        } finally {
            eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(),
                    getDestinationName(), ConnectionStatusEventType.IDLE));
        }
    }

    return new Response(responseStatus, responseData, responseStatusMessage, responseError, validateResponse);
}

From source file:eu.europa.ec.markt.dss.validation102853.https.CommonDataLoader.java

private RegistryBuilder<ConnectionSocketFactory> setConnectionManagerSchemeHttps(
        RegistryBuilder<ConnectionSocketFactory> socketFactoryRegistryBuilder) throws DSSException {

    try {//from  w ww . j  a  v  a2s .c o m

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() },
                new SecureRandom());
        SSLContext.setDefault(sslContext);

        final SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                sslContext);
        return socketFactoryRegistryBuilder.register("https", sslConnectionSocketFactory);
    } catch (Exception e) {
        throw new DSSException(e);
    }
}

From source file:org.seasar.robot.client.http.HcHttpClient.java

public synchronized void init() {
    if (httpClient != null) {
        return;/*from ww  w  .jav a  2 s.  co  m*/
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Initializing " + HcHttpClient.class.getName());
    }

    // access timeout
    final Integer accessTimeoutParam = getInitParameter(ACCESS_TIMEOUT_PROPERTY, accessTimeout);
    if (accessTimeoutParam != null) {
        accessTimeout = accessTimeoutParam;
    }

    // robots.txt parser
    final Boolean robotsTxtEnabled = getInitParameter(ROBOTS_TXT_ENABLED_PROPERTY, Boolean.TRUE);
    if (robotsTxtHelper != null) {
        robotsTxtHelper.setEnabled(robotsTxtEnabled.booleanValue());
    }

    // httpclient
    final org.apache.http.client.config.RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    final Integer connectionTimeoutParam = getInitParameter(CONNECTION_TIMEOUT_PROPERTY, connectionTimeout);
    if (connectionTimeoutParam != null) {
        requestConfigBuilder.setConnectTimeout(connectionTimeoutParam);

    }
    final Boolean staleCheckingEnabledParam = getInitParameter(STALE_CHECKING_ENABLED_PROPERTY,
            staleCheckingEnabled);
    if (staleCheckingEnabledParam != null) {
        requestConfigBuilder.setStaleConnectionCheckEnabled(staleCheckingEnabledParam);
    }
    final Integer soTimeoutParam = getInitParameter(SO_TIMEOUT_PROPERTY, soTimeout);
    if (soTimeoutParam != null) {
        requestConfigBuilder.setSocketTimeout(soTimeoutParam);
    }

    // AuthSchemeFactory
    final RegistryBuilder<AuthSchemeProvider> authSchemeProviderBuilder = RegistryBuilder.create();
    final Map<String, AuthSchemeProvider> factoryMap = getInitParameter(AUTH_SCHEME_PROVIDERS_PROPERTY,
            authSchemeProviderMap);
    if (factoryMap != null) {
        for (final Map.Entry<String, AuthSchemeProvider> entry : factoryMap.entrySet()) {
            authSchemeProviderBuilder.register(entry.getKey(), entry.getValue());
        }
    }

    // user agent
    userAgent = getInitParameter(USER_AGENT_PROPERTY, userAgent);
    if (StringUtil.isNotBlank(userAgent)) {
        httpClientBuilder.setUserAgent(userAgent);
    }

    CredentialsProvider credsProvider = null;
    AuthCache authCache = null;

    // proxy
    final String proxyHost = getInitParameter(PROXY_HOST_PROPERTY, this.proxyHost);
    final Integer proxyPort = getInitParameter(PROXY_PORT_PROPERTY, this.proxyPort);
    if (proxyHost != null && proxyPort != null) {
        final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        final DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        httpClientBuilder.setRoutePlanner(routePlanner);

        final Credentials credentials = getInitParameter(PROXY_CREDENTIALS_PROPERTY, proxyCredentials);
        if (credentials != null) {
            authCache = new BasicAuthCache();
            credsProvider = new BasicCredentialsProvider();

            credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), credentials);
            final AuthScheme authScheme = getInitParameter(PROXY_AUTH_SCHEME_PROPERTY, proxyAuthScheme);
            if (authScheme != null) {
                authCache.put(proxy, authScheme);
            }
        }
    }

    // Authentication
    final Authentication[] siteCredentialList = getInitParameter(BASIC_AUTHENTICATIONS_PROPERTY,
            new Authentication[0]);
    if (siteCredentialList != null && siteCredentialList.length > 0 && authCache == null) {
        authCache = new BasicAuthCache();
        credsProvider = new BasicCredentialsProvider();
    }
    for (final Authentication authentication : siteCredentialList) {
        final AuthScope authScope = authentication.getAuthScope();
        credsProvider.setCredentials(authScope, authentication.getCredentials());
        final AuthScheme authScheme = authentication.getAuthScheme();
        if (authScope.getHost() != null && authScheme != null) {
            final HttpHost targetHost = new HttpHost(authScope.getHost(), authScope.getPort());
            authCache.put(targetHost, authScheme);
        }
    }
    if (authCache != null) {
        httpClientContext.setAuthCache(authCache);
        httpClientContext.setCredentialsProvider(credsProvider);
    }

    // Request Header
    final RequestHeader[] requestHeaders = getInitParameter(REQUERT_HEADERS_PROPERTY, new RequestHeader[0]);
    for (final RequestHeader requestHeader : requestHeaders) {
        if (requestHeader.isValid()) {
            requestHeaderList.add(new BasicHeader(requestHeader.getName(), requestHeader.getValue()));
        }
    }

    // do not redirect
    requestConfigBuilder.setRedirectsEnabled(false);

    // cookie
    if (cookieSpec != null) {
        requestConfigBuilder.setCookieSpec(cookieSpec);
    }

    // cookie store
    httpClientBuilder.setDefaultCookieStore(cookieStore);
    if (cookieStore != null) {
        final Cookie[] cookies = getInitParameter(COOKIES_PROPERTY, new Cookie[0]);
        for (final Cookie cookie : cookies) {
            cookieStore.addCookie(cookie);
        }
    }

    connectionMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(
            new HcConnectionMonitorTarget(clientConnectionManager, idleConnectionTimeout),
            connectionCheckInterval, true);

    final CloseableHttpClient closeableHttpClient = httpClientBuilder
            .setConnectionManager(clientConnectionManager).setDefaultRequestConfig(requestConfigBuilder.build())
            .build();
    if (!httpClientPropertyMap.isEmpty()) {
        final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(closeableHttpClient.getClass());
        for (final Map.Entry<String, Object> entry : httpClientPropertyMap.entrySet()) {
            final String propertyName = entry.getKey();
            if (beanDesc.hasPropertyDesc(propertyName)) {
                final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(propertyName);
                propertyDesc.setValue(closeableHttpClient, entry.getValue());
            } else {
                logger.warn("DefaultHttpClient does not have " + propertyName + ".");
            }
        }
    }

    httpClient = closeableHttpClient;
}

From source file:crawlercommons.fetcher.http.SimpleHttpFetcher.java

private void init() {
    if (_httpClient == null) {
        synchronized (SimpleHttpFetcher.class) {
            if (_httpClient != null)
                return;

            final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();

            // Set the socket and connection timeout to be something
            // reasonable.
            requestConfigBuilder.setSocketTimeout(_socketTimeout);
            requestConfigBuilder.setConnectTimeout(_connectionTimeout);
            requestConfigBuilder.setConnectionRequestTimeout(_connectionRequestTimeout);

            /*//from   w w  w  .  ja  va2  s  . c  o  m
             * CoreConnectionPNames.TCP_NODELAY='http.tcp.nodelay':
             * determines whether Nagle's algorithm is to be used. Nagle's
             * algorithm tries to conserve bandwidth by minimizing the
             * number of segments that are sent. When applications wish to
             * decrease network latency and increase performance, they can
             * disable Nagle's algorithm (that is enable TCP_NODELAY. Data
             * will be sent earlier, at the cost of an increase in bandwidth
             * consumption. This parameter expects a value of type
             * java.lang.Boolean. If this parameter is not set, TCP_NODELAY
             * will be enabled (no delay).
             */
            // FIXME Could not find this parameter in http-client version
            // 4.5
            // HttpConnectionParams.setTcpNoDelay(params, true);
            // HttpProtocolParams.setVersion(params, _httpVersion);

            httpClientBuilder.setUserAgent(_userAgent.getUserAgentString());

            // HttpProtocolParams.setContentCharset(params, "UTF-8");
            // HttpProtocolParams.setHttpElementCharset(params, "UTF-8");

            /*
             * CoreProtocolPNames.USE_EXPECT_CONTINUE=
             * 'http.protocol.expect-continue': activates the Expect:
             * 100-Continue handshake for the entity enclosing methods. The
             * purpose of the Expect: 100-Continue handshake is to allow the
             * client that is sending a request message with a request body
             * to determine if the origin server is willing to accept the
             * request (based on the request headers) before the client
             * sends the request body. The use of the Expect: 100-continue
             * handshake can result in a noticeable performance improvement
             * for entity enclosing requests (such as POST and PUT) that
             * require the target server's authentication. The Expect:
             * 100-continue handshake should be used with caution, as it may
             * cause problems with HTTP servers and proxies that do not
             * support HTTP/1.1 protocol. This parameter expects a value of
             * type java.lang.Boolean. If this parameter is not set,
             * HttpClient will not attempt to use the handshake.
             */
            requestConfigBuilder.setExpectContinueEnabled(true);

            /*
             * CoreProtocolPNames.WAIT_FOR_CONTINUE=
             * 'http.protocol.wait-for-continue': defines the maximum period
             * of time in milliseconds the client should spend waiting for a
             * 100-continue response. This parameter expects a value of type
             * java.lang.Integer. If this parameter is not set HttpClient
             * will wait 3 seconds for a confirmation before resuming the
             * transmission of the request body.
             */
            // FIXME Could not find this parameter in http-client version
            // 4.5
            // params.setIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE,
            // 5000);

            // FIXME Could not find this parameter in http-client version
            // 4.5
            // CookieSpecParamBean cookieParams = new
            // CookieSpecParamBean(params);
            // cookieParams.setSingleHeader(false);

            // Create and initialize connection socket factory registry
            RegistryBuilder<ConnectionSocketFactory> registry = RegistryBuilder
                    .<ConnectionSocketFactory>create();
            registry.register("http", PlainConnectionSocketFactory.getSocketFactory());
            SSLConnectionSocketFactory sf = createSSLConnectionSocketFactory();
            if (sf != null) {
                registry.register("https", sf);
            } else {
                LOGGER.warn("No valid SSLContext found for https");
            }

            _connectionManager = new PoolingHttpClientConnectionManager(registry.build());
            _connectionManager.setMaxTotal(_maxThreads);
            _connectionManager.setDefaultMaxPerRoute(getMaxConnectionsPerHost());

            /*
             * CoreConnectionPNames.STALE_CONNECTION_CHECK=
             * 'http.connection.stalecheck': determines whether stale
             * connection check is to be used. Disabling stale connection
             * check may result in a noticeable performance improvement (the
             * check can cause up to 30 millisecond overhead per request) at
             * the risk of getting an I/O error when executing a request
             * over a connection that has been closed at the server side.
             * This parameter expects a value of type java.lang.Boolean. For
             * performance critical operations the check should be disabled.
             * If this parameter is not set, the stale connection check will
             * be performed before each request execution.
             * 
             * We don't need I/O exceptions in case if Server doesn't
             * support Kee-Alive option; our client by default always tries
             * keep-alive.
             */
            // 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.
            // Stale connections will be checked in a separate monitor
            // thread
            _connectionManager.setValidateAfterInactivity(-1);

            httpClientBuilder.setConnectionManager(_connectionManager);
            httpClientBuilder.setRetryHandler(new MyRequestRetryHandler(_maxRetryCount));
            httpClientBuilder.setRedirectStrategy(new MyRedirectStrategy(getRedirectMode()));
            httpClientBuilder.setRequestExecutor(new MyHttpRequestExecutor());

            // FUTURE KKr - support authentication
            // FIXME Could not find this parameter in http-client version
            // 4.5
            // HttpClientParams.setAuthenticating(params, false);

            requestConfigBuilder.setCookieSpec(CookieSpecs.DEFAULT);

            if (getMaxRedirects() == 0) {
                requestConfigBuilder.setRedirectsEnabled(false);
            } else {
                requestConfigBuilder.setRedirectsEnabled(true);
                requestConfigBuilder.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));

            httpClientBuilder.setDefaultHeaders(defaultHeaders);

            httpClientBuilder.setKeepAliveStrategy(new MyConnectionKeepAliveStrategy());

            monitor = new IdleConnectionMonitorThread(_connectionManager);
            monitor.start();

            httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
            _httpClient = httpClientBuilder.build();
        }
    }

}

From source file:org.codelibs.fess.crawler.client.http.HcHttpClient.java

public synchronized void init() {
    if (httpClient != null) {
        return;/*from  w  w  w .jav  a  2s. c  o m*/
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Initializing " + HcHttpClient.class.getName());
    }

    super.init();

    // robots.txt parser
    final Boolean robotsTxtEnabled = getInitParameter(ROBOTS_TXT_ENABLED_PROPERTY, Boolean.TRUE, Boolean.class);
    if (robotsTxtHelper != null) {
        robotsTxtHelper.setEnabled(robotsTxtEnabled.booleanValue());
    }

    // httpclient
    final org.apache.http.client.config.RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    final Integer connectionTimeoutParam = getInitParameter(CONNECTION_TIMEOUT_PROPERTY, connectionTimeout,
            Integer.class);
    if (connectionTimeoutParam != null) {
        requestConfigBuilder.setConnectTimeout(connectionTimeoutParam);

    }
    final Integer soTimeoutParam = getInitParameter(SO_TIMEOUT_PROPERTY, soTimeout, Integer.class);
    if (soTimeoutParam != null) {
        requestConfigBuilder.setSocketTimeout(soTimeoutParam);
    }

    // AuthSchemeFactory
    final RegistryBuilder<AuthSchemeProvider> authSchemeProviderBuilder = RegistryBuilder.create();
    @SuppressWarnings("unchecked")
    final Map<String, AuthSchemeProvider> factoryMap = getInitParameter(AUTH_SCHEME_PROVIDERS_PROPERTY,
            authSchemeProviderMap, Map.class);
    if (factoryMap != null) {
        for (final Map.Entry<String, AuthSchemeProvider> entry : factoryMap.entrySet()) {
            authSchemeProviderBuilder.register(entry.getKey(), entry.getValue());
        }
    }

    // user agent
    userAgent = getInitParameter(USER_AGENT_PROPERTY, userAgent, String.class);
    if (StringUtil.isNotBlank(userAgent)) {
        httpClientBuilder.setUserAgent(userAgent);
    }

    final HttpRoutePlanner planner = buildRoutePlanner();
    if (planner != null) {
        httpClientBuilder.setRoutePlanner(planner);
    }

    // Authentication
    final Authentication[] siteCredentialList = getInitParameter(BASIC_AUTHENTICATIONS_PROPERTY,
            new Authentication[0], Authentication[].class);
    for (final Authentication authentication : siteCredentialList) {
        final AuthScope authScope = authentication.getAuthScope();
        credentialsProvider.setCredentials(authScope, authentication.getCredentials());
        final AuthScheme authScheme = authentication.getAuthScheme();
        if (authScope.getHost() != null && authScheme != null) {
            final HttpHost targetHost = new HttpHost(authScope.getHost(), authScope.getPort());
            authCache.put(targetHost, authScheme);
        }
    }

    httpClientContext.setAuthCache(authCache);
    httpClientContext.setCredentialsProvider(credentialsProvider);

    // Request Header
    final RequestHeader[] requestHeaders = getInitParameter(REQUERT_HEADERS_PROPERTY, new RequestHeader[0],
            RequestHeader[].class);
    for (final RequestHeader requestHeader : requestHeaders) {
        if (requestHeader.isValid()) {
            requestHeaderList.add(new BasicHeader(requestHeader.getName(), requestHeader.getValue()));
        }
    }

    // do not redirect
    requestConfigBuilder
            .setRedirectsEnabled(getInitParameter(REDIRECTS_ENABLED, redirectsEnabled, Boolean.class));

    // cookie
    if (cookieSpec != null) {
        requestConfigBuilder.setCookieSpec(cookieSpec);
    }

    // cookie store
    httpClientBuilder.setDefaultCookieStore(cookieStore);
    if (cookieStore != null) {
        final Cookie[] cookies = getInitParameter(COOKIES_PROPERTY, new Cookie[0], Cookie[].class);
        for (final Cookie cookie : cookies) {
            cookieStore.addCookie(cookie);
        }
    }

    connectionMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(
            new HcConnectionMonitorTarget(clientConnectionManager, idleConnectionTimeout),
            connectionCheckInterval, true);

    final CloseableHttpClient closeableHttpClient = httpClientBuilder
            .setConnectionManager(clientConnectionManager).setDefaultRequestConfig(requestConfigBuilder.build())
            .build();
    if (!httpClientPropertyMap.isEmpty()) {
        final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(closeableHttpClient.getClass());
        for (final Map.Entry<String, Object> entry : httpClientPropertyMap.entrySet()) {
            final String propertyName = entry.getKey();
            if (beanDesc.hasPropertyDesc(propertyName)) {
                final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(propertyName);
                propertyDesc.setValue(closeableHttpClient, entry.getValue());
            } else {
                logger.warn("DefaultHttpClient does not have " + propertyName + ".");
            }
        }
    }

    httpClient = closeableHttpClient;
}