Java Code Examples for io.searchbox.client.config.HttpClientConfig#Builder

The following examples show how to use io.searchbox.client.config.HttpClientConfig#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XPackAuthTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void appliesCredentialsIfConfigured() {

    // given
    Credentials<HttpClientConfig.Builder> credentials = Mockito.mock(Credentials.class);

    HttpClientConfig.Builder settingsBuilder = createDefaultClientConfigBuilder();

    XPackAuth xPackAuth = createTestBuilder()
            .withCredentials(credentials)
            .build();

    // when
    xPackAuth.configure(settingsBuilder);

    // then
    verify(credentials).applyTo(builderArgumentCaptor.capture());
    Assert.assertEquals(settingsBuilder, builderArgumentCaptor.getValue());

}
 
Example 2
Source File: ExtendedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void getAsyncConnectionManagerConfiguresMaxTotalPerRouteIfConfigured() {

    // given
    HttpClientConfig.Builder config = createDefaultTestHttpClientConfigBuilder();

    HttpRoute expectedHttpRoute = new HttpRoute(new HttpHost("localhost"));
    int expectedMaxTotalConnection = random.nextInt(100) + 10;
    config.maxTotalConnectionPerRoute(expectedHttpRoute, expectedMaxTotalConnection);

    WrappedHttpClientConfig.Builder builder = createDefaultTestWrappedHttpClientConfigBuilder(config.build());
    ExtendedJestClientFactory factory = spy(new ExtendedJestClientFactory(builder.build()));

    PoolingNHttpClientConnectionManager mockedNHttpConnectionManager = mock(PoolingNHttpClientConnectionManager.class);
    when(factory.createUnconfiguredPoolingNHttpClientConnectionManager())
            .thenReturn(mockedNHttpConnectionManager);

    // when
    factory.getAsyncConnectionManager();

    // then
    verify(mockedNHttpConnectionManager).setMaxPerRoute(eq(expectedHttpRoute), eq(expectedMaxTotalConnection));

}
 
Example 3
Source File: ExtendedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void getAsyncConnectionManagerConfiguresMaxTotalIfConfigured() {

    // given
    HttpClientConfig.Builder config = createDefaultTestHttpClientConfigBuilder();

    int expectedMaxTotalConnection = random.nextInt(100) + 10;
    config.maxTotalConnection(expectedMaxTotalConnection);

    WrappedHttpClientConfig.Builder builder = createDefaultTestWrappedHttpClientConfigBuilder(config.build());
    ExtendedJestClientFactory factory = spy(new ExtendedJestClientFactory(builder.build()));

    PoolingNHttpClientConnectionManager mockedNHttpConnectionManager = mock(PoolingNHttpClientConnectionManager.class);
    when(factory.createUnconfiguredPoolingNHttpClientConnectionManager())
            .thenReturn(mockedNHttpConnectionManager);

    // when
    factory.getAsyncConnectionManager();

    // then
    verify(mockedNHttpConnectionManager).setMaxTotal(eq(expectedMaxTotalConnection));

}
 
Example 4
Source File: XPackAuthTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void doesntApplyCertInfoIfNotConfigured() {

    // given
    XPackAuth auth = createTestBuilder()
            .withCertInfo(null)
            .build();

    HttpClientConfig.Builder settingsBuilder = spy(createDefaultClientConfigBuilder());

    // when
    auth.configure(settingsBuilder);

    // then
    verify(settingsBuilder, never()).sslSocketFactory(any());
    verify(settingsBuilder, never()).httpsIOSessionStrategy(any());
}
 
Example 5
Source File: JestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public JestClient createClient() {
    if (client == null) {

        HttpClientConfig.Builder builder = new HttpClientConfig.Builder(serverUris)
                .maxTotalConnection(maxTotalConnections)
                .defaultMaxTotalConnectionPerRoute(defaultMaxTotalConnectionsPerRoute)
                .connTimeout(connTimeout)
                .readTimeout(readTimeout)
                .discoveryEnabled(discoveryEnabled)
                .multiThreaded(true);

        if (this.auth != null) {
            auth.configure(builder);
        }

        WrappedHttpClientConfig.Builder wrappedHttpClientConfigBuilder =
                new WrappedHttpClientConfig.Builder(builder.build())
                        .ioThreadCount(ioThreadCount);

        client = getClientProvider(wrappedHttpClientConfigBuilder).createClient();
    }
    return client;
}
 
Example 6
Source File: JKSCertInfo.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void applyTo(HttpClientConfig.Builder clientConfigBuilder) {

    try (
            FileInputStream keystoreFile = new FileInputStream(new File(keystorePath));
            FileInputStream truststoreFile = new FileInputStream(new File(truststorePath))
    ) {
        KeyStore keyStore = KeyStore.getInstance("jks");
        keyStore.load(keystoreFile, keystorePassword.toCharArray());
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, keystorePassword.toCharArray());

        KeyStore trustStore = KeyStore.getInstance("jks");
        trustStore.load(truststoreFile, truststorePassword.toCharArray());

        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

        // TODO: add support for hostname verification modes
        clientConfigBuilder.sslSocketFactory(new SSLConnectionSocketFactory(sslContext));
        clientConfigBuilder.httpsIOSessionStrategy(new SSLIOSessionStrategy(sslContext, new NoopHostnameVerifier()));

    } catch (IOException | GeneralSecurityException e) {
        throw new ConfigurationException(configExceptionMessage, e);
    }
}
 
Example 7
Source File: BasicCredentials.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void applyTo(HttpClientConfig.Builder builder) {

    BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
    basicCredentialsProvider.setCredentials(
            AuthScope.ANY,
            new UsernamePasswordCredentials(username, password)
    );

    builder.credentialsProvider(basicCredentialsProvider);

}
 
Example 8
Source File: PEMCertInfo.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void applyTo(HttpClientConfig.Builder builder) {

    if (java.security.Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    }

    try (
            FileInputStream clientCert = new FileInputStream(new File(clientCertPath));
            FileInputStream key = new FileInputStream(new File(keyPath));
            FileInputStream certificateAuthoritiies = new FileInputStream(new File(caPath))
    ) {
        KeyStore keyStore = PemReader.loadKeyStore(clientCert, key, Optional.ofNullable(keyPassphrase));
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, keyPassphrase.toCharArray());

        KeyStore trustStore = PemReader.loadTrustStore(certificateAuthoritiies);

        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

        // TODO: add support for hostname verification modes
        builder.sslSocketFactory(new SSLConnectionSocketFactory(sslContext));
        builder.httpsIOSessionStrategy(new SSLIOSessionStrategy(sslContext, new NoopHostnameVerifier()));

    } catch (IOException | GeneralSecurityException e) {
        throw new ConfigurationException(configExceptionMessage, e);
    }

}
 
Example 9
Source File: HttpESMetricsDispatcher.java    From gradle-metrics-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected void startUpClient() {
    JestClientFactory factory = new JestClientFactory();
    HttpClientConfig.Builder config = new HttpClientConfig
            .Builder(getURI(this.extension))
            .multiThreaded(false);
    if (!Strings.isNullOrEmpty(extension.getEsBasicAuthUsername())) {
        config.defaultCredentials(extension.getEsBasicAuthUsername(), extension.getEsBasicAuthPassword());
    }
    factory.setHttpClientConfig(config.build());
    client = factory.getObject();
}
 
Example 10
Source File: XPackAuthTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
public static HttpClientConfig.Builder createDefaultClientConfigBuilder() {
    return new HttpClientConfig.Builder("localhost");
}
 
Example 11
Source File: BufferedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
private HttpClientConfig.Builder createDefaultTestHttpClientConfig() {
    return new HttpClientConfig.Builder("http://localhost:9200");
}
 
Example 12
Source File: BufferedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void getObjectConfiguresConnectionReaperIfMaxConnectionIdleTimeIsGreaterThanZero() {

    // given
    HttpClientConfig.Builder httpClientConfigBuilder = createDefaultTestHttpClientConfig();
    httpClientConfigBuilder.maxConnectionIdleTime(1000, TimeUnit.MILLISECONDS);

    BufferedJestClientFactory factory = spy(createDefaultTestBufferedJestClientFactory(httpClientConfigBuilder));

    BufferedJestHttpClient client = spy(factory.createDefaultClient());
    when(factory.createDefaultClient()).thenReturn(client);

    // when
    factory.getObject();

    // then
    verify(factory).createConnectionReaper(eq(client), any(), any());

}
 
Example 13
Source File: ExtendedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void ioReactorConfigUsesGivenReadTimeout() {

    // given
    HttpClientConfig.Builder httpClientConfigBuilder = createDefaultTestHttpClientConfigBuilder();

    int expectedReadTImeout = random.nextInt(1000) + 1;
    httpClientConfigBuilder.readTimeout(expectedReadTImeout);

    WrappedHttpClientConfig.Builder builder = createDefaultTestWrappedHttpClientConfigBuilder(httpClientConfigBuilder.build());
    ExtendedJestClientFactory factory = new ExtendedJestClientFactory(builder.build());

    // when
    IOReactorConfig ioReactorConfig = factory.createIoReactorConfig();

    // then
    assertEquals(expectedReadTImeout, ioReactorConfig.getSoTimeout());

}
 
Example 14
Source File: ExtendedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void ioReactorConfigUsesGivenConnectTimeout() {

    // given
    HttpClientConfig.Builder httpClientConfigBuilder = createDefaultTestHttpClientConfigBuilder();

    int expectedConnectTImeout = random.nextInt(1000) + 1;
    httpClientConfigBuilder.connTimeout(expectedConnectTImeout);

    WrappedHttpClientConfig.Builder builder = createDefaultTestWrappedHttpClientConfigBuilder(httpClientConfigBuilder.build());
    ExtendedJestClientFactory factory = new ExtendedJestClientFactory(builder.build());

    // when
    IOReactorConfig ioReactorConfig = factory.createIoReactorConfig();

    // then
    assertEquals(expectedConnectTImeout, ioReactorConfig.getConnectTimeout());

}
 
Example 15
Source File: BufferedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void getObjectConfiguresNodeCheckerIfDiscoveryEnabled() {

    // given
    HttpClientConfig.Builder httpClientConfigBuilder = createDefaultTestHttpClientConfig();
    httpClientConfigBuilder.discoveryEnabled(true);

    BufferedJestClientFactory factory = spy(createDefaultTestBufferedJestClientFactory(httpClientConfigBuilder));

    BufferedJestHttpClient client = spy(factory.createDefaultClient());
    when(factory.createDefaultClient()).thenReturn(client);

    // when
    factory.getObject();

    // then
    verify(factory).createNodeChecker(eq(client), any());

}
 
Example 16
Source File: BufferedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
private BufferedJestClientFactory createDefaultTestBufferedJestClientFactory(HttpClientConfig.Builder httpClientconfigBuilder) {
        BufferedJestClientFactory factory = new BufferedJestClientFactory(httpClientconfigBuilder.build());
//        factory.setHttpClientConfig(httpClientconfigBuilder.build());
        return factory;
    }
 
Example 17
Source File: JestManager.java    From EserKnife with Apache License 2.0 4 votes vote down vote up
/**
 * 获取JestClient对象
 */
public static JestClient initJestClient(String clustName,EsClusterDetail esCluster) {
    JestClientFactory factory = new JestClientFactory();
    StringBuilder key = new StringBuilder().append(clustName);
    try {
        if(esCluster != null && esCluster.getClusterList()!= null && esCluster.getClusterList().size() > 0){
            List<Cluster> clusters = esCluster.getClusterList();
            List<String> notes = new LinkedList<String>();
            for (Cluster cluster : clusters){
                notes.add("http://"+cluster.getHost()+":"+cluster.getHttpPort());
            }
            HttpClientConfig.Builder httpClientConfig = new HttpClientConfig
                    .Builder(notes)
                    .connTimeout(1500)
                    .readTimeout(15000)
                    .multiThreaded(true)
                    .discoveryEnabled(true)
                    .discoveryFrequency(15000L, TimeUnit.MILLISECONDS);
            if(StringUtils.isNotBlank(esCluster.getUserName())){
                CustomUser customUser = (CustomUser) RequestContext.getSessionAttribute(Constant.CUSTOM_USER_SESSION_NAME);
                ClusterInfo clusterInfo = CLUSTER_MAP.get(clustName);
                if(customUser != null){
                    if(customUser.isAdmin() && clusterInfo != null){
                        httpClientConfig.defaultCredentials(clusterInfo.getAdminRoleName(),
                                clusterInfo.getAdminRolePwd());
                        key.append(clusterInfo.getAdminRoleName());
                    }else if (StringUtils.isNotBlank(customUser.getEsAccout()) && StringUtils.isNotBlank(customUser.getEsPwd())) {
                        httpClientConfig.defaultCredentials(customUser.getEsAccout(),
                                customUser.getEsPwd());
                        key.append(customUser.getEsAccout());
                    } else {
                        httpClientConfig.defaultCredentials(clusterInfo != null ? clusterInfo.getMonitorRoleName() : "",
                                clusterInfo != null ? clusterInfo.getMonitorRolePwd() : null);
                        key.append(clusterInfo != null ? clusterInfo.getMonitorRoleName() : null);
                    }
                }else{
                    httpClientConfig.defaultCredentials(clusterInfo.getMonitorRoleName(),
                            clusterInfo.getMonitorRolePwd());
                    key.append(clusterInfo.getMonitorRoleName());
                }
            }
            factory.setHttpClientConfig(httpClientConfig.build());
        }
    }catch (Exception e){
        e.printStackTrace();
        LOGGER.error("初始化jestclient实例失败:",e);
    }
    JestClient jestClient = factory.getObject();
    JEST_MAP.put(key.toString(),jestClient);
    return  jestClient;
}
 
Example 18
Source File: BufferedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void getObjectDoesNotConfigureNodeCheckerIfDiscoveryNotEnabled() {

    // given
    HttpClientConfig.Builder httpClientConfigBuilder = createDefaultTestHttpClientConfig();
    httpClientConfigBuilder.discoveryEnabled(false);

    BufferedJestClientFactory factory = spy(createDefaultTestBufferedJestClientFactory(httpClientConfigBuilder));

    BufferedJestHttpClient client = spy(factory.createDefaultClient());
    when(factory.createDefaultClient()).thenReturn(client);

    // when
    factory.getObject();

    // then
    verify(factory, never()).createNodeChecker(eq(client), any());

}
 
Example 19
Source File: ExtendedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
private HttpClientConfig.Builder createDefaultTestHttpClientConfigBuilder() {
    return new HttpClientConfig.Builder("http://localhost:9200");
}
 
Example 20
Source File: XPackAuth.java    From log4j2-elasticsearch with Apache License 2.0 3 votes vote down vote up
@Override
public void configure(HttpClientConfig.Builder builder) {

    credentials.applyTo(builder);

    if (certInfo != null) {
        certInfo.applyTo(builder);
    }

}