use of org.elasticsearch.client.RestClientBuilder in project janusgraph by JanusGraph.
the class RestClientSetup method getRestClient.
protected RestClient getRestClient(HttpHost[] hosts, Configuration config) {
final RestClientBuilder restClientBuilder = getRestClientBuilder(hosts);
final HttpClientConfigCallback httpClientConfigCallback = getHttpClientConfigCallback(config);
if (httpClientConfigCallback != null) {
restClientBuilder.setHttpClientConfigCallback(httpClientConfigCallback);
}
final RequestConfigCallback requestConfigCallback = getRequestConfigCallback(config);
if (requestConfigCallback != null) {
restClientBuilder.setRequestConfigCallback(requestConfigCallback);
}
return restClientBuilder.build();
}
use of org.elasticsearch.client.RestClientBuilder in project janusgraph by JanusGraph.
the class RestClientSetup method getHttpClientConfigCallback.
/**
* <p>
* Returns the callback for customizing {@link CloseableHttpAsyncClient} or null if no
* customization is needed.
* </p>
* <p>
* See {@link RestClientBuilder#setHttpClientConfigCallback(HttpClientConfigCallback)} for more details.
* </p>
*
* @param config
* ES index configuration
* @return callback or null if the client customization is not needed
*/
protected HttpClientConfigCallback getHttpClientConfigCallback(Configuration config) {
final List<HttpClientConfigCallback> callbackList = new LinkedList<>();
final HttpAuthTypes authType = ConfigOption.getEnumValue(config.get(ElasticSearchIndex.ES_HTTP_AUTH_TYPE), HttpAuthTypes.class);
log.debug("Configuring HTTP(S) authentication type {}", authType);
switch(authType) {
case BASIC:
callbackList.add(new BasicAuthHttpClientConfigCallback(config.has(ElasticSearchIndex.ES_HTTP_AUTH_REALM) ? config.get(ElasticSearchIndex.ES_HTTP_AUTH_REALM) : "", config.get(ElasticSearchIndex.ES_HTTP_AUTH_USERNAME), config.get(ElasticSearchIndex.ES_HTTP_AUTH_PASSWORD)));
break;
case CUSTOM:
callbackList.add(getCustomAuthenticator(config.get(ElasticSearchIndex.ES_HTTP_AUTHENTICATOR_CLASS), config.get(ElasticSearchIndex.ES_HTTP_AUTHENTICATOR_ARGS)));
break;
case NONE:
break;
default:
// not expected
throw new IllegalArgumentException("Authentication type \"" + authType + "\" is not implemented");
}
if (config.has(ElasticSearchIndex.CLIENT_KEEP_ALIVE)) {
callbackList.add(new ConnectionKeepAliveConfigCallback(config.get(ElasticSearchIndex.CLIENT_KEEP_ALIVE)));
}
if (config.get(ElasticSearchIndex.SSL_ENABLED)) {
// Custom SSL configuration
final Builder sslConfCBBuilder = getSSLConfigurationCallbackBuilder();
boolean configureSSL = false;
if (config.has(ElasticSearchIndex.SSL_TRUSTSTORE_LOCATION)) {
sslConfCBBuilder.withTrustStore(config.get(ElasticSearchIndex.SSL_TRUSTSTORE_LOCATION), config.get(ElasticSearchIndex.SSL_TRUSTSTORE_PASSWORD));
configureSSL = true;
}
if (config.has(ElasticSearchIndex.SSL_KEYSTORE_LOCATION)) {
final String keystorePassword = config.get(ElasticSearchIndex.SSL_KEYSTORE_PASSWORD);
sslConfCBBuilder.withKeyStore(config.get(ElasticSearchIndex.SSL_KEYSTORE_LOCATION), keystorePassword, config.has(ElasticSearchIndex.SSL_KEY_PASSWORD) ? config.get(ElasticSearchIndex.SSL_KEY_PASSWORD) : keystorePassword);
configureSSL = true;
}
if (config.has(ElasticSearchIndex.SSL_DISABLE_HOSTNAME_VERIFICATION) && config.get(ElasticSearchIndex.SSL_DISABLE_HOSTNAME_VERIFICATION)) {
log.warn("SSL hostname verification is disabled, Elasticsearch HTTPS connections may not be secure");
sslConfCBBuilder.disableHostNameVerification();
configureSSL = true;
}
if (config.has(ElasticSearchIndex.SSL_ALLOW_SELF_SIGNED_CERTIFICATES) && config.get(ElasticSearchIndex.SSL_ALLOW_SELF_SIGNED_CERTIFICATES)) {
log.warn("Self-signed SSL certificate support is enabled, Elasticsearch HTTPS connections may not be secure");
sslConfCBBuilder.allowSelfSignedCertificates();
configureSSL = true;
}
if (configureSSL) {
callbackList.add(sslConfCBBuilder.build());
}
}
if (callbackList.isEmpty()) {
return null;
}
// will execute the chain of individual callbacks
return httpClientBuilder -> {
for (HttpClientConfigCallback cb : callbackList) {
cb.customizeHttpClient(httpClientBuilder);
}
return httpClientBuilder;
};
}
use of org.elasticsearch.client.RestClientBuilder in project jnosql-diana-driver by eclipse.
the class ElasticsearchDocumentConfiguration method get.
@Override
public ElasticsearchDocumentCollectionManagerFactory get(Settings settings) throws NullPointerException {
requireNonNull(settings, "settings is required");
Map<String, String> configurations = new HashMap<>();
settings.forEach((key, value) -> configurations.put(key, value.toString()));
configurations.keySet().stream().filter(k -> k.startsWith(HOST_PREFIX)).sorted().map(h -> ElasticsearchAddress.of(configurations.get(h), DEFAULT_PORT)).map(ElasticsearchAddress::toHttpHost).forEach(httpHosts::add);
RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]));
builder.setDefaultHeaders(headers.stream().toArray(Header[]::new));
String maxRetry = configurations.get("elasticsearch-maxRetryTimeoutMillis");
if (maxRetry != null) {
maxRetryTimoutMillis = Integer.valueOf(maxRetry);
}
builder.setMaxRetryTimeoutMillis(maxRetryTimoutMillis);
RestHighLevelClient client = new RestHighLevelClient(builder);
return new ElasticsearchDocumentCollectionManagerFactory(client);
}
use of org.elasticsearch.client.RestClientBuilder in project presto by prestodb.
the class ElasticsearchClient method createClient.
private static RestHighLevelClient createClient(ElasticsearchConfig config, Optional<AwsSecurityConfig> awsSecurityConfig) {
RestClientBuilder builder = RestClient.builder(new HttpHost(config.getHost(), config.getPort(), config.isTlsEnabled() ? "https" : "http")).setMaxRetryTimeoutMillis((int) config.getMaxRetryTime().toMillis());
builder.setHttpClientConfigCallback(ignored -> {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(toIntExact(config.getConnectTimeout().toMillis())).setSocketTimeout(toIntExact(config.getRequestTimeout().toMillis())).build();
IOReactorConfig reactorConfig = IOReactorConfig.custom().setIoThreadCount(config.getHttpThreadCount()).build();
// the client builder passed to the call-back is configured to use system properties, which makes it
// impossible to configure concurrency settings, so we need to build a new one from scratch
HttpAsyncClientBuilder clientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfig).setDefaultIOReactorConfig(reactorConfig).setMaxConnPerRoute(config.getMaxHttpConnections()).setMaxConnTotal(config.getMaxHttpConnections());
if (config.isTlsEnabled()) {
buildSslContext(config.getKeystorePath(), config.getKeystorePassword(), config.getTrustStorePath(), config.getTruststorePassword()).ifPresent(clientBuilder::setSSLContext);
if (config.isVerifyHostnames()) {
clientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
}
}
awsSecurityConfig.ifPresent(securityConfig -> clientBuilder.addInterceptorLast(new AwsRequestSigner(securityConfig.getRegion(), getAwsCredentialsProvider(securityConfig))));
return clientBuilder;
});
return new RestHighLevelClient(builder);
}
use of org.elasticsearch.client.RestClientBuilder in project vorto by eclipse.
the class ElasticSearchConfiguration method awsIndexingClient.
@Bean
@Profile({ "prod", "int" })
public RestHighLevelClient awsIndexingClient() {
logger.info("Creating an elastic server client with config(serviceName=" + serviceName + " region=" + region + " aesEndpoint=" + aesEndpoint);
AWS4Signer signer = new AWS4Signer();
signer.setServiceName(serviceName);
signer.setRegionName(region);
HttpRequestInterceptor interceptor = new AWSRequestSigningApacheInterceptor(serviceName, signer, credentialsProvider);
RestClientBuilder builder = RestClient.builder(HttpHost.create(aesEndpoint)).setHttpClientConfigCallback(httpClientConfig(getProxy(), interceptor));
return new RestHighLevelClient(builder);
}
Aggregations