Example usage for org.apache.http.nio.conn.ssl SSLIOSessionStrategy SSLIOSessionStrategy

List of usage examples for org.apache.http.nio.conn.ssl SSLIOSessionStrategy SSLIOSessionStrategy

Introduction

In this page you can find the example usage for org.apache.http.nio.conn.ssl SSLIOSessionStrategy SSLIOSessionStrategy.

Prototype

public SSLIOSessionStrategy(final SSLContext sslcontext, final HostnameVerifier hostnameVerifier) 

Source Link

Usage

From source file:org.syslog_ng.elasticsearch_v2.client.http.ESHttpsClient.java

@Override
protected void setupHttpClientBuilder(HttpClientConfig.Builder httpClientConfigBuilder,
        ElasticSearchOptions options) {/*from w w w.  j ava 2s . c om*/
    SSLContextBuilder sslContextBuilder = setupSSLContextBuilder(options);
    SSLContext sslContext = buildSSLContext(sslContextBuilder);
    HostnameVerifier hostnameVerifier = setupHostnameVerifier(options);
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
    SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext, hostnameVerifier);

    httpClientConfigBuilder.sslSocketFactory(sslSocketFactory).httpsIOSessionStrategy(httpsIOSessionStrategy);
}

From source file:io.apiman.gateway.engine.es.DefaultESClientFactory.java

/**
 * @param httpConfig/*from   ww  w .  jav a  2 s  .  c om*/
 * @param config 
 */
@SuppressWarnings("nls")
private void updateSslConfig(Builder httpConfig, Map<String, String> config) {
    try {
        String clientKeystorePath = config.get("client-keystore");
        String clientKeystorePassword = config.get("client-keystore.password");
        String trustStorePath = config.get("trust-store");
        String trustStorePassword = config.get("trust-store.password");

        SSLContext sslContext = SSLContext.getInstance("TLS");
        Info kPathInfo = new Info(clientKeystorePath, clientKeystorePassword);
        Info tPathInfo = new Info(trustStorePath, trustStorePassword);
        sslContext.init(KeyStoreUtil.getKeyManagers(kPathInfo), KeyStoreUtil.getTrustManagers(tPathInfo), null);
        HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier();
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
                hostnameVerifier);
        SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext, hostnameVerifier);

        httpConfig.defaultSchemeForDiscoveredNodes("https");
        httpConfig.sslSocketFactory(sslSocketFactory); // for sync calls
        httpConfig.httpsIOSessionStrategy(httpsIOSessionStrategy); // for async calls

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.springframework.integration.splunk.support.SplunkHECWriter.java

@Override
public void start() {

    try {//from   ww w . ja v a2 s .c  o m
        this.batchBuffer = Collections.synchronizedList(new LinkedList<String>());
        this.lastEventReceivedTime = System.currentTimeMillis();

        Registry<SchemeIOSessionStrategy> sslSessionStrategy = RegistryBuilder.<SchemeIOSessionStrategy>create()
                .register("http", NoopIOSessionStrategy.INSTANCE)
                .register("https", new SSLIOSessionStrategy(getSSLContext(), HOSTNAME_VERIFIER)).build();

        ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
        PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor,
                sslSessionStrategy);
        cm.setMaxTotal(getPoolsize());

        HttpHost splunk = new HttpHost(getHost(), getPort());
        cm.setMaxPerRoute(new HttpRoute(splunk), getPoolsize());

        httpClient = HttpAsyncClients.custom().setConnectionManager(cm).build();

        uri = new URIBuilder().setScheme(isHttps() ? "https" : "http").setHost(getHost()).setPort(getPort())
                .setPath("/services/collector").build();

        httpClient.start();

        if (isBatchMode()) {
            new BatchBufferActivityCheckerThread(this).start();
        }
    } catch (Exception e) {
    }

    this.running = true;

}

From source file:org.apache.http.localserver.AbstractAsyncTest.java

@Before
public void setUp() throws Exception {
    this.serverBootstrap = ServerBootstrap.bootstrap();
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(15000).build();
    this.serverBootstrap.setServerInfo("TEST/1.1");
    this.serverBootstrap.setIOReactorConfig(ioReactorConfig);
    this.serverBootstrap.setExceptionLogger(new ExceptionLogger() {

        private final Log log = LogFactory.getLog(AbstractAsyncTest.class);

        @Override/*from w  ww  . ja  va2 s  .c o  m*/
        public void log(final Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    });
    if (this.scheme.equals(ProtocolScheme.https)) {
        this.serverBootstrap.setSslContext(createServerSSLContext());
    }

    final RegistryBuilder<SchemeIOSessionStrategy> builder = RegistryBuilder.create();
    builder.register("http", NoopIOSessionStrategy.INSTANCE);
    if (this.scheme.equals(ProtocolScheme.https)) {
        builder.register("https",
                new SSLIOSessionStrategy(createClientSSLContext(), new DefaultHostnameVerifier()));
    }
    final Registry<SchemeIOSessionStrategy> registry = builder.build();
    final DefaultConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
    this.connMgr = new PoolingNHttpClientConnectionManager(ioReactor, registry);
}

From source file:org.apache.nutch.indexwriter.elasticrest.ElasticRestIndexWriter.java

@Override
public void open(IndexWriterParams parameters) throws IOException {
    host = parameters.get(ElasticRestConstants.HOST);
    if (StringUtils.isBlank(host)) {
        String message = "Missing host. It should be set in index-writers.xml";
        message += "\n" + describe();
        LOG.error(message);//from   w  ww .jav a 2 s  . co  m
        throw new RuntimeException(message);
    }

    port = parameters.getInt(ElasticRestConstants.PORT, 9200);
    user = parameters.get(ElasticRestConstants.USER);
    password = parameters.get(ElasticRestConstants.PASSWORD);
    https = parameters.getBoolean(ElasticRestConstants.HTTPS, false);
    trustAllHostnames = parameters.getBoolean(ElasticRestConstants.HOSTNAME_TRUST, false);

    languages = parameters.getStrings(ElasticRestConstants.LANGUAGES);
    separator = parameters.get(ElasticRestConstants.SEPARATOR, DEFAULT_SEPARATOR);
    sink = parameters.get(ElasticRestConstants.SINK, DEFAULT_SINK);

    // trust ALL certificates
    SSLContext sslContext = null;
    try {
        sslContext = new SSLContextBuilder().loadTrustMaterial(new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        LOG.error("Failed to instantiate sslcontext object: \n{}", ExceptionUtils.getStackTrace(e));
        throw new SecurityException();
    }

    // skip hostname checks
    HostnameVerifier hostnameVerifier = null;
    if (trustAllHostnames) {
        hostnameVerifier = NoopHostnameVerifier.INSTANCE;
    } else {
        hostnameVerifier = new DefaultHostnameVerifier();
    }

    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
    SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext, hostnameVerifier);

    JestClientFactory jestClientFactory = new JestClientFactory();
    URL urlOfElasticsearchNode = new URL(https ? "https" : "http", host, port, "");

    if (host != null && port > 1) {
        HttpClientConfig.Builder builder = new HttpClientConfig.Builder(urlOfElasticsearchNode.toString())
                .multiThreaded(true).connTimeout(300000).readTimeout(300000);
        if (https) {
            if (user != null && password != null) {
                builder.defaultCredentials(user, password);
            }
            builder.defaultSchemeForDiscoveredNodes("https").sslSocketFactory(sslSocketFactory) // this only affects sync calls
                    .httpsIOSessionStrategy(httpsIOSessionStrategy); // this only affects async calls
        }
        jestClientFactory.setHttpClientConfig(builder.build());
    } else {
        throw new IllegalStateException(
                "No host or port specified. Please set the host and port in nutch-site.xml");
    }

    client = jestClientFactory.getObject();

    defaultIndex = parameters.get(ElasticRestConstants.INDEX, "nutch");
    defaultType = parameters.get(ElasticRestConstants.TYPE, "doc");

    maxBulkDocs = parameters.getInt(ElasticRestConstants.MAX_BULK_DOCS, DEFAULT_MAX_BULK_DOCS);
    maxBulkLength = parameters.getInt(ElasticRestConstants.MAX_BULK_LENGTH, DEFAULT_MAX_BULK_LENGTH);

    bulkBuilder = new Bulk.Builder().defaultIndex(defaultIndex).defaultType(defaultType);
}

From source file:org.fao.geonet.es.EsClient.java

@Override
public void afterPropertiesSet() throws Exception {
    if (StringUtils.isNotEmpty(serverUrl)) {
        JestClientFactory factory = new JestClientFactory();

        if (serverUrl.startsWith("https://")) {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                    return true;
                }/*ww  w  .j a  v a  2  s .  c  om*/
            }).build();
            // skip hostname checks
            HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;

            SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
                    hostnameVerifier);
            SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext,
                    hostnameVerifier);
            factory.setHttpClientConfig(
                    new HttpClientConfig.Builder(this.serverUrl).defaultCredentials(username, password)
                            .multiThreaded(true).sslSocketFactory(sslSocketFactory) // this only affects sync calls
                            .httpsIOSessionStrategy(httpsIOSessionStrategy) // this only affects async calls
                            .readTimeout(-1).build());
        } else {
            factory.setHttpClientConfig(
                    new HttpClientConfig.Builder(this.serverUrl).multiThreaded(true).readTimeout(-1).build());
        }
        client = factory.getObject();
        //            Depends on java.lang.NoSuchFieldError: LUCENE_5_2_1
        //            client = new PreBuiltTransportClient(Settings.EMPTY)
        //                .addTransportAddress(new InetSocketTransportAddress(
        //                    InetAddress.getByName("127.0.0.1"), 9300));

        synchronized (EsClient.class) {
            instance = this;
        }
        activated = true;
    } else {
        Log.debug("geonetwork.index",
                String.format(
                        "No Elasticsearch URL defined '%s'. "
                                + "Check bean configuration. Statistics and dasboard will not be available.",
                        this.serverUrl));
    }
}