You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by re...@apache.org on 2025/08/11 17:36:18 UTC

(wicket) 01/01: [WICKET-7162] avoid a NPE when a JavaxUpgradeHttpRequest is created in a situation when there is no session (e.g. some stateless page).

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

reiern70 pushed a commit to branch reiern70/WICKET-7162
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit 86b36f1e84a47213ff66366357d4035b1c7e5075
Author: reiern70 <re...@gmail.com>
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).
---
 .../protocol/ws/javax/JavaxUpgradeHttpRequest.java  | 21 +++++++++++++++++----
 .../ws/javax/WicketServerEndpointConfig.java        |  6 ++++--
 2 files changed, 21 insertions(+), 6 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..2215c00817 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,10 @@ import org.apache.wicket.util.string.StringValue;
  */
 public class JavaxUpgradeHttpRequest implements HttpServletRequest
 {
+	public static final String SESSION = "session";
+	public static final String HEADERS = "headers";
+	private static final Logger log = LoggerFactory.getLogger(JavaxUpgradeHttpRequest.class);
+
 	private final HttpSession httpSession;
 	private final String queryString;
 	private final Principal userPrincipal;
@@ -75,14 +81,21 @@ 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();
-
+		if (httpSession == null)
+		{
+			this.contextPath = "/";
+			log.debug("No HTTP session is present. Web sockets are used in a stateless page");
+		}
+		else
+		{
+			this.contextPath = httpSession.getServletContext().getContextPath();
+		}
 		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..f3d40645a9 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
@@ -131,14 +131,16 @@ public class WicketServerEndpointConfig implements ServerEndpointConfig
 			LOG.trace("httpSession: {}", httpSession);
 			if (httpSession != null)
 			{
-				userProperties.put("session", httpSession);
+				userProperties.put(JavaxUpgradeHttpRequest.SESSION, httpSession);
+			} else {
+				sec.getPath();
 			}
 
 			Map<String, List<String>> headers = request.getHeaders();
 			LOG.trace("headers: {}", headers);
 			if (headers != null)
 			{
-				userProperties.put("headers", headers);
+				userProperties.put(JavaxUpgradeHttpRequest.HEADERS, headers);
 			}