Skip to content

Commit 5f737e7

Browse files
committed
[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
1 parent 4294934 commit 5f737e7

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/JavaxUpgradeHttpRequest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,20 @@
4949
import jakarta.websocket.Session;
5050

5151
import org.apache.wicket.util.string.StringValue;
52+
import org.slf4j.Logger;
53+
import org.slf4j.LoggerFactory;
5254

5355
/**
5456
* An artificial HttpServletRequest with data collected from the
5557
* available WebSocket Session and from the HandshakeRequest
5658
*/
5759
public class JavaxUpgradeHttpRequest implements HttpServletRequest
5860
{
61+
public static final String SESSION = "session";
62+
public static final String HEADERS = "headers";
63+
public static final String CONTEXT_PATH = "contextPath";
64+
private static final Logger log = LoggerFactory.getLogger(JavaxUpgradeHttpRequest.class);
65+
5966
private final HttpSession httpSession;
6067
private final String queryString;
6168
private final Principal userPrincipal;
@@ -75,14 +82,13 @@ public JavaxUpgradeHttpRequest(final Session session, EndpointConfig endpointCon
7582
userProperties = endpointConfig.getUserProperties();
7683
}
7784

78-
this.httpSession = (HttpSession) userProperties.get("session");
79-
this.headers = (Map<String, List<String>>) userProperties.get("headers");
85+
this.httpSession = (HttpSession) userProperties.get(SESSION);
86+
this.headers = (Map<String, List<String>>) userProperties.get(HEADERS);
8087
this.queryString = session.getQueryString();
8188
this.userPrincipal = session.getUserPrincipal();
8289
Object requestURI = session.getRequestURI();
8390
this.requestUri = requestURI != null ? requestURI.toString() : "";
84-
this.contextPath = httpSession.getServletContext().getContextPath();
85-
91+
this.contextPath = (String)userProperties.get(CONTEXT_PATH);
8692
this.parametersMap = new HashMap<>();
8793

8894
Map<String, List<String>> parameters = session.getRequestParameterMap();

wicket-native-websocket/wicket-native-websocket-javax/src/main/java/org/apache/wicket/protocol/ws/javax/WicketServerEndpointConfig.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import jakarta.servlet.http.HttpSession;
2425
import jakarta.websocket.Decoder;
2526
import jakarta.websocket.Encoder;
2627
import jakarta.websocket.Extension;
@@ -127,18 +128,33 @@ public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request,
127128
// do not store null keys/values because Tomcat 8 uses ConcurrentMap for UserProperties
128129

129130
Map<String, Object> userProperties = sec.getUserProperties();
131+
132+
URI requestURI = request.getRequestURI();
133+
LOG.trace("requestURI: {}", requestURI);
134+
if (requestURI != null)
135+
{
136+
userProperties.put("requestURI", requestURI);
137+
String path = requestURI.getPath();
138+
if (path != null && path.indexOf("/wicket") > 0) {
139+
String contextPath = path.substring(0, path.indexOf("/wicket"));
140+
userProperties.put(JavaxUpgradeHttpRequest.CONTEXT_PATH, contextPath);
141+
}
142+
}
143+
130144
Object httpSession = request.getHttpSession();
131145
LOG.trace("httpSession: {}", httpSession);
132-
if (httpSession != null)
146+
userProperties.put(JavaxUpgradeHttpRequest.SESSION, httpSession);
147+
if (httpSession instanceof HttpSession)
133148
{
134-
userProperties.put("session", httpSession);
149+
userProperties.put(JavaxUpgradeHttpRequest.CONTEXT_PATH,((HttpSession)httpSession).getServletContext().getContextPath());
135150
}
136151

152+
137153
Map<String, List<String>> headers = request.getHeaders();
138154
LOG.trace("headers: {}", headers);
139155
if (headers != null)
140156
{
141-
userProperties.put("headers", headers);
157+
userProperties.put(JavaxUpgradeHttpRequest.HEADERS, headers);
142158
}
143159

144160

@@ -157,14 +173,6 @@ public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request,
157173
userProperties.put("queryString", queryString);
158174
}
159175

160-
161-
URI requestURI = request.getRequestURI();
162-
LOG.trace("requestURI: {}", requestURI);
163-
if (requestURI != null)
164-
{
165-
userProperties.put("requestURI", requestURI);
166-
}
167-
168176
Principal userPrincipal = request.getUserPrincipal();
169177
LOG.trace("userPrincipal: {}", userPrincipal);
170178
if (userPrincipal != null)

0 commit comments

Comments
 (0)