You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2021/06/20 14:57:49 UTC

[isis] branch ISIS-2758 updated (9098a27 -> 1e7948a)

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

danhaywood pushed a change to branch ISIS-2758
in repository https://gitbox.apache.org/repos/asf/isis.git.


    from 9098a27  ISIS-2758: adds javax.ws.rs-api to applib
     new 868d9eb  ISIS-2758: reworks ImpersonatedUserHolderUsingHttpSession, so not @RequestScoped
     new 1e7948a  ISIS-2758: also create RequestContextListener as a bean

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../isis/core/webapp/IsisModuleCoreWebapp.java     | 13 +++++++++
 .../ImpersonatedUserHolderUsingHttpSession.java    | 31 +++++++++++-----------
 2 files changed, 28 insertions(+), 16 deletions(-)

[isis] 01/02: ISIS-2758: reworks ImpersonatedUserHolderUsingHttpSession, so not @RequestScoped

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch ISIS-2758
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 868d9eb21ae234916398308bdeed995c3d1f6904
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Sun Jun 20 15:53:28 2021 +0100

    ISIS-2758: reworks ImpersonatedUserHolderUsingHttpSession, so not @RequestScoped
    
    as this prevents it from being used in initial fixture scripts.
    
    domain-app-demo/persist-all/simple-object-builder: Error creating bean with name 'scopedTarget.isis.webapp.ImpersonatedUserHolderUsingHttpSession': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of  [...]
---
 .../ImpersonatedUserHolderUsingHttpSession.java    | 30 +++++++++++-----------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/core/webapp/src/main/java/org/apache/isis/core/webapp/impersonation/ImpersonatedUserHolderUsingHttpSession.java b/core/webapp/src/main/java/org/apache/isis/core/webapp/impersonation/ImpersonatedUserHolderUsingHttpSession.java
index a70e3fd..75e0964 100644
--- a/core/webapp/src/main/java/org/apache/isis/core/webapp/impersonation/ImpersonatedUserHolderUsingHttpSession.java
+++ b/core/webapp/src/main/java/org/apache/isis/core/webapp/impersonation/ImpersonatedUserHolderUsingHttpSession.java
@@ -20,15 +20,15 @@ package org.apache.isis.core.webapp.impersonation;
 
 import java.util.Optional;
 
-import javax.inject.Inject;
 import javax.inject.Named;
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 
-import org.apache.isis.applib.annotation.PriorityPrecedence;
 import org.springframework.stereotype.Component;
 import org.springframework.web.context.annotation.RequestScope;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 
+import org.apache.isis.applib.annotation.PriorityPrecedence;
 import org.apache.isis.applib.services.user.ImpersonatedUserHolder;
 import org.apache.isis.applib.services.user.UserMemento;
 
@@ -39,36 +39,28 @@ import org.apache.isis.applib.services.user.UserMemento;
  * @since 2.0 {@index}
  */
 @Component
-@RequestScope
 @Named("isis.webapp.ImpersonatedUserHolderUsingHttpSession")
 @javax.annotation.Priority(PriorityPrecedence.MIDPOINT)
 public class ImpersonatedUserHolderUsingHttpSession implements ImpersonatedUserHolder {
 
-    private final Optional<HttpSession> httpSession;
-
     private static final String HTTP_SESSION_KEY_IMPERSONATED_USER =
             ImpersonatedUserHolderUsingHttpSession.class.getName() + "#userMemento";
 
-    @Inject
-    public ImpersonatedUserHolderUsingHttpSession(final HttpServletRequest httpServletRequest) {
-        this.httpSession = Optional.ofNullable(httpServletRequest.getSession(false));
-    }
-
     @Override
     public boolean supportsImpersonation() {
-        return httpSession.isPresent();
+        return httpSession().isPresent();
     }
 
     @Override
     public void setUserMemento(final UserMemento userMemento) {
-        httpSession
+        httpSession()
         .ifPresent(session->
             session.setAttribute(HTTP_SESSION_KEY_IMPERSONATED_USER, userMemento));
     }
 
     @Override
     public Optional<UserMemento> getUserMemento() {
-        return httpSession
+        return httpSession()
             .map(session->session.getAttribute(HTTP_SESSION_KEY_IMPERSONATED_USER))
             .filter(UserMemento.class::isInstance)
             .map(UserMemento.class::cast);
@@ -76,9 +68,17 @@ public class ImpersonatedUserHolderUsingHttpSession implements ImpersonatedUserH
 
     @Override
     public void clearUserMemento() {
-        httpSession.ifPresent(session->
+        httpSession()
+        .ifPresent(session->
             session.removeAttribute(HTTP_SESSION_KEY_IMPERSONATED_USER));
     }
 
+    private static Optional<HttpSession> httpSession() {
+        return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
+                .filter(ServletRequestAttributes.class::isInstance)
+                .map(ServletRequestAttributes.class::cast)
+                .map(ServletRequestAttributes::getRequest)
+                .map(x -> x.getSession(false));
+    }
 
 }

[isis] 02/02: ISIS-2758: also create RequestContextListener as a bean

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch ISIS-2758
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 1e7948ac550dbc14fe73ae5303fc1dd2f3f405ad
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Sun Jun 20 15:57:19 2021 +0100

    ISIS-2758: also create RequestContextListener as a bean
---
 .../org/apache/isis/core/webapp/IsisModuleCoreWebapp.java   | 13 +++++++++++++
 .../ImpersonatedUserHolderUsingHttpSession.java             |  1 -
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/core/webapp/src/main/java/org/apache/isis/core/webapp/IsisModuleCoreWebapp.java b/core/webapp/src/main/java/org/apache/isis/core/webapp/IsisModuleCoreWebapp.java
index 8f2607f..86a4707 100644
--- a/core/webapp/src/main/java/org/apache/isis/core/webapp/IsisModuleCoreWebapp.java
+++ b/core/webapp/src/main/java/org/apache/isis/core/webapp/IsisModuleCoreWebapp.java
@@ -24,6 +24,7 @@ import org.springframework.context.annotation.Import;
 import org.springframework.context.annotation.Scope;
 import org.springframework.context.annotation.ScopedProxyMode;
 import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.request.RequestContextListener;
 
 import org.apache.isis.core.interaction.session.MessageBroker;
 import org.apache.isis.core.runtime.IsisModuleCoreRuntime;
@@ -63,4 +64,16 @@ public class IsisModuleCoreWebapp {
         return new MessageBroker();
     }
 
+    /**
+     * for implementation of {@link ImpersonatedUserHolderUsingHttpSession}, using {@link org.springframework.web.context.request.RequestContextHolder}.
+     *
+     * @see org.springframework.web.context.request.RequestContextHolder
+     * @see <a href="https://stackoverflow.com/a/44830684/56880">https://stackoverflow.com/a/44830684/56880</a>
+     * @see <a href="https://stackoverflow.com/a/61431621/56880">https://stackoverflow.com/a/61431621/56880</a>
+     */
+    @Bean
+    public RequestContextListener requestContextListener() {
+        return new RequestContextListener();
+    }
+
 }
diff --git a/core/webapp/src/main/java/org/apache/isis/core/webapp/impersonation/ImpersonatedUserHolderUsingHttpSession.java b/core/webapp/src/main/java/org/apache/isis/core/webapp/impersonation/ImpersonatedUserHolderUsingHttpSession.java
index 75e0964..7f7db8e 100644
--- a/core/webapp/src/main/java/org/apache/isis/core/webapp/impersonation/ImpersonatedUserHolderUsingHttpSession.java
+++ b/core/webapp/src/main/java/org/apache/isis/core/webapp/impersonation/ImpersonatedUserHolderUsingHttpSession.java
@@ -24,7 +24,6 @@ import javax.inject.Named;
 import javax.servlet.http.HttpSession;
 
 import org.springframework.stereotype.Component;
-import org.springframework.web.context.annotation.RequestScope;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;