1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.ext.opensearch.client;
20
21 import com.fasterxml.jackson.databind.SerializationFeature;
22 import com.fasterxml.jackson.databind.json.JsonMapper;
23 import java.nio.charset.StandardCharsets;
24 import java.util.Base64;
25 import java.util.List;
26 import org.apache.http.Header;
27 import org.apache.http.HttpHeaders;
28 import org.apache.http.HttpHost;
29 import org.apache.http.auth.AuthScope;
30 import org.apache.http.auth.UsernamePasswordCredentials;
31 import org.apache.http.client.CredentialsProvider;
32 import org.apache.http.impl.client.BasicCredentialsProvider;
33 import org.apache.http.message.BasicHeader;
34 import org.opensearch.client.RestClient;
35 import org.opensearch.client.RestClientBuilder;
36 import org.opensearch.client.json.jackson.JacksonJsonpMapper;
37 import org.opensearch.client.opensearch.OpenSearchClient;
38 import org.opensearch.client.transport.rest_client.RestClientTransport;
39 import org.springframework.beans.factory.DisposableBean;
40 import org.springframework.beans.factory.FactoryBean;
41
42
43
44
45 public class OpenSearchClientFactoryBean implements FactoryBean<OpenSearchClient>, DisposableBean {
46
47 private final List<HttpHost> hosts;
48
49 private String username;
50
51 private String password;
52
53 private String serviceToken;
54
55 private String apiKeyId;
56
57 private String apiKeySecret;
58
59 private RestClient restClient;
60
61 private OpenSearchClient client;
62
63 public OpenSearchClientFactoryBean(final List<HttpHost> hosts) {
64 this.hosts = hosts;
65 }
66
67 public void setUsername(final String username) {
68 this.username = username;
69 }
70
71 public void setPassword(final String password) {
72 this.password = password;
73 }
74
75 public String getServiceToken() {
76 return serviceToken;
77 }
78
79 public void setServiceToken(final String serviceToken) {
80 this.serviceToken = serviceToken;
81 }
82
83 public String getApiKeyId() {
84 return apiKeyId;
85 }
86
87 public void setApiKeyId(final String apiKeyId) {
88 this.apiKeyId = apiKeyId;
89 }
90
91 public String getApiKeySecret() {
92 return apiKeySecret;
93 }
94
95 public void setApiKeySecret(final String apiKeySecret) {
96 this.apiKeySecret = apiKeySecret;
97 }
98
99 @Override
100 public OpenSearchClient getObject() throws Exception {
101 synchronized (this) {
102 if (client == null) {
103 RestClientBuilder builder = RestClient.builder(hosts.toArray(HttpHost[]::new));
104 if (username != null && password != null) {
105 CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
106 credentialsProvider.setCredentials(
107 AuthScope.ANY, new UsernamePasswordCredentials(username, password));
108 builder.setHttpClientConfigCallback(b -> b.setDefaultCredentialsProvider(credentialsProvider));
109 } else if (serviceToken != null) {
110 builder.setDefaultHeaders(
111 new Header[] { new BasicHeader(HttpHeaders.AUTHORIZATION, "Bearer " + serviceToken) });
112 } else if (apiKeyId != null && apiKeySecret != null) {
113 String apiKeyAuth = Base64.getEncoder().encodeToString(
114 (apiKeyId + ":" + apiKeySecret).getBytes(StandardCharsets.UTF_8));
115 builder.setDefaultHeaders(
116 new Header[] { new BasicHeader(HttpHeaders.AUTHORIZATION, "ApiKey " + apiKeyAuth) });
117 }
118
119 restClient = builder.build();
120 client = new OpenSearchClient(new RestClientTransport(
121 restClient,
122 new JacksonJsonpMapper(JsonMapper.builder().
123 findAndAddModules().disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build())));
124 }
125 }
126 return client;
127 }
128
129 @Override
130 public Class<?> getObjectType() {
131 return OpenSearchClient.class;
132 }
133
134 @Override
135 public void destroy() throws Exception {
136 if (restClient != null) {
137 restClient.close();
138 }
139 }
140 }