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

reiern70 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 5f737e7ff8 [WICKET-7162] avoid a NPE when a JavaxUpgradeHttpRequest is 
created in a situation when there is no session (e.g. some stateless page) and 
extract context from requestedUI
5f737e7ff8 is described below

commit 5f737e7ff8af676d22426579b0e3a9950aa0ca4d
Author: reiern70 <[email protected]>
AuthorDate: Mon Aug 11 12:36:05 2025 -0500

    [WICKET-7162] avoid a NPE when a JavaxUpgradeHttpRequest is created in a 
situation when there is no session (e.g. some stateless page) and extract 
context from requestedUI
---
 .../protocol/ws/javax/JavaxUpgradeHttpRequest.java | 14 +++++++---
 .../ws/javax/WicketServerEndpointConfig.java       | 30 ++++++++++++++--------
 2 files changed, 29 insertions(+), 15 deletions(-)

diff --git 
a/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/JavaxUpgradeHttpRequest.java
 
b/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/JavaxUpgradeHttpRequest.java
index 04bb59d14c..9b7c029ab6 100644
--- 
a/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/JavaxUpgradeHttpRequest.java
+++ 
b/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/JavaxUpgradeHttpRequest.java
@@ -49,6 +49,8 @@ import jakarta.websocket.EndpointConfig;
 import jakarta.websocket.Session;
 
 import org.apache.wicket.util.string.StringValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * An artificial HttpServletRequest with data collected from the
@@ -56,6 +58,11 @@ import org.apache.wicket.util.string.StringValue;
  */
 public class JavaxUpgradeHttpRequest implements HttpServletRequest
 {
+       public static final String SESSION = "session";
+       public static final String HEADERS = "headers";
+       public static final String CONTEXT_PATH = "contextPath";
+       private static final Logger log = 
LoggerFactory.getLogger(JavaxUpgradeHttpRequest.class);
+
        private final HttpSession httpSession;
        private final String queryString;
        private final Principal userPrincipal;
@@ -75,14 +82,13 @@ public class JavaxUpgradeHttpRequest implements 
HttpServletRequest
                        userProperties = endpointConfig.getUserProperties();
                }
 
-               this.httpSession = (HttpSession) userProperties.get("session");
-               this.headers = (Map<String, List<String>>) 
userProperties.get("headers");
+               this.httpSession = (HttpSession) userProperties.get(SESSION);
+               this.headers = (Map<String, List<String>>) 
userProperties.get(HEADERS);
                this.queryString = session.getQueryString();
                this.userPrincipal = session.getUserPrincipal();
                Object requestURI = session.getRequestURI();
                this.requestUri = requestURI != null ? requestURI.toString() : 
"";
-               this.contextPath = 
httpSession.getServletContext().getContextPath();
-
+               this.contextPath = (String)userProperties.get(CONTEXT_PATH);
                this.parametersMap = new HashMap<>();
 
                Map<String, List<String>> parameters = 
session.getRequestParameterMap();
diff --git 
a/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/WicketServerEndpointConfig.java
 
b/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/WicketServerEndpointConfig.java
index 632e9e427d..8e7f3299d8 100644
--- 
a/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/WicketServerEndpointConfig.java
+++ 
b/wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/WicketServerEndpointConfig.java
@@ -21,6 +21,7 @@ import java.security.Principal;
 import java.util.List;
 import java.util.Map;
 
+import jakarta.servlet.http.HttpSession;
 import jakarta.websocket.Decoder;
 import jakarta.websocket.Encoder;
 import jakarta.websocket.Extension;
@@ -127,18 +128,33 @@ public class WicketServerEndpointConfig implements 
ServerEndpointConfig
                        // do not store null keys/values because Tomcat 8 uses 
ConcurrentMap for UserProperties
 
                        Map<String, Object> userProperties = 
sec.getUserProperties();
+
+                       URI requestURI = request.getRequestURI();
+                       LOG.trace("requestURI: {}", requestURI);
+                       if (requestURI != null)
+                       {
+                               userProperties.put("requestURI", requestURI);
+                               String path = requestURI.getPath();
+                               if (path != null && path.indexOf("/wicket") > 
0) {
+                                       String contextPath = path.substring(0, 
path.indexOf("/wicket"));
+                                       
userProperties.put(JavaxUpgradeHttpRequest.CONTEXT_PATH, contextPath);
+                               }
+                       }
+
                        Object httpSession = request.getHttpSession();
                        LOG.trace("httpSession: {}", httpSession);
-                       if (httpSession != null)
+                       userProperties.put(JavaxUpgradeHttpRequest.SESSION, 
httpSession);
+                       if (httpSession instanceof HttpSession)
                        {
-                               userProperties.put("session", httpSession);
+                               
userProperties.put(JavaxUpgradeHttpRequest.CONTEXT_PATH,((HttpSession)httpSession).getServletContext().getContextPath());
                        }
 
+
                        Map<String, List<String>> headers = 
request.getHeaders();
                        LOG.trace("headers: {}", headers);
                        if (headers != null)
                        {
-                               userProperties.put("headers", headers);
+                               
userProperties.put(JavaxUpgradeHttpRequest.HEADERS, headers);
                        }
 
 
@@ -157,14 +173,6 @@ public class WicketServerEndpointConfig implements 
ServerEndpointConfig
                                userProperties.put("queryString", queryString);
                        }
 
-
-                       URI requestURI = request.getRequestURI();
-                       LOG.trace("requestURI: {}", requestURI);
-                       if (requestURI != null)
-                       {
-                               userProperties.put("requestURI", requestURI);
-                       }
-
                        Principal userPrincipal = request.getUserPrincipal();
                        LOG.trace("userPrincipal: {}", userPrincipal);
                        if (userPrincipal != null)

Reply via email to