1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.client.lib;
20
21 import com.fasterxml.jackson.jakarta.rs.json.JacksonJsonProvider;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Optional;
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.cxf.configuration.jsse.TLSClientParameters;
27 import org.apache.cxf.ext.logging.LoggingFeature;
28 import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
29 import org.apache.cxf.transports.http.configuration.ConnectionType;
30 import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
31 import org.apache.syncope.common.lib.jackson.SyncopeJsonMapper;
32 import org.apache.syncope.common.rest.api.DateParamConverterProvider;
33 import org.apache.syncope.common.rest.api.RESTHeaders;
34
35
36
37
38
39 public class SyncopeClientFactoryBean {
40
41 private JacksonJsonProvider jsonProvider;
42
43 private RestClientExceptionMapper exceptionMapper;
44
45 private String address;
46
47 private String domain;
48
49 private boolean useCompression;
50
51 private HTTPClientPolicy httpClientPolicy;
52
53 private TLSClientParameters tlsClientParameters;
54
55 private JAXRSClientFactoryBean restClientFactoryBean;
56
57 protected static JacksonJsonProvider defaultJsonProvider() {
58 return new JacksonJsonProvider(new SyncopeJsonMapper());
59 }
60
61 protected static RestClientExceptionMapper defaultExceptionMapper() {
62 return new RestClientExceptionMapper();
63 }
64
65 protected static HTTPClientPolicy defaultHTTPClientPolicy() {
66 HTTPClientPolicy policy = new HTTPClientPolicy();
67 policy.setConnection(ConnectionType.CLOSE);
68 return policy;
69 }
70
71 protected JAXRSClientFactoryBean defaultRestClientFactoryBean() {
72 JAXRSClientFactoryBean defaultRestClientFactoryBean = new JAXRSClientFactoryBean();
73 defaultRestClientFactoryBean.setHeaders(new HashMap<>());
74
75 if (StringUtils.isBlank(address)) {
76 throw new IllegalArgumentException("Property 'address' is missing");
77 }
78 defaultRestClientFactoryBean.setAddress(address);
79
80 if (StringUtils.isNotBlank(domain)) {
81 defaultRestClientFactoryBean.getHeaders().put(RESTHeaders.DOMAIN, List.of(domain));
82 }
83
84 defaultRestClientFactoryBean.setThreadSafe(true);
85 defaultRestClientFactoryBean.setInheritHeaders(true);
86
87 defaultRestClientFactoryBean.setFeatures(List.of(new LoggingFeature()));
88
89 defaultRestClientFactoryBean.setProviders(List.of(
90 new DateParamConverterProvider(),
91 getJsonProvider(),
92 getExceptionMapper()));
93
94 return defaultRestClientFactoryBean;
95 }
96
97 public JacksonJsonProvider getJsonProvider() {
98 return Optional.ofNullable(jsonProvider).orElseGet(SyncopeClientFactoryBean::defaultJsonProvider);
99 }
100
101 public void setJsonProvider(final JacksonJsonProvider jsonProvider) {
102 this.jsonProvider = jsonProvider;
103 }
104
105 public RestClientExceptionMapper getExceptionMapper() {
106 return Optional.ofNullable(exceptionMapper).orElseGet(SyncopeClientFactoryBean::defaultExceptionMapper);
107 }
108
109 public SyncopeClientFactoryBean setExceptionMapper(final RestClientExceptionMapper exceptionMapper) {
110 this.exceptionMapper = exceptionMapper;
111 return this;
112 }
113
114 public String getAddress() {
115 return address;
116 }
117
118 public SyncopeClientFactoryBean setAddress(final String address) {
119 this.address = address;
120 return this;
121 }
122
123 public String getDomain() {
124 return domain;
125 }
126
127 public SyncopeClientFactoryBean setDomain(final String domain) {
128 this.domain = domain;
129 return this;
130 }
131
132
133
134
135
136
137
138 public SyncopeClientFactoryBean setUseCompression(final boolean useCompression) {
139 this.useCompression = useCompression;
140 return this;
141 }
142
143 public boolean isUseCompression() {
144 return useCompression;
145 }
146
147 public SyncopeClientFactoryBean setHttpClientPolicy(final HTTPClientPolicy httpClientPolicy) {
148 this.httpClientPolicy = httpClientPolicy;
149 return this;
150 }
151
152 public HTTPClientPolicy getHttpClientPolicy() {
153 return Optional.ofNullable(httpClientPolicy).orElseGet(SyncopeClientFactoryBean::defaultHTTPClientPolicy);
154 }
155
156
157
158
159
160
161
162 public SyncopeClientFactoryBean setTlsClientParameters(final TLSClientParameters tlsClientParameters) {
163 this.tlsClientParameters = tlsClientParameters;
164 return this;
165 }
166
167 public TLSClientParameters getTlsClientParameters() {
168 return tlsClientParameters;
169 }
170
171 public JAXRSClientFactoryBean getRestClientFactoryBean() {
172 return Optional.ofNullable(restClientFactoryBean).orElseGet(this::defaultRestClientFactoryBean);
173 }
174
175 public SyncopeClientFactoryBean setRestClientFactoryBean(final JAXRSClientFactoryBean restClientFactoryBean) {
176 this.restClientFactoryBean = restClientFactoryBean;
177 return this;
178 }
179
180
181
182
183
184
185
186
187
188
189 public SyncopeClient create(final String username, final String password) {
190 return create(new ObtainingJWTAuthenticationHandler(username, password));
191 }
192
193
194
195
196
197
198
199
200
201
202
203 public SyncopeClient create(final String username, final String password, final String otp) {
204 return create(new ObtainingJWTAuthenticationHandler(username, password + ":" + otp));
205 }
206
207
208
209
210
211
212
213
214
215 public SyncopeClient create(final String jwt) {
216 return create(new JWTAuthenticationHandler(jwt));
217 }
218
219
220
221
222
223
224
225 public SyncopeClient create(final AuthenticationHandler handler) {
226 return new SyncopeClient(
227 getRestClientFactoryBean(),
228 getExceptionMapper(),
229 handler,
230 useCompression,
231 getHttpClientPolicy(),
232 tlsClientParameters);
233 }
234
235
236
237
238
239
240
241
242 public SyncopeAnonymousClient createAnonymous(final String username, final String password) {
243 return new SyncopeAnonymousClient(
244 getRestClientFactoryBean(),
245 getExceptionMapper(),
246 new BasicAuthenticationHandler(username, password),
247 useCompression,
248 getHttpClientPolicy(),
249 tlsClientParameters);
250 }
251 }