From 625dfd1a4d0b82b6174452b3c669e723388eebe5 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Tue, 7 Apr 2020 12:18:29 +1000 Subject: [PATCH] Issue #4747 - Fix WS path params to work within a context path Signed-off-by: Lachlan Roberts --- .../client/internal/JavaxClientUpgradeRequest.java | 6 ++++++ .../JavaxWebSocketClientFrameHandlerFactory.java | 2 +- .../common/JavaxWebSocketFrameHandlerFactory.java | 7 +++---- .../jetty/websocket/javax/common/UpgradeRequest.java | 11 ++++++++--- .../javax/common/UpgradeRequestAdapter.java | 12 ++++++++++-- .../websocket/javax/common/AbstractSessionTest.java | 2 +- .../javax/common/DummyFrameHandlerFactory.java | 2 +- .../server/internal/JavaxServerUpgradeRequest.java | 8 +++++++- .../jetty/websocket/javax/tests/PathParamTest.java | 6 +++--- ...WebSocketFrameHandlerOnMessageTextStreamTest.java | 3 ++- .../websocket/servlet/ServletUpgradeRequest.java | 9 +++++++++ 11 files changed, 51 insertions(+), 17 deletions(-) diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxClientUpgradeRequest.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxClientUpgradeRequest.java index b2fd4d5ae52..d2c8856db66 100644 --- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxClientUpgradeRequest.java +++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxClientUpgradeRequest.java @@ -64,4 +64,10 @@ public class JavaxClientUpgradeRequest extends ClientUpgradeRequest implements U { return getURI(); } + + @Override + public String getPathInContext() + { + throw new UnsupportedOperationException(); + } } diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientFrameHandlerFactory.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientFrameHandlerFactory.java index 39f3221b877..5a1b5252e0e 100644 --- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientFrameHandlerFactory.java +++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JavaxWebSocketClientFrameHandlerFactory.java @@ -40,7 +40,7 @@ public class JavaxWebSocketClientFrameHandlerFactory extends JavaxWebSocketFrame } @Override - public EndpointConfig newDefaultEndpointConfig(Class endpointClass, String path) + public EndpointConfig newDefaultEndpointConfig(Class endpointClass) { return new BasicClientEndpointConfig(); } diff --git a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketFrameHandlerFactory.java b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketFrameHandlerFactory.java index 5eeea251808..eb51e98d4fa 100644 --- a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketFrameHandlerFactory.java +++ b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketFrameHandlerFactory.java @@ -144,7 +144,7 @@ public abstract class JavaxWebSocketFrameHandlerFactory public abstract JavaxWebSocketFrameHandlerMetadata getMetadata(Class endpointClass, EndpointConfig endpointConfig); - public abstract EndpointConfig newDefaultEndpointConfig(Class endpointClass, String path); + public abstract EndpointConfig newDefaultEndpointConfig(Class endpointClass); public JavaxWebSocketFrameHandler newJavaxWebSocketFrameHandler(Object endpointInstance, UpgradeRequest upgradeRequest) { @@ -160,8 +160,7 @@ public abstract class JavaxWebSocketFrameHandlerFactory else { endpoint = endpointInstance; - String path = (upgradeRequest.getRequestURI() == null) ? null : upgradeRequest.getRequestURI().getPath(); - config = newDefaultEndpointConfig(endpoint.getClass(), path); + config = newDefaultEndpointConfig(endpoint.getClass()); } JavaxWebSocketFrameHandlerMetadata metadata = getMetadata(endpoint.getClass(), config); @@ -180,7 +179,7 @@ public abstract class JavaxWebSocketFrameHandlerFactory if (templatePathSpec != null) { String[] namedVariables = templatePathSpec.getVariables(); - Map pathParams = templatePathSpec.getPathParams(upgradeRequest.getRequestURI().getRawPath()); + Map pathParams = templatePathSpec.getPathParams(upgradeRequest.getPathInContext()); // Handle parameterized @PathParam entries openHandle = bindTemplateVariables(openHandle, namedVariables, pathParams); diff --git a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/UpgradeRequest.java b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/UpgradeRequest.java index 90de5c9c492..5764910685a 100644 --- a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/UpgradeRequest.java +++ b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/UpgradeRequest.java @@ -31,9 +31,14 @@ public interface UpgradeRequest Principal getUserPrincipal(); /** - * For obtaining {@link javax.websocket.server.PathParam} values from Request URI path - * - * @return the request URI + * @return the full URI of this request. */ URI getRequestURI(); + + /** + * For obtaining {@link javax.websocket.server.PathParam} values from the Request context path. + * + * @return the path in Context. + */ + String getPathInContext(); } diff --git a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/UpgradeRequestAdapter.java b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/UpgradeRequestAdapter.java index 4ab2345d669..42e2f94e7fa 100644 --- a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/UpgradeRequestAdapter.java +++ b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/UpgradeRequestAdapter.java @@ -24,16 +24,18 @@ import java.security.Principal; public class UpgradeRequestAdapter implements UpgradeRequest { private final URI requestURI; + private final String pathInContext; public UpgradeRequestAdapter() { /* anonymous, no requestURI, upgrade request */ - this(null); + this(null, null); } - public UpgradeRequestAdapter(URI uri) + public UpgradeRequestAdapter(URI uri, String pathInContext) { this.requestURI = uri; + this.pathInContext = pathInContext; } @Override @@ -47,4 +49,10 @@ public class UpgradeRequestAdapter implements UpgradeRequest { return requestURI; } + + @Override + public String getPathInContext() + { + return pathInContext; + } } diff --git a/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/AbstractSessionTest.java b/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/AbstractSessionTest.java index e425420c99b..c96927c7dc6 100644 --- a/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/AbstractSessionTest.java +++ b/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/AbstractSessionTest.java @@ -41,7 +41,7 @@ public abstract class AbstractSessionTest JavaxWebSocketFrameHandler frameHandler = container.newFrameHandler(websocketPojo, upgradeRequest); CoreSession coreSession = new CoreSession.Empty(); session = new JavaxWebSocketSession(container, coreSession, frameHandler, container.getFrameHandlerFactory() - .newDefaultEndpointConfig(websocketPojo.getClass(), null)); + .newDefaultEndpointConfig(websocketPojo.getClass())); } @AfterAll diff --git a/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/DummyFrameHandlerFactory.java b/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/DummyFrameHandlerFactory.java index d7468d6a2d0..12d00d0c67d 100644 --- a/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/DummyFrameHandlerFactory.java +++ b/jetty-websocket/websocket-javax-common/src/test/java/org/eclipse/jetty/websocket/javax/common/DummyFrameHandlerFactory.java @@ -33,7 +33,7 @@ public class DummyFrameHandlerFactory extends JavaxWebSocketFrameHandlerFactory } @Override - public EndpointConfig newDefaultEndpointConfig(Class endpointClass, String path) + public EndpointConfig newDefaultEndpointConfig(Class endpointClass) { return ClientEndpointConfig.Builder.create().build(); } diff --git a/jetty-websocket/websocket-javax-server/src/main/java/org/eclipse/jetty/websocket/javax/server/internal/JavaxServerUpgradeRequest.java b/jetty-websocket/websocket-javax-server/src/main/java/org/eclipse/jetty/websocket/javax/server/internal/JavaxServerUpgradeRequest.java index 9b4d4d55939..69624723ec8 100644 --- a/jetty-websocket/websocket-javax-server/src/main/java/org/eclipse/jetty/websocket/javax/server/internal/JavaxServerUpgradeRequest.java +++ b/jetty-websocket/websocket-javax-server/src/main/java/org/eclipse/jetty/websocket/javax/server/internal/JavaxServerUpgradeRequest.java @@ -42,6 +42,12 @@ public class JavaxServerUpgradeRequest implements UpgradeRequest @Override public URI getRequestURI() { - return this.servletRequest.getRequestURI(); + return servletRequest.getRequestURI(); + } + + @Override + public String getPathInContext() + { + return servletRequest.getPathInContext(); } } diff --git a/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/PathParamTest.java b/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/PathParamTest.java index 2b3e28eeff8..b67e44a462a 100644 --- a/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/PathParamTest.java +++ b/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/PathParamTest.java @@ -53,7 +53,7 @@ public class PathParamTest _server.addConnector(_connector); _context = new ServletContextHandler(ServletContextHandler.SESSIONS); - _context.setContextPath("/"); + _context.setContextPath("/context"); _server.setHandler(_context); JavaxWebSocketServletContainerInitializer.configure(_context, (context, container) -> @@ -68,7 +68,7 @@ public class PathParamTest _server.stop(); } - @ServerEndpoint("/pathparam/echo/{name}") + @ServerEndpoint("/pathParam/echo/{name}") public static class EchoParamSocket { private Session session; @@ -92,7 +92,7 @@ public class PathParamTest WebSocketContainer container = ContainerProvider.getWebSocketContainer(); EventSocket clientEndpoint = new EventSocket(); - URI serverUri = URI.create("ws://localhost:" + _connector.getLocalPort() + "/pathparam/echo/myParam"); + URI serverUri = URI.create("ws://localhost:" + _connector.getLocalPort() + "/context/pathParam/echo/myParam"); Session session = container.connectToServer(clientEndpoint, serverUri); session.getBasicRemote().sendText("echo"); diff --git a/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/server/JavaxWebSocketFrameHandlerOnMessageTextStreamTest.java b/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/server/JavaxWebSocketFrameHandlerOnMessageTextStreamTest.java index 78117206672..3b1b6b5abe4 100644 --- a/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/server/JavaxWebSocketFrameHandlerOnMessageTextStreamTest.java +++ b/jetty-websocket/websocket-javax-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/server/JavaxWebSocketFrameHandlerOnMessageTextStreamTest.java @@ -46,7 +46,8 @@ public class JavaxWebSocketFrameHandlerOnMessageTextStreamTest extends AbstractJ @SuppressWarnings("Duplicates") private T performOnMessageInvocation(T socket, Consumer func) throws Exception { - UpgradeRequest request = new UpgradeRequestAdapter(URI.create("http://localhost:8080/msg/foo")); + URI uri = URI.create("http://localhost:8080/msg/foo"); + UpgradeRequest request = new UpgradeRequestAdapter(uri, uri.getPath()); // Establish endpoint function JavaxWebSocketFrameHandler frameHandler = container.newFrameHandler(socket, request); diff --git a/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/ServletUpgradeRequest.java b/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/ServletUpgradeRequest.java index ef81155a00b..f327a424a95 100644 --- a/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/ServletUpgradeRequest.java +++ b/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/ServletUpgradeRequest.java @@ -38,6 +38,7 @@ import javax.servlet.http.HttpSession; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.websocket.core.ExtensionConfig; import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.server.Negotiation; @@ -314,6 +315,14 @@ public class ServletUpgradeRequest return requestURI; } + /** + * @return the path within the context, combination of the ServletPath with the PathInfo. + */ + public String getPathInContext() + { + return URIUtil.addPaths(request.getServletPath(), request.getPathInfo()); + } + /** * @param name Attribute name * @return Attribute value or null