You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2022/04/12 22:45:46 UTC

[isis] branch master updated: ISIS-2999: converts command replay service to new rest client

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

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


The following commit(s) were added to refs/heads/master by this push:
     new ef3e852104 ISIS-2999: converts command replay service to new rest client
ef3e852104 is described below

commit ef3e8521042a5a2a9dcc9255801aeaedc25de37d
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Apr 13 00:45:40 2022 +0200

    ISIS-2999: converts command replay service to new rest client
---
 extensions/core/command-replay/secondary/pom.xml   |   6 ++
 .../secondary/fetch/CommandFetcher.java            | 110 +++++++++++----------
 .../secondary/fetch/_LegacyClient.java             |  56 -----------
 .../secondary/fetch/CommandFetcher_Test.java       |  40 +++++---
 .../restfulobjects/client/ResponseDigest.java      |  10 +-
 .../restfulobjects/client/RestfulClient.java       |  24 +++++
 6 files changed, 115 insertions(+), 131 deletions(-)

diff --git a/extensions/core/command-replay/secondary/pom.xml b/extensions/core/command-replay/secondary/pom.xml
index 782411a6ed..62de394642 100644
--- a/extensions/core/command-replay/secondary/pom.xml
+++ b/extensions/core/command-replay/secondary/pom.xml
@@ -77,6 +77,12 @@
             <artifactId>isis-core-internaltestsupport</artifactId>
             <scope>test</scope>
         </dependency>
+        
+        <dependency>
+			<groupId>org.glassfish.jersey.ext</groupId>
+			<artifactId>jersey-spring5</artifactId>
+			<scope>test</scope>
+		</dependency>
 
     </dependencies>
 
diff --git a/extensions/core/command-replay/secondary/src/main/java/org/apache/isis/extensions/commandreplay/secondary/fetch/CommandFetcher.java b/extensions/core/command-replay/secondary/src/main/java/org/apache/isis/extensions/commandreplay/secondary/fetch/CommandFetcher.java
index a4d2c0a76d..758a42426f 100644
--- a/extensions/core/command-replay/secondary/src/main/java/org/apache/isis/extensions/commandreplay/secondary/fetch/CommandFetcher.java
+++ b/extensions/core/command-replay/secondary/src/main/java/org/apache/isis/extensions/commandreplay/secondary/fetch/CommandFetcher.java
@@ -18,7 +18,6 @@
  */
 package org.apache.isis.extensions.commandreplay.secondary.fetch;
 
-import java.net.URI;
 import java.util.Collections;
 import java.util.List;
 import java.util.UUID;
@@ -26,23 +25,24 @@ import java.util.UUID;
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriBuilder;
 
 import org.springframework.lang.Nullable;
 import org.springframework.stereotype.Service;
 
 import org.apache.isis.applib.annotation.PriorityPrecedence;
+import org.apache.isis.applib.client.SuppressionType;
 import org.apache.isis.applib.services.jaxb.JaxbService;
 import org.apache.isis.applib.services.jaxb.JaxbService.Simple;
 import org.apache.isis.extensions.commandlog.model.command.CommandModel;
 import org.apache.isis.extensions.commandreplay.secondary.SecondaryStatus;
 import org.apache.isis.extensions.commandreplay.secondary.StatusException;
 import org.apache.isis.extensions.commandreplay.secondary.config.SecondaryConfig;
-import org.apache.isis.extensions.commandreplay.secondary.fetch._LegacyClient.JaxRsClient;
-import org.apache.isis.extensions.commandreplay.secondary.fetch._LegacyClient.JaxRsResponse;
 import org.apache.isis.schema.cmd.v2.CommandDto;
 import org.apache.isis.schema.cmd.v2.CommandsDto;
+import org.apache.isis.viewer.restfulobjects.client.RestfulClient;
+import org.apache.isis.viewer.restfulobjects.client.RestfulClientConfig;
 
+import lombok.val;
 import lombok.extern.log4j.Log4j2;
 
 
@@ -56,7 +56,8 @@ import lombok.extern.log4j.Log4j2;
 public class CommandFetcher {
 
     static final String URL_SUFFIX =
-            "services/isisExtensionsCommandReplayPrimary.CommandRetrievalService/actions/findCommandsOnPrimaryFrom/invoke";
+            "services/isisExtensionsCommandReplayPrimary."
+            + "CommandRetrievalService/actions/findCommandsOnPrimaryFrom/invoke";
 
 
     /**
@@ -89,11 +90,7 @@ public class CommandFetcher {
 
         log.debug("finding commands on primary ...");
 
-        final URI uri = buildUri(transactionId);
-
-        final _LegacyClient.JaxRsResponse response = callPrimary(uri);
-
-        final CommandsDto commandsDto = unmarshal(response, uri);
+        final CommandsDto commandsDto = callPrimary(transactionId);
 
         final int size = commandsDto.getCommandDto().size();
         if(size == 0) {
@@ -103,69 +100,74 @@ public class CommandFetcher {
     }
 
 
-    private URI buildUri(final UUID interactionId) {
-        final UriBuilder uriBuilder = UriBuilder.fromUri(
+    private String buildUri(final UUID interactionId) {
+        val uri =
                 interactionId != null
                         ? String.format(
-                            "%s%s?interactionId=%s&batchSize=%d",
-                            secondaryConfig.getPrimaryBaseUrlRestful(), URL_SUFFIX, interactionId, secondaryConfig.getBatchSize())
+                            "%s?interactionId=%s&batchSize=%d",
+                            URL_SUFFIX, interactionId, secondaryConfig.getBatchSize())
                         : String.format(
-                            "%s%s?batchSize=%d",
-                            secondaryConfig.getPrimaryBaseUrlRestful(), URL_SUFFIX, secondaryConfig.getBatchSize())
-        );
-        final URI uri = uriBuilder.build();
+                            "%s?batchSize=%d",
+                            URL_SUFFIX, secondaryConfig.getBatchSize());
         log.info("uri = {}", uri);
         return uri;
     }
 
-    private _LegacyClient.JaxRsResponse callPrimary(final URI uri) throws StatusException {
-        final _LegacyClient.JaxRsResponse response;
-        final _LegacyClient.JaxRsClient jaxRsClient = new _LegacyClient.JaxRsClientDefault();
-        try {
-            final String user = secondaryConfig.getPrimaryUser();
-            final String password = secondaryConfig.getPrimaryPassword();
-            response = jaxRsClient.get(uri, CommandsDto.class, JaxRsClient.ReprType.ACTION_RESULT, user, password);
-            int status = response.getStatus();
-            if(status != Response.Status.OK.getStatusCode()) {
-                final String entity = readEntityFrom(response);
-                if(entity != null) {
-                    log.warn("status: {}, entity: \n{}", status, entity);
-                } else {
-                    log.warn("status: {}, unable to read entity from response", status);
-                }
-                throw new StatusException(SecondaryStatus.REST_CALL_FAILING);
-            }
-        } catch(Exception ex) {
-            log.warn("rest call failed", ex);
-            throw new StatusException(SecondaryStatus.REST_CALL_FAILING, ex);
-        }
-        return response;
+    // package private in support of JUnit
+    CommandsDto callPrimary(final UUID transactionId) throws StatusException {
+
+        val endpointUri = buildUri(transactionId);
+        val client = newClient(secondaryConfig, useRequestDebugLogging );
+        val request = client.request(
+                endpointUri,
+                SuppressionType.RO);
+
+        final Response response = request.get();
+        val digest = client.digest(response, String.class)
+                .mapFailure(failure->{
+                    log.warn("rest call failed", failure);
+                    return new StatusException(SecondaryStatus.REST_CALL_FAILING);
+                })
+                .ifFailureFail();
+
+        return unmarshal(digest.getValue().orElse("<unable to read from response entity>"), endpointUri);
     }
 
-    private CommandsDto unmarshal(final JaxRsResponse response, final URI uri) throws StatusException {
+    private CommandsDto unmarshal(final String rawValue, final String endpointUri) throws StatusException {
         CommandsDto commandsDto;
-        String entity = "<unable to read from response entity>";
         try {
-            entity = readEntityFrom(response);
             final JaxbService jaxbService = new Simple();
-            commandsDto = jaxbService.fromXml(CommandsDto.class, entity);
-            log.debug("commands:\n{}", entity);
+            commandsDto = jaxbService.fromXml(CommandsDto.class, rawValue);
+            log.debug("commands:\n{}", rawValue);
         } catch(Exception ex) {
-            log.warn("unable to unmarshal entity from {} to CommandsDto.class; was:\n{}", uri, entity);
+            log.warn("unable to unmarshal entity from {} to CommandsDto.class; was:\n{}", endpointUri, rawValue);
             throw new StatusException(SecondaryStatus.FAILED_TO_UNMARSHALL_RESPONSE, ex);
         }
         return commandsDto;
     }
 
-    private static String readEntityFrom(final JaxRsResponse response) {
-        try {
-            return response.readEntity(String.class);
-        } catch(Exception e) {
-            return null;
-        }
+    // package private in support of JUnit
+    boolean useRequestDebugLogging = false;
+
+    private static RestfulClient newClient(
+            final SecondaryConfig secondaryConfig,
+            final boolean useRequestDebugLogging) {
+
+        RestfulClientConfig clientConfig = new RestfulClientConfig();
+        clientConfig.setRestfulBase(secondaryConfig.getPrimaryBaseUrlRestful());
+        // setup basic-auth
+        clientConfig.setUseBasicAuth(true);
+        clientConfig.setRestfulAuthUser(secondaryConfig.getPrimaryUser());
+        clientConfig.setRestfulAuthPassword(secondaryConfig.getPrimaryPassword());
+        // setup request/response debug logging
+        clientConfig.setUseRequestDebugLogging(useRequestDebugLogging);
+
+        RestfulClient client = RestfulClient.ofConfig(clientConfig);
+
+        return client;
     }
 
-    @Inject
-    SecondaryConfig secondaryConfig;
+    // package private in support of JUnit
+    @Inject SecondaryConfig secondaryConfig;
 
 }
diff --git a/extensions/core/command-replay/secondary/src/main/java/org/apache/isis/extensions/commandreplay/secondary/fetch/_LegacyClient.java b/extensions/core/command-replay/secondary/src/main/java/org/apache/isis/extensions/commandreplay/secondary/fetch/_LegacyClient.java
deleted file mode 100644
index a4fcb0d2b4..0000000000
--- a/extensions/core/command-replay/secondary/src/main/java/org/apache/isis/extensions/commandreplay/secondary/fetch/_LegacyClient.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
- *  with the License.  You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing,
- *  software distributed under the License is distributed on an
- *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- *  KIND, either express or implied.  See the License for the
- *  specific language governing permissions and limitations
- *  under the License.
- */
-package org.apache.isis.extensions.commandreplay.secondary.fetch;
-
-import java.net.URI;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import lombok.experimental.UtilityClass;
-
-@Deprecated
-@UtilityClass
-class _LegacyClient {
-
-    public static interface JaxRsResponse {
-        int getStatus();
-        <T> T readEntity(final Class<T> entityType);
-    }
-
-    public static interface JaxRsClient {
-        @RequiredArgsConstructor
-        public enum ReprType {
-            ACTION_RESULT("action-result");
-            @Getter private final String suffix;
-        }
-        JaxRsResponse get(URI uri, Class<?> dtoClass, ReprType reprType, String username, String password);
-    }
-
-    public static class JaxRsClientDefault implements JaxRsClient {
-
-        @Override
-        public JaxRsResponse get(final URI uri, final Class<?> dtoClass, final ReprType reprType, final String username, final String password) {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-    }
-
-
-}
diff --git a/extensions/core/command-replay/secondary/src/test/java/org/apache/isis/extensions/commandreplay/secondary/fetch/CommandFetcher_Test.java b/extensions/core/command-replay/secondary/src/test/java/org/apache/isis/extensions/commandreplay/secondary/fetch/CommandFetcher_Test.java
index b4eec7230c..e681c9523e 100644
--- a/extensions/core/command-replay/secondary/src/test/java/org/apache/isis/extensions/commandreplay/secondary/fetch/CommandFetcher_Test.java
+++ b/extensions/core/command-replay/secondary/src/test/java/org/apache/isis/extensions/commandreplay/secondary/fetch/CommandFetcher_Test.java
@@ -18,34 +18,42 @@
  */
 package org.apache.isis.extensions.commandreplay.secondary.fetch;
 
-import java.net.URI;
-
-import javax.ws.rs.core.UriBuilder;
+import java.util.Optional;
 
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
 import org.apache.isis.applib.util.JaxbUtil;
+import org.apache.isis.core.metamodel._testing.MetaModelContext_forTesting;
+import org.apache.isis.extensions.commandreplay.secondary.StatusException;
+import org.apache.isis.extensions.commandreplay.secondary.config.SecondaryConfig;
 import org.apache.isis.schema.cmd.v2.CommandsDto;
 
 import lombok.val;
 
-
-public class CommandFetcher_Test {
+class CommandFetcher_Test {
 
     @Disabled // intended only for manual verification.
     @Test
-    public void testing_the_unmarshalling() {
-        val jaxRsClient = new _LegacyClient.JaxRsClientDefault();
-        final UriBuilder uriBuilder = UriBuilder.fromUri(
-                        String.format(
-                        "%s%s?batchSize=%d",
-                        "http://localhost:8080/restful/", CommandFetcher.URL_SUFFIX, 10)
-        );
-        URI uri = uriBuilder.build();
-        _LegacyClient.JaxRsResponse invoke = jaxRsClient.get(
-                uri, CommandsDto.class, _LegacyClient.JaxRsClient.ReprType.ACTION_RESULT, "sven", "pass");
-        CommandsDto entity = invoke.readEntity(CommandsDto.class);
+    void testing_the_fetcher() throws StatusException {
+
+        // given
+        val mmc = MetaModelContext_forTesting.buildDefault();
+
+        val config = mmc.getConfiguration().getExtensions().getCommandReplay();
+        config.getPrimaryAccess().setUser(Optional.of("sven"));
+        config.getPrimaryAccess().setPassword(Optional.of("pass"));
+        config.getPrimaryAccess().setBaseUrlRestful(Optional.of("http://localhost:8080/restful/"));
+        config.getPrimaryAccess().setBaseUrlWicket(Optional.of("http://localhost:8080/wicket/"));
+        config.setBatchSize(10);
+
+        val fetcher = new CommandFetcher();
+        fetcher.secondaryConfig = new SecondaryConfig(mmc.getConfiguration());
+        fetcher.useRequestDebugLogging = true;
+
+        // when
+        CommandsDto entity = fetcher.callPrimary(null);
+
         System.out.println(JaxbUtil.toXml(entity));
     }
 }
diff --git a/viewers/restfulobjects/client/src/main/java/org/apache/isis/viewer/restfulobjects/client/ResponseDigest.java b/viewers/restfulobjects/client/src/main/java/org/apache/isis/viewer/restfulobjects/client/ResponseDigest.java
index c2201a0966..d2e94fa5c7 100644
--- a/viewers/restfulobjects/client/src/main/java/org/apache/isis/viewer/restfulobjects/client/ResponseDigest.java
+++ b/viewers/restfulobjects/client/src/main/java/org/apache/isis/viewer/restfulobjects/client/ResponseDigest.java
@@ -96,35 +96,35 @@ class ResponseDigest<T> {
     /**
      * @return whether the REST endpoint replied with a success status code.
      */
-    public boolean isSuccess() {
+    boolean isSuccess() {
         return !isFailure();
     }
 
     /**
      * @return whether the REST endpoint replied with a failure status code.
      */
-    public boolean isFailure() {
+    boolean isFailure() {
         return failureCause!=null;
     }
 
     /**
      * @return (non-null), optionally the result if cardinality is exactly ONE
      */
-    public Optional<T> getEntity() {
+    Optional<T> getEntity() {
         return getEntities().getSingleton();
     }
 
     /**
      * @return (non-null), the entities replied by the REST endpoint supporting any cardinality ZERO, ONE or more.
      */
-    public Can<T> getEntities(){
+    Can<T> getEntities(){
         return entities;
     }
 
     /**
      * @return (nullable), the failure case (if any), when the REST endpoint replied with a failure status code
      */
-    public @Nullable Exception getFailureCause(){
+    @Nullable Exception getFailureCause(){
         return failureCause;
     }
 
diff --git a/viewers/restfulobjects/client/src/main/java/org/apache/isis/viewer/restfulobjects/client/RestfulClient.java b/viewers/restfulobjects/client/src/main/java/org/apache/isis/viewer/restfulobjects/client/RestfulClient.java
index f9e773685f..72c398850b 100644
--- a/viewers/restfulobjects/client/src/main/java/org/apache/isis/viewer/restfulobjects/client/RestfulClient.java
+++ b/viewers/restfulobjects/client/src/main/java/org/apache/isis/viewer/restfulobjects/client/RestfulClient.java
@@ -86,6 +86,30 @@ if(digest.isSuccess()) {
 }
  * </pre></blockquote>
  *
+ * Maven Setup:
+ * <blockquote><pre>{@code
+<dependency>
+    <groupId>org.apache.isis.core</groupId>
+    <artifactId>isis-core-applib</artifactId>
+    <version>2.0.0-M7-SNAPSHOT</version>
+</dependency>
+<dependency>
+    <groupId>org.glassfish.jersey.ext</groupId>
+    <artifactId>jersey-spring5</artifactId>
+    <version>2.29.1</version>
+</dependency>
+<dependency>
+    <groupId>org.glassfish</groupId>
+    <artifactId>javax.json</artifactId>
+    <version>1.1.4</version>
+</dependency>
+<dependency>
+    <groupId>org.eclipse.persistence</groupId>
+    <artifactId>org.eclipse.persistence.moxy</artifactId>
+    <version>2.7.5</version>
+</dependency>
+ * }</pre></blockquote>
+ *
  * @since 2.0 {@index}
  */
 @Log4j2