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.io.ByteArrayInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URI;
25 import java.nio.charset.StandardCharsets;
26 import java.util.List;
27 import javax.ws.rs.core.HttpHeaders;
28 import javax.ws.rs.core.Response;
29 import org.apache.cxf.configuration.jsse.TLSClientParameters;
30 import org.apache.cxf.helpers.IOUtils;
31 import org.apache.cxf.jaxrs.client.ClientConfiguration;
32 import org.apache.cxf.jaxrs.client.WebClient;
33 import org.apache.cxf.transport.http.HTTPConduit;
34 import org.apache.syncope.common.rest.api.RESTHeaders;
35 import org.apache.syncope.common.rest.api.batch.BatchPayloadParser;
36 import org.apache.syncope.common.rest.api.batch.BatchResponseItem;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43 public class BatchResponse {
44
45 private static final Logger LOG = LoggerFactory.getLogger(BatchResponse.class);
46
47
48
49
50
51
52
53
54
55
56
57 public static Response poll(
58 final URI monitor,
59 final String jwt,
60 final String boundary,
61 final TLSClientParameters tlsClientParameters) {
62
63 WebClient webClient = WebClient.create(monitor).
64 header(HttpHeaders.AUTHORIZATION, "Bearer " + jwt).
65 type(RESTHeaders.multipartMixedWith(boundary.substring(2)));
66 if (tlsClientParameters != null) {
67 ClientConfiguration config = WebClient.getConfig(webClient);
68 HTTPConduit httpConduit = (HTTPConduit) config.getConduit();
69 httpConduit.setTlsClientParameters(tlsClientParameters);
70 }
71
72 return webClient.get();
73 }
74
75
76
77
78
79
80
81
82 public static List<BatchResponseItem> getItems(final Response response) throws IOException {
83 String body = IOUtils.toString((InputStream) response.getEntity(), StandardCharsets.UTF_8.name());
84 LOG.debug("Batch response body:\n{}", body);
85
86 return BatchPayloadParser.parse(
87 new ByteArrayInputStream(body.getBytes()),
88 response.getMediaType(),
89 new BatchResponseItem());
90 }
91
92 private final String boundary;
93
94 private final String jwt;
95
96 private final URI monitor;
97
98 private final TLSClientParameters tlsClientParameters;
99
100 private Response response;
101
102 public BatchResponse(
103 final String boundary,
104 final String jwt,
105 final TLSClientParameters tlsClientParameters,
106 final Response response) {
107
108 this.boundary = boundary;
109 this.jwt = jwt;
110 this.tlsClientParameters = tlsClientParameters;
111 this.monitor = response.getLocation();
112 this.response = response;
113 }
114
115 public String getBoundary() {
116 return boundary;
117 }
118
119 public URI getMonitor() {
120 return monitor;
121 }
122
123
124
125
126
127
128 public Response getResponse() {
129 return response;
130 }
131
132
133
134
135
136
137 public Response poll() {
138 response = poll(monitor, jwt, boundary, tlsClientParameters);
139 return response;
140 }
141
142
143
144
145
146
147
148 public List<BatchResponseItem> getItems() throws IOException {
149 return getItems(response);
150 }
151 }