MCPcopy Create free account
hub / github.com/Hakky54/java-tutorials / App

Class App

elasticsearch-with-ssl/src/main/java/nl/altindag/ssl/es/App.java:38–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36import java.util.List;
37
38public class App {
39
40 public static void main(String[] args) throws IOException {
41 Path certificatePath = Paths.get("elasticsearch-with-ssl/elasticsearch-docker/ca.crt").normalize().toAbsolutePath();
42 List<Certificate> certificates = CertificateUtils.loadCertificate(certificatePath);
43 SSLFactory sslFactory = SSLFactory.builder()
44 .withTrustMaterial(certificates)
45 .build();
46
47 CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
48 credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "PleaseChangeMe"));
49
50 RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "https"))
51 .setHttpClientConfigCallback(httpClientBuilder ->
52 httpClientBuilder.setSSLContext(sslFactory.getSslContext()).setDefaultCredentialsProvider(credentialsProvider))
53 .build();
54
55 ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
56 ElasticsearchClient client = new ElasticsearchClient(transport);
57
58 HealthResponse healthResponse = client.cluster().health();
59 System.out.printf("Elasticsearch status is: [%s]", healthResponse.status());
60
61 restClient.close();
62 }
63
64}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected