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/12/01 11:53:55 UTC

[isis] branch master updated: ISIS-3275: re-implement OutboxClient on top of RestClient (5)

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 72f653d655 ISIS-3275: re-implement OutboxClient on top of RestClient (5)
72f653d655 is described below

commit 72f653d655dcdd62b7582e29591bc9c1f204c71b
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Dec 1 12:53:47 2022 +0100

    ISIS-3275: re-implement OutboxClient on top of RestClient (5)
    
    - fix suppression header
---
 .../applib/restapi/OutboxRestApi.java              |  2 -
 .../restclient/api/OutboxClient.java               | 24 ++-----
 .../client/RestfulClientMediaType.java             | 74 ++++++++++++++--------
 3 files changed, 54 insertions(+), 46 deletions(-)

diff --git a/extensions/core/executionoutbox/applib/src/main/java/org/apache/causeway/extensions/executionoutbox/applib/restapi/OutboxRestApi.java b/extensions/core/executionoutbox/applib/src/main/java/org/apache/causeway/extensions/executionoutbox/applib/restapi/OutboxRestApi.java
index 2e5986471c..d3f0fa9048 100644
--- a/extensions/core/executionoutbox/applib/src/main/java/org/apache/causeway/extensions/executionoutbox/applib/restapi/OutboxRestApi.java
+++ b/extensions/core/executionoutbox/applib/src/main/java/org/apache/causeway/extensions/executionoutbox/applib/restapi/OutboxRestApi.java
@@ -62,8 +62,6 @@ public class OutboxRestApi  {
      * <p>
      *     The {@link ContentMappingServiceForOutboxEvents} will then serialize the resultant {@link OutboxEvents} view model into XML.
      * </p>
-     *
-     * @return
      */
     @Action(
             semantics = SemanticsOf.SAFE,
diff --git a/extensions/core/executionoutbox/restclient/src/main/java/org/apache/causeway/extensions/executionoutbox/restclient/api/OutboxClient.java b/extensions/core/executionoutbox/restclient/src/main/java/org/apache/causeway/extensions/executionoutbox/restclient/api/OutboxClient.java
index 53d8c926a6..27bc33f225 100644
--- a/extensions/core/executionoutbox/restclient/src/main/java/org/apache/causeway/extensions/executionoutbox/restclient/api/OutboxClient.java
+++ b/extensions/core/executionoutbox/restclient/src/main/java/org/apache/causeway/extensions/executionoutbox/restclient/api/OutboxClient.java
@@ -25,7 +25,6 @@ import java.util.List;
 
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.UriBuilder;
 
 import org.apache.causeway.applib.util.schema.InteractionsDtoUtils;
 import org.apache.causeway.commons.functional.Try;
@@ -89,9 +88,6 @@ public class OutboxClient {
         return this;
     }
 
-    private UriBuilder deleteUriBuilder;
-    private UriBuilder deleteManyUriBuilder;
-
     @Setter private String base;
     @Setter private String username;
     @Setter private String password;
@@ -102,9 +98,6 @@ public class OutboxClient {
      * Should be called once all properties have been injected.
      */
     public void init() {
-        this.deleteUriBuilder = UriBuilder.fromUri(base + "services/causeway.ext.executionOutbox.OutboxRestApi/actions/delete/invoke");
-        this.deleteManyUriBuilder = UriBuilder.fromUri(base + "services/causeway.ext.executionOutbox.OutboxRestApi/actions/deleteMany/invoke");
-
         restfulClientConfig.setRestfulBase(base);
         restfulClientConfig.setUseBasicAuth(true);
         restfulClientConfig.setRestfulAuthUser(username);
@@ -145,8 +138,8 @@ public class OutboxClient {
     }
 
     public void delete(final String interactionId, final int sequence) {
-        val entity = new DeleteMessage(interactionId, sequence);
-        invoke(entity, DELETE_URI);
+        invoke(DELETE_URI,
+                new DeleteMessage(interactionId, sequence));
     }
 
     public void deleteMany(final List<InteractionDto> interactionDtos) {
@@ -154,9 +147,8 @@ public class OutboxClient {
         interactionDtos.forEach(interactionDto -> {
             addTo(interactionsDto, interactionDto);
         });
-
-        val entity = new DeleteManyMessage(InteractionsDtoUtils.toXml(interactionsDto));
-        invoke(entity, DELETE_MANY_URI);
+        invoke(DELETE_MANY_URI,
+                new DeleteManyMessage(InteractionsDtoUtils.toXml(interactionsDto)));
     }
 
     // -- HELPER
@@ -185,25 +177,23 @@ public class OutboxClient {
                 : new PropertyEditDto();
     }
 
-    private void invoke(final Object entity, final String path) {
+    private void invoke(final String path, final Object dto) {
 
         ensureInitialized();
 
-        val json =  _Json.toString(entity);
-
         try(val client = RestfulClient.ofConfig(restfulClientConfig)) {
 
             var invocationBuilder = client.request(path);
 
             val invocation = invocationBuilder.buildPut(
-                    Entity.entity(json, MediaType.APPLICATION_JSON_TYPE));
+                    Entity.entity(_Json.toString(dto), MediaType.APPLICATION_JSON_TYPE));
 
             val response = invocation.invoke();
 
             val responseStatus = response.getStatus();
             if (responseStatus != 200) {
                 // if failed to log message via REST service, then fallback by logging to slf4j
-                log.warn(entity.toString());
+                log.warn(dto.toString());
             }
         }
 
diff --git a/viewers/restfulobjects/client/src/main/java/org/apache/causeway/viewer/restfulobjects/client/RestfulClientMediaType.java b/viewers/restfulobjects/client/src/main/java/org/apache/causeway/viewer/restfulobjects/client/RestfulClientMediaType.java
index 30f6eefd3c..19e9ca8c1f 100644
--- a/viewers/restfulobjects/client/src/main/java/org/apache/causeway/viewer/restfulobjects/client/RestfulClientMediaType.java
+++ b/viewers/restfulobjects/client/src/main/java/org/apache/causeway/viewer/restfulobjects/client/RestfulClientMediaType.java
@@ -20,51 +20,71 @@ package org.apache.causeway.viewer.restfulobjects.client;
 
 import java.util.EnumSet;
 import java.util.Map;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 import javax.ws.rs.core.MediaType;
 
+import org.springframework.lang.Nullable;
+
 import org.apache.causeway.applib.client.SuppressionType;
 import org.apache.causeway.commons.internal.base._NullSafe;
 import org.apache.causeway.commons.internal.base._Strings;
+import org.apache.causeway.commons.internal.collections._Maps;
+
+import lombok.RequiredArgsConstructor;
+import lombok.val;
 
+@RequiredArgsConstructor
 public enum RestfulClientMediaType {
-    RO_XML{
-        @Override
-        public MediaType mediaTypeFor(final Class<?> dtoClass, final EnumSet<SuppressionType> suppressionTypes) {
-            return new MediaType("application", "xml",
-                    Map.<String, String>of(
-                            "profile", "urn:org.restfulobjects:repr-types/action-result"
-                                    + toSuppressionLiteral(suppressionTypes),
-                            "x-ro-domain-type", dtoClass.getName()));
-        }
-    },
-    SIMPLE_JSON {
-        @Override
-        public MediaType mediaTypeFor(final Class<?> dtoClass, final EnumSet<SuppressionType> suppressionTypes) {
-            return new MediaType("application", "json",
-                    Map.<String, String>of(
-                            "profile", "urn:org.apache.causeway/v2"
-                                    + toSuppressionLiteral(suppressionTypes),
-                            "x-ro-domain-type", dtoClass.getName()));
-        }
-    }
+    RO_XML("application", "xml", "org.restfulobjects:repr-types/action-result"),
+    SIMPLE_JSON("application", "json", "org.apache.causeway/v2");
     ;
 
-    public final MediaType mediaTypeFor(final Class<?> dtoClass) {
+    private final String type;
+    private final String subType;
+    private final String urn;
+
+    public final MediaType mediaTypeFor() {
+        return mediaTypeFor(null, EnumSet.noneOf(SuppressionType.class));
+    }
+
+    public final MediaType mediaTypeFor(
+            final @Nullable Class<?> dtoClass) {
         return mediaTypeFor(dtoClass, EnumSet.noneOf(SuppressionType.class));
     }
 
-    public abstract MediaType mediaTypeFor(final Class<?> dtoClass, EnumSet<SuppressionType> suppressionTypes);
+    public final MediaType mediaTypeFor(
+            final @Nullable Class<?> dtoClass,
+            final @Nullable EnumSet<SuppressionType> suppressionTypes) {
+        return new MediaType(type, subType, headerMap(urn, dtoClass, suppressionTypes));
+    }
+
+    // -- HELPER
+
+    private static Map<String, String> headerMap(
+            final String urn,
+            final Class<?> dtoClass,
+            final EnumSet<SuppressionType> suppressionTypes) {
+        val headerMap = _Maps.<String, String>newHashMap();
+
+        headerMap.put("profile", "urn:" + urn);
+
+        toSuppressionLiteral(suppressionTypes)
+        .ifPresent(suppress->headerMap.put("suppress", suppress));
+
+        Optional.ofNullable(dtoClass)
+        .map(Class::getName)
+        .ifPresent(typeLiteral->headerMap.put("x-ro-domain-type", typeLiteral));
+
+        return headerMap;
+    }
 
-    private static String toSuppressionLiteral(final EnumSet<SuppressionType> suppressionTypes) {
+    private static Optional<String> toSuppressionLiteral(final EnumSet<SuppressionType> suppressionTypes) {
         final String suppressionSetLiteral = _NullSafe.stream(suppressionTypes)
                 .map(SuppressionType::name)
                 .collect(Collectors.joining(","));
-        if(_Strings.isNotEmpty(suppressionSetLiteral)) {
-            return ";suppress=" + suppressionSetLiteral;
-        }
-        return "";
+        return _Strings.nonEmpty(suppressionSetLiteral);
     }
 
 }