org.elasticsearch.client.SSLSocketFactoryHttpConfigCallback.java Source code

Java tutorial

Introduction

Here is the source code for org.elasticsearch.client.SSLSocketFactoryHttpConfigCallback.java

Source

/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.client;

import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

/**
 * Helps configuring the http client when needing to communicate over ssl. It effectively replaces the connection manager
 * with one that has ssl properly configured thanks to the provided {@link SSLConnectionSocketFactory}.
 */
public class SSLSocketFactoryHttpConfigCallback implements RestClient.HttpClientConfigCallback {

    private final SSLConnectionSocketFactory sslSocketFactory;

    public SSLSocketFactoryHttpConfigCallback(SSLConnectionSocketFactory sslSocketFactory) {
        this.sslSocketFactory = sslSocketFactory;
    }

    @Override
    public void customizeHttpClient(HttpClientBuilder httpClientBuilder) {
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory).build();
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
                socketFactoryRegistry);
        //default settings may be too constraining
        connectionManager.setDefaultMaxPerRoute(10);
        connectionManager.setMaxTotal(30);
        httpClientBuilder.setConnectionManager(connectionManager);
    }
}