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 2021/09/04 08:59:06 UTC

[isis] branch master updated: ISIS-2768: internal: simplify usability, make WebAppContextPath handling more convenient

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 ecf3150  ISIS-2768: internal: simplify usability, make WebAppContextPath handling more convenient
ecf3150 is described below

commit ecf3150a4a64558166408ce9bfbfb408e17e0922
Author: andi-huber <ah...@apache.org>
AuthorDate: Sat Sep 4 10:58:56 2021 +0200

    ISIS-2768: internal: simplify usability, make WebAppContextPath handling
    more convenient
---
 .../core/config/viewer/web/WebAppContextPath.java   | 21 ++++++++++++++++++++-
 .../restfulobjects/rendering/IResourceContext.java  |  2 +-
 .../viewer/context/ResourceContext.java             |  8 +++-----
 .../viewer/resources/HomePageReprRenderer.java      |  4 ----
 .../viewer/resources/ResourceAbstract.java          | 13 ++++++++++---
 5 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/core/config/src/main/java/org/apache/isis/core/config/viewer/web/WebAppContextPath.java b/core/config/src/main/java/org/apache/isis/core/config/viewer/web/WebAppContextPath.java
index 0381251..54f3357 100644
--- a/core/config/src/main/java/org/apache/isis/core/config/viewer/web/WebAppContextPath.java
+++ b/core/config/src/main/java/org/apache/isis/core/config/viewer/web/WebAppContextPath.java
@@ -22,12 +22,12 @@ import java.io.Serializable;
 import java.util.Optional;
 import java.util.regex.Pattern;
 
-import org.springframework.lang.Nullable;
 import javax.annotation.Priority;
 import javax.inject.Named;
 import javax.inject.Singleton;
 
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.lang.Nullable;
 import org.springframework.stereotype.Service;
 
 import org.apache.isis.applib.annotation.PriorityPrecedence;
@@ -55,6 +55,12 @@ public class WebAppContextPath implements Serializable {
     @Getter
     private String contextPath = "";
 
+//    public Optional<String> getContextPath() {
+//        return hasContextPath()
+//                ? Optional.of(contextPath)
+//                : Optional.empty();
+//    }
+
     /**
      * @param contextPath - any form allowed: leading or trailing '/',
      * no matter what, gets normalized
@@ -84,6 +90,19 @@ public class WebAppContextPath implements Serializable {
         return getContextPath() + _Strings.prefix(localPath, "/");
     }
 
+    public String appendContextPath(final @Nullable String path) {
+        if(path==null) {
+            return getContextPath();
+        }
+        if(!hasContextPath()) {
+            return path;
+        }
+        return _Strings.suffix(path, "/") +
+                (getContextPath().startsWith("/")
+                    ? getContextPath().substring(1)
+                    : path);
+    }
+
     /**
      * @param urlOrLocalPath - when detected to be a localPath prepends the context-path if any,
      * identity operator otherwise
diff --git a/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/IResourceContext.java b/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/IResourceContext.java
index f4d0483..95f3241 100644
--- a/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/IResourceContext.java
+++ b/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/IResourceContext.java
@@ -51,7 +51,7 @@ public interface IResourceContext {
 
     /**
      * Prepends with the application's base URI.
-     * @param url - relative resource, must include context-path if any
+     * @param url - relative resource, must not include context-path if any
      */
     String applicationUrlFor(final String url);
 
diff --git a/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/context/ResourceContext.java b/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/context/ResourceContext.java
index 4a56b8b..8db48cd 100644
--- a/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/context/ResourceContext.java
+++ b/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/context/ResourceContext.java
@@ -55,6 +55,7 @@ import org.apache.isis.viewer.restfulobjects.viewer.resources.ResourceDescriptor
 import org.apache.isis.viewer.restfulobjects.viewer.resources.serialization.SerializationStrategy;
 
 import lombok.Getter;
+import lombok.NonNull;
 import lombok.Setter;
 import lombok.val;
 
@@ -271,15 +272,12 @@ implements IResourceContext {
     }
 
     @Override
-    public String restfulUrlFor(final String url) {
+    public String restfulUrlFor(final @NonNull String url) {
         return restfulAbsoluteBase + url;
     }
 
     @Override
-    public String applicationUrlFor(final String url) {
-        if(_Strings.isEmpty(url)) {
-            return "";
-        }
+    public String applicationUrlFor(final @NonNull String url) {
         return applicationAbsoluteBase + (
                 url.startsWith("/")
                 ? url.substring(1)
diff --git a/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/HomePageReprRenderer.java b/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/HomePageReprRenderer.java
index ccf130e..38d4ba0 100644
--- a/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/HomePageReprRenderer.java
+++ b/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/HomePageReprRenderer.java
@@ -24,7 +24,6 @@ import javax.inject.Inject;
 
 import org.apache.isis.applib.services.iactnlayer.InteractionContext;
 import org.apache.isis.commons.collections.Can;
-import org.apache.isis.core.config.viewer.web.WebAppContextPath;
 import org.apache.isis.core.metamodel.spec.ManagedObject;
 import org.apache.isis.core.metamodel.spec.ObjectSpecification;
 import org.apache.isis.viewer.common.model.branding.BrandingUiModelProvider;
@@ -46,7 +45,6 @@ extends ReprRendererAbstract<Void> {
 
     // injection points not directly managed by Spring, instead resolved via constructor
     @Inject BrandingUiModelProvider brandingUiModelProvider;
-    @Inject WebAppContextPath webAppContextPath;
 
     HomePageReprRenderer(
             final IResourceContext resourceContext,
@@ -176,7 +174,6 @@ extends ReprRendererAbstract<Void> {
         brandingUiModelProvider
         .getSignInBranding()
         .getLogoHref()
-        .map(webAppContextPath::prependContextPathIfLocal)
         .ifPresent(href->
             getLinks()
                 .arrayAdd(LinkBuilder.newBuilder(
@@ -189,7 +186,6 @@ extends ReprRendererAbstract<Void> {
         brandingUiModelProvider
         .getHeaderBranding()
         .getLogoHref()
-        .map(webAppContextPath::prependContextPathIfLocal)
         .ifPresent(href->
             getLinks()
                 .arrayAdd(LinkBuilder.newBuilder(
diff --git a/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/ResourceAbstract.java b/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/ResourceAbstract.java
index 5771d0f..f28ab1a 100644
--- a/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/ResourceAbstract.java
+++ b/viewers/restfulobjects/viewer/src/main/java/org/apache/isis/viewer/restfulobjects/viewer/resources/ResourceAbstract.java
@@ -39,6 +39,7 @@ import org.apache.isis.commons.internal.base._Refs;
 import org.apache.isis.commons.internal.base._Strings;
 import org.apache.isis.commons.internal.codec._UrlDecoderUtil;
 import org.apache.isis.core.config.IsisConfiguration;
+import org.apache.isis.core.config.viewer.web.WebAppContextPath;
 import org.apache.isis.core.metamodel.consent.InteractionInitiatedBy;
 import org.apache.isis.core.metamodel.context.MetaModelContext;
 import org.apache.isis.core.metamodel.spec.ManagedObject;
@@ -68,6 +69,8 @@ public abstract class ResourceAbstract {
     @Context SecurityContext securityContext;
     @Context Providers providers;
 
+    @Inject WebAppContextPath webAppContextPath;
+
     @Inject
     protected ResourceAbstract(
             final MetaModelContext metaModelContext,
@@ -110,21 +113,25 @@ public abstract class ResourceAbstract {
             throw RestfulObjectsApplicationException.create(HttpStatusCode.UNAUTHORIZED);
         }
 
-        // eg. http://localhost:8080/restful/
+        // eg. http://localhost:8080/ctx/restful/
         final String restfulAbsoluteBase = isisConfiguration.getViewer().getRestfulobjects().getBaseUri()
                                     .orElseGet(()->uriInfo.getBaseUri().toString());
 
-        // eg. /restful/
+        // eg. /ctx/restful/
         val restfulRelativeBase = uriInfo.getBaseUri().getRawPath();
 
         // eg. http://localhost:8080/
-        val applicationAbsoluteBase =
+        val serverAbsoluteBase =
                 _Strings
                 .suffix(_Refs
                         .stringRef(restfulAbsoluteBase)
                         .cutAtLastIndexOfAndDrop(restfulRelativeBase),
                 "/");
 
+        // eg. http://localhost:8080/ctx/
+        val applicationAbsoluteBase = _Strings
+                .suffix(webAppContextPath.appendContextPath(serverAbsoluteBase), "/");
+
         return resourceContext(
                 resourceDescriptor,
                 applicationAbsoluteBase,