You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2020/05/14 15:33:52 UTC

[syncope] branch master updated: Utility methods in BatchResponse

This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/master by this push:
     new 55d2f23  Utility methods in BatchResponse
55d2f23 is described below

commit 55d2f2381e51d2e0c21aa5b41748f4fbcabe64a1
Author: Francesco Chicchiriccò <il...@apache.org>
AuthorDate: Thu May 14 17:32:05 2020 +0200

    Utility methods in BatchResponse
---
 .../client/console/widgets/JobActionPanel.java     |  1 -
 .../syncope/client/lib/batch/BatchResponse.java    | 75 ++++++++++++++++------
 2 files changed, 55 insertions(+), 21 deletions(-)

diff --git a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/widgets/JobActionPanel.java b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/widgets/JobActionPanel.java
index 3d44525..1e1d551 100644
--- a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/widgets/JobActionPanel.java
+++ b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/widgets/JobActionPanel.java
@@ -152,6 +152,5 @@ public class JobActionPanel extends WizardMgtPanel<Serializable> {
         public AjaxRequestTarget getTarget() {
             return target;
         }
-
     }
 }
diff --git a/client/idrepo/lib/src/main/java/org/apache/syncope/client/lib/batch/BatchResponse.java b/client/idrepo/lib/src/main/java/org/apache/syncope/client/lib/batch/BatchResponse.java
index 5b6ee8b..90882d3 100644
--- a/client/idrepo/lib/src/main/java/org/apache/syncope/client/lib/batch/BatchResponse.java
+++ b/client/idrepo/lib/src/main/java/org/apache/syncope/client/lib/batch/BatchResponse.java
@@ -44,6 +44,51 @@ public class BatchResponse {
 
     private static final Logger LOG = LoggerFactory.getLogger(BatchResponse.class);
 
+    /**
+     * If asynchronous processing was requested, queries the monitor URI.
+     *
+     * @param monitor monitor URI
+     * @param jwt authorization JWT
+     * @param boundary mutipart / mixed boundary
+     * @param tlsClientParameters (optional) TLS client parameters
+     *
+     * @return the last Response received from the Batch service
+     */
+    public static Response poll(
+            final URI monitor,
+            final String jwt,
+            final String boundary,
+            final TLSClientParameters tlsClientParameters) {
+
+        WebClient webClient = WebClient.create(monitor).
+                header(HttpHeaders.AUTHORIZATION, "Bearer " + jwt).
+                type(RESTHeaders.multipartMixedWith(boundary.substring(2)));
+        if (tlsClientParameters != null) {
+            ClientConfiguration config = WebClient.getConfig(webClient);
+            HTTPConduit httpConduit = (HTTPConduit) config.getConduit();
+            httpConduit.setTlsClientParameters(tlsClientParameters);
+        }
+
+        return webClient.get();
+    }
+
+    /**
+     * Parses the given Response into a list of {@link BatchResponseItem}s.
+     *
+     * @param response response to extract items from
+     * @return the Batch Response parsed as list of {@link BatchResponseItem}s
+     * @throws IOException if there are issues when reading the response body
+     */
+    public static List<BatchResponseItem> getItems(final Response response) throws IOException {
+        String body = IOUtils.toString((InputStream) response.getEntity(), StandardCharsets.UTF_8.name());
+        LOG.debug("Batch response body:\n{}", body);
+
+        return BatchPayloadParser.parse(
+                new ByteArrayInputStream(body.getBytes()),
+                response.getMediaType(),
+                new BatchResponseItem());
+    }
+
     private final String boundary;
 
     private final String jwt;
@@ -67,6 +112,14 @@ public class BatchResponse {
         this.response = response;
     }
 
+    public String getBoundary() {
+        return boundary;
+    }
+
+    public URI getMonitor() {
+        return monitor;
+    }
+
     /**
      * Gives the last Response received from the Batch service.
      *
@@ -82,19 +135,7 @@ public class BatchResponse {
      * @return the last Response received from the Batch service
      */
     public Response poll() {
-        if (monitor != null) {
-            WebClient webClient = WebClient.create(monitor).
-                    header(HttpHeaders.AUTHORIZATION, "Bearer " + jwt).
-                    type(RESTHeaders.multipartMixedWith(boundary.substring(2)));
-            if (tlsClientParameters != null) {
-                ClientConfiguration config = WebClient.getConfig(webClient);
-                HTTPConduit httpConduit = (HTTPConduit) config.getConduit();
-                httpConduit.setTlsClientParameters(tlsClientParameters);
-            }
-
-            response = webClient.get();
-        }
-
+        response = poll(monitor, jwt, boundary, tlsClientParameters);
         return response;
     }
 
@@ -105,12 +146,6 @@ public class BatchResponse {
      * @throws IOException if there are issues when reading the response body
      */
     public List<BatchResponseItem> getItems() throws IOException {
-        String body = IOUtils.toString((InputStream) response.getEntity(), StandardCharsets.UTF_8.name());
-        LOG.debug("Batch response body:\n{}", body);
-
-        return BatchPayloadParser.parse(
-                new ByteArrayInputStream(body.getBytes()),
-                response.getMediaType(),
-                new BatchResponseItem());
+        return getItems(response);
     }
 }