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 java.util.List;
22 import java.util.UUID;
23 import javax.ws.rs.core.HttpHeaders;
24 import javax.ws.rs.core.MediaType;
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 MediaType mediaType;
45
46 private final String jwt;
47
48 private final String address;
49
50 private final List<?> providers;
51
52 private final TLSClientParameters tlsClientParameters;
53
54 private BatchClientFactoryBean bcfb;
55
56 public BatchRequest(
57 final MediaType mediaType,
58 final String address,
59 final List<?> providers,
60 final String jwt,
61 final TLSClientParameters tlsClientParameters) {
62
63 this.mediaType = mediaType;
64 this.jwt = jwt;
65 this.address = address;
66 this.providers = providers;
67 this.tlsClientParameters = tlsClientParameters;
68 initBatchClientFactoryBean();
69 }
70
71 private void initBatchClientFactoryBean() {
72 this.bcfb = new BatchClientFactoryBean();
73 this.bcfb.setAddress(address);
74 this.bcfb.setProviders(providers);
75 }
76
77 public <T> T getService(final Class<T> serviceClass) {
78 bcfb.setServiceClass(serviceClass);
79 T serviceInstance = bcfb.create(serviceClass);
80
81 Client client = WebClient.client(serviceInstance);
82 client.type(mediaType).accept(mediaType);
83
84 return serviceInstance;
85 }
86
87 public List<BatchRequestItem> getItems() {
88 return bcfb.getBatchRequestItems();
89 }
90
91
92
93
94
95
96
97
98 public BatchResponse commit() {
99 return commit(false);
100 }
101
102
103
104
105
106
107
108
109
110
111 public BatchResponse commit(final boolean async) {
112 String boundary = "--batch_" + UUID.randomUUID().toString();
113
114 WebClient webClient = WebClient.create(bcfb.getAddress()).path("batch").
115 header(HttpHeaders.AUTHORIZATION, "Bearer " + jwt).
116 type(RESTHeaders.multipartMixedWith(boundary.substring(2)));
117 if (async) {
118 webClient.header(RESTHeaders.PREFER, Preference.RESPOND_ASYNC);
119 }
120 if (tlsClientParameters != null) {
121 ClientConfiguration config = WebClient.getConfig(webClient);
122 HTTPConduit httpConduit = (HTTPConduit) config.getConduit();
123 httpConduit.setTlsClientParameters(tlsClientParameters);
124 }
125
126 String body = BatchPayloadGenerator.generate(bcfb.getBatchRequestItems(), boundary);
127 LOG.debug("Batch request body:\n{}", body);
128
129 initBatchClientFactoryBean();
130
131 return new BatchResponse(boundary, jwt, tlsClientParameters, webClient.post(body));
132 }
133 }