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

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


The following commit(s) were added to refs/heads/master by this push:
     new c17cd455eb WICKET-7056 try-catch attempts to operate on invalid HTTP 
session and… (#670)
c17cd455eb is described below

commit c17cd455ebb483dbb62629eeb93d2493af519c72
Author: DavesMan <[email protected]>
AuthorDate: Thu Oct 12 13:01:39 2023 +0200

    WICKET-7056 try-catch attempts to operate on invalid HTTP session and… 
(#670)
    
    * WICKET-7056 try-catch attempts to operate on invalid HTTP session and log 
them
    
    * WICKET-7056: Simplify the code by using lambdas
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    
    ---------
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    Co-authored-by: Martin Tzvetanov Grigorov <[email protected]>
    (cherry picked from commit 9a5b1cbec337edf1cf229e74cba0055236ff4df3)
---
 .../apache/wicket/session/HttpSessionStore.java    | 112 ++++++++++++++-------
 1 file changed, 74 insertions(+), 38 deletions(-)

diff --git 
a/wicket-core/src/main/java/org/apache/wicket/session/HttpSessionStore.java 
b/wicket-core/src/main/java/org/apache/wicket/session/HttpSessionStore.java
index 908484a933..059c0c461c 100644
--- a/wicket-core/src/main/java/org/apache/wicket/session/HttpSessionStore.java
+++ b/wicket-core/src/main/java/org/apache/wicket/session/HttpSessionStore.java
@@ -22,6 +22,7 @@ import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.Callable;
 import java.util.concurrent.CopyOnWriteArraySet;
 
 import jakarta.servlet.http.HttpServletRequest;
@@ -51,9 +52,9 @@ public class HttpSessionStore implements ISessionStore
 {
        private static final Logger log = 
LoggerFactory.getLogger(HttpSessionStore.class);
 
-       private final Set<UnboundListener> unboundListeners = new 
CopyOnWriteArraySet<UnboundListener>();
+       private final Set<UnboundListener> unboundListeners = new 
CopyOnWriteArraySet<>();
 
-       private final Set<BindListener> bindListeners = new 
CopyOnWriteArraySet<BindListener>();
+       private final Set<BindListener> bindListeners = new 
CopyOnWriteArraySet<>();
 
        /**
         * @param request The Wicket request
@@ -62,7 +63,7 @@ public class HttpSessionStore implements ISessionStore
        protected final HttpServletRequest getHttpServletRequest(final Request 
request)
        {
                Object containerRequest = request.getContainerRequest();
-               if (containerRequest == null || (containerRequest instanceof 
HttpServletRequest) == false)
+               if ((containerRequest instanceof HttpServletRequest) == false)
                {
                        throw new IllegalArgumentException("Request must be 
ServletWebRequest");
                }
@@ -103,11 +104,14 @@ public class HttpSessionStore implements ISessionStore
                        {
                                // register an unbinding listener for cleaning 
up
                                String applicationKey = 
Application.get().getName();
-                               
httpSession.setAttribute("Wicket:SessionUnbindingListener-" + applicationKey,
-                                       new 
SessionBindingListener(applicationKey, newSession));
-
-                               // register the session object itself
-                               setWicketSession(request, newSession);
+                               withSession(httpSession.getId(), () -> {
+                                       
httpSession.setAttribute("Wicket:SessionUnbindingListener-" + applicationKey,
+                                                       new 
SessionBindingListener(applicationKey, newSession));
+
+                                       // register the session object itself
+                                       setWicketSession(request, newSession);
+                                       return null;
+                               });
                        }
                }
        }
@@ -161,8 +165,10 @@ public class HttpSessionStore implements ISessionStore
                HttpSession httpSession = getHttpSession(request, false);
                if (httpSession != null)
                {
-                       // tell the app server the session is no longer valid
-                       httpSession.invalidate();
+                       withSession(httpSession.getId(), () -> {
+                               httpSession.invalidate();
+                               return null;
+                       });
                }
        }
 
@@ -253,7 +259,10 @@ public class HttpSessionStore implements ISessionStore
                HttpSession httpSession = getHttpSession(request, false);
                if (httpSession != null)
                {
-                       return 
(Serializable)httpSession.getAttribute(getSessionAttributePrefix(request) + 
name);
+                       return withSession(httpSession.getId(), () -> {
+                               return (Serializable)httpSession
+                                       
.getAttribute(getSessionAttributePrefix(request) + name);
+                       });
                }
                return null;
        }
@@ -261,21 +270,23 @@ public class HttpSessionStore implements ISessionStore
        @Override
        public final List<String> getAttributeNames(final Request request)
        {
-               List<String> list = new ArrayList<String>();
+               List<String> list = new ArrayList<>();
                HttpSession httpSession = getHttpSession(request, false);
                if (httpSession != null)
                {
-                       @SuppressWarnings("unchecked")
-                       final Enumeration<String> names = 
httpSession.getAttributeNames();
-                       final String prefix = 
getSessionAttributePrefix(request);
-                       while (names.hasMoreElements())
-                       {
-                               final String name = names.nextElement();
-                               if (name.startsWith(prefix))
+                       withSession(httpSession.getId(), () -> {
+                               final Enumeration<String> names = 
httpSession.getAttributeNames();
+                               final String prefix = 
getSessionAttributePrefix(request);
+                               while (names.hasMoreElements())
                                {
-                                       
list.add(name.substring(prefix.length()));
+                                       final String name = names.nextElement();
+                                       if (name.startsWith(prefix))
+                                       {
+                                               
list.add(name.substring(prefix.length()));
+                                       }
                                }
-                       }
+                               return null;
+                       });
                }
                return list;
        }
@@ -289,15 +300,18 @@ public class HttpSessionStore implements ISessionStore
                        String attributeName = 
getSessionAttributePrefix(request) + name;
 
                        IRequestLogger logger = 
Application.get().getRequestLogger();
-                       if (logger != null)
-                       {
-                               Object value = 
httpSession.getAttribute(attributeName);
-                               if (value != null)
+                       withSession(httpSession.getId(), () -> {
+                               if (logger != null)
                                {
-                                       logger.objectRemoved(value);
+                                       Object value = 
httpSession.getAttribute(attributeName);
+                                       if (value != null)
+                                       {
+                                               logger.objectRemoved(value);
+                                       }
                                }
-                       }
-                       httpSession.removeAttribute(attributeName);
+                               httpSession.removeAttribute(attributeName);
+                               return null;
+                       });
                }
        }
 
@@ -311,18 +325,21 @@ public class HttpSessionStore implements ISessionStore
                {
                        String attributeName = 
getSessionAttributePrefix(request) + name;
                        IRequestLogger logger = 
Application.get().getRequestLogger();
-                       if (logger != null)
-                       {
-                               if (httpSession.getAttribute(attributeName) == 
null)
-                               {
-                                       logger.objectCreated(value);
-                               }
-                               else
+                       withSession(httpSession.getId(), () -> {
+                               if (logger != null)
                                {
-                                       logger.objectUpdated(value);
+                                       if 
(httpSession.getAttribute(attributeName) == null)
+                                       {
+                                               logger.objectCreated(value);
+                                       }
+                                       else
+                                       {
+                                               logger.objectUpdated(value);
+                                       }
                                }
-                       }
-                       httpSession.setAttribute(attributeName, value);
+                               httpSession.setAttribute(attributeName, value);
+                               return null;
+                       });
                }
        }
 
@@ -446,4 +463,23 @@ public class HttpSessionStore implements ISessionStore
                        }
                }
        }
+
+       /**
+        * @param sessionId The id of the HTTP session that might happen to be 
invalidated 
+        *                  in the meantime
+        * @return prefix for session attributes
+        */
+       private <V> V withSession(String sessionId, Callable<V> callable) {
+               try
+               {
+                       return callable.call();
+               }
+               catch (IllegalStateException isx)
+               {
+                       log.debug("HTTP session {} is no more valid!", 
sessionId, isx);
+               } catch (Exception e) {
+                       throw new RuntimeException(e);
+               }
+               return null;
+       }
 }

Reply via email to