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.batch;
20
21 import jakarta.ws.rs.core.HttpHeaders;
22 import jakarta.ws.rs.core.MediaType;
23 import java.util.List;
24 import java.util.UUID;
25 import org.apache.cxf.configuration.jsse.TLSClientParameters;
26 import org.apache.cxf.jaxrs.client.Client;
27 import org.apache.cxf.jaxrs.client.ClientConfiguration;
28 import org.apache.cxf.jaxrs.client.WebClient;
29 import org.apache.cxf.transport.http.HTTPConduit;
30 import org.apache.syncope.common.rest.api.Preference;
31 import org.apache.syncope.common.rest.api.RESTHeaders;
32 import org.apache.syncope.common.rest.api.batch.BatchPayloadGenerator;
33 import org.apache.syncope.common.rest.api.batch.BatchRequestItem;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40 public class BatchRequest {
41
42 private static final Logger LOG = LoggerFactory.getLogger(BatchRequest.class);
43
44 private final String jwt;
45
46 private final String address;
47
48 private final List<?> providers;
49
50 private final TLSClientParameters tlsClientParameters;
51
52 private BatchClientFactoryBean bcfb;
53
54 public BatchRequest(
55 final String address,
56 final List<?> providers,
57 final String jwt,
58 final TLSClientParameters tlsClientParameters) {
59
60 this.jwt = jwt;
61 this.address = address;
62 this.providers = providers;
63 this.tlsClientParameters = tlsClientParameters;
64 initBatchClientFactoryBean();
65 }
66
67 private void initBatchClientFactoryBean() {
68 this.bcfb = new BatchClientFactoryBean();
69 this.bcfb.setAddress(address);
70 this.bcfb.setProviders(providers);
71 }
72
73 public <T> T getService(final Class<T> serviceClass) {
74 bcfb.setServiceClass(serviceClass);
75 T serviceInstance = bcfb.create(serviceClass);
76
77 Client client = WebClient.client(serviceInstance);
78 client.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
79
80 return serviceInstance;
81 }
82
83 public List<BatchRequestItem> getItems() {
84 return bcfb.getBatchRequestItems();
85 }
86
87
88
89
90
91
92
93
94 public BatchResponse commit() {
95 return commit(false);
96 }
97
98
99
100
101
102
103
104
105
106
107 public BatchResponse commit(final boolean async) {
108 String boundary = "--batch_" + UUID.randomUUID();
109
110 WebClient webClient = WebClient.create(bcfb.getAddress()).path("batch").
111 header(HttpHeaders.AUTHORIZATION, "Bearer " + jwt).
112 type(RESTHeaders.multipartMixedWith(boundary.substring(2)));
113 if (async) {
114 webClient.header(RESTHeaders.PREFER, Preference.RESPOND_ASYNC);
115 }
116 if (tlsClientParameters != null) {
117 ClientConfiguration config = WebClient.getConfig(webClient);
118 HTTPConduit httpConduit = (HTTPConduit) config.getConduit();
119 httpConduit.setTlsClientParameters(tlsClientParameters);
120 }
121
122 String body = BatchPayloadGenerator.generate(bcfb.getBatchRequestItems(), boundary);
123 LOG.debug("Batch request body:\n{}", body);
124
125 initBatchClientFactoryBean();
126
127 return new BatchResponse(boundary, jwt, tlsClientParameters, webClient.post(body));
128 }
129 }