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 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   * Encapsulates the Batch response management via CXF Proxy Client.
42   */
43  public class BatchResponse {
44  
45      private static final Logger LOG = LoggerFactory.getLogger(BatchResponse.class);
46  
47      /**
48       * If asynchronous processing was requested, queries the monitor URI.
49       *
50       * @param monitor monitor URI
51       * @param jwt authorization JWT
52       * @param boundary mutipart / mixed boundary
53       * @param tlsClientParameters (optional) TLS client parameters
54       *
55       * @return the last Response received from the Batch service
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       * Parses the given Response into a list of {@link BatchResponseItem}s.
77       *
78       * @param response response to extract items from
79       * @return the Batch Response parsed as list of {@link BatchResponseItem}s
80       * @throws IOException if there are issues when reading the response body
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      * Gives the last Response received from the Batch service.
125      *
126      * @return the last Response received from the Batch service
127      */
128     public Response getResponse() {
129         return response;
130     }
131 
132     /**
133      * If asynchronous processing was requested, queries the monitor URI.
134      *
135      * @return the last Response received from the Batch service
136      */
137     public Response poll() {
138         response = poll(monitor, jwt, boundary, tlsClientParameters);
139         return response;
140     }
141 
142     /**
143      * Parses the latest Response received into a list of {@link BatchResponseItem}s.
144      *
145      * @return the Batch Response parsed as list of {@link BatchResponseItem}s
146      * @throws IOException if there are issues when reading the response body
147      */
148     public List<BatchResponseItem> getItems() throws IOException {
149         return getItems(response);
150     }
151 }