View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Encapsulates the Batch request management via CXF Proxy Client.
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       * Sends the current request, with items accumulated by invoking methods on proxies obtained via
89       * {@link #getService(java.lang.Class)}, to the Batch service, and awaits for synchronous response.
90       * It also clears out the accumulated items, in case of reuse of this instance for subsequent requests.
91       *
92       * @return batch response
93       */
94      public BatchResponse commit() {
95          return commit(false);
96      }
97  
98      /**
99       * Sends the current request, with items accumulated by invoking methods on proxies obtained via
100      * {@link #getService(java.lang.Class)}, to the Batch service, and awaits for a synchronous or asynchronous
101      * response, depending on the {@code async} parameter.
102      * It also clears out the accumulated items, in case of reuse of this instance for subsequent requests.
103      *
104      * @param async whether asynchronous Batch process is requested, or not
105      * @return batch response
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 }