Example usage for org.apache.http.impl.conn ManagedHttpClientConnectionFactory ManagedHttpClientConnectionFactory

List of usage examples for org.apache.http.impl.conn ManagedHttpClientConnectionFactory ManagedHttpClientConnectionFactory

Introduction

In this page you can find the example usage for org.apache.http.impl.conn ManagedHttpClientConnectionFactory ManagedHttpClientConnectionFactory.

Prototype

public ManagedHttpClientConnectionFactory() 

Source Link

Usage

From source file:sabina.integration.TestScenario.java

TestScenario(String backend, int port, boolean secure, boolean externalFiles) {
    this.port = port;
    this.backend = backend;
    this.secure = secure;
    this.externalFiles = externalFiles;

    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(getSslFactory(),
            ALLOW_ALL_HOSTNAME_VERIFIER);

    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", sslConnectionSocketFactory).build();

    HttpClientConnectionManager connManager = new BasicHttpClientConnectionManager(socketFactoryRegistry,
            new ManagedHttpClientConnectionFactory());

    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.NETSCAPE).build();
    cookieStore = new BasicCookieStore();
    HttpClientContext context = HttpClientContext.create();
    context.setCookieStore(cookieStore);

    this.httpClient = HttpClients.custom().setSchemePortResolver(h -> {
        Args.notNull(h, "HTTP host");
        final int port1 = h.getPort();
        if (port1 > 0) {
            return port1;
        }//w ww.j a v  a 2 s .  c  o m

        final String name = h.getSchemeName();
        if (name.equalsIgnoreCase("http")) {
            return port1;
        } else if (name.equalsIgnoreCase("https")) {
            return port1;
        } else {
            throw new UnsupportedSchemeException("unsupported protocol: " + name);
        }
    }).setConnectionManager(connManager).setDefaultRequestConfig(globalConfig).build();
}

From source file:org.archive.modules.fetcher.FetchHTTPRequest.java

protected HttpClientConnectionManager buildConnectionManager() {
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE).register("https",
                    new SSLConnectionSocketFactory(fetcher.sslContext(), new AllowAllHostnameVerifier()) {

                        @Override
                        public Socket createLayeredSocket(final Socket socket, final String target,
                                final int port, final HttpContext context) throws IOException {

                            return super.createLayeredSocket(socket, isDisableSNI() ? "" : target, port,
                                    context);
                        }//from   www.j a  va2  s .co  m
                    })
            .build();

    DnsResolver dnsResolver = new ServerCacheResolver(fetcher.getServerCache());

    ManagedHttpClientConnectionFactory connFactory = new ManagedHttpClientConnectionFactory() {
        private static final int DEFAULT_BUFSIZE = 8 * 1024;

        @Override
        public ManagedHttpClientConnection create(HttpRoute route, ConnectionConfig config) {
            final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
            CharsetDecoder chardecoder = null;
            CharsetEncoder charencoder = null;
            final Charset charset = cconfig.getCharset();
            final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null
                    ? cconfig.getMalformedInputAction()
                    : CodingErrorAction.REPORT;
            final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null
                    ? cconfig.getUnmappableInputAction()
                    : CodingErrorAction.REPORT;
            if (charset != null) {
                chardecoder = charset.newDecoder();
                chardecoder.onMalformedInput(malformedInputAction);
                chardecoder.onUnmappableCharacter(unmappableInputAction);
                charencoder = charset.newEncoder();
                charencoder.onMalformedInput(malformedInputAction);
                charencoder.onUnmappableCharacter(unmappableInputAction);
            }
            return new RecordingHttpClientConnection(DEFAULT_BUFSIZE, DEFAULT_BUFSIZE, chardecoder, charencoder,
                    cconfig.getMessageConstraints(), null, null, DefaultHttpRequestWriterFactory.INSTANCE,
                    DefaultHttpResponseParserFactory.INSTANCE);
        }
    };
    BasicHttpClientConnectionManager connMan = new BasicHttpClientConnectionManager(socketFactoryRegistry,
            connFactory, null, dnsResolver);

    SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
    socketConfigBuilder.setSoTimeout(fetcher.getSoTimeoutMs());
    connMan.setSocketConfig(socketConfigBuilder.build());

    return connMan;
}