Skip to content

Commit

Permalink
Issue #4747 - Fix WS path params to work within a context path
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Apr 7, 2020
1 parent 54ee3f3 commit 625dfd1
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ public URI getRequestURI()
{
return getURI();
}

@Override
public String getPathInContext()
{
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public JavaxWebSocketClientFrameHandlerFactory(JavaxWebSocketContainer container
}

@Override
public EndpointConfig newDefaultEndpointConfig(Class<?> endpointClass, String path)
public EndpointConfig newDefaultEndpointConfig(Class<?> endpointClass)
{
return new BasicClientEndpointConfig();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public JavaxWebSocketFrameHandlerFactory(JavaxWebSocketContainer container, Invo

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)
{
Expand All @@ -160,8 +160,7 @@ public JavaxWebSocketFrameHandler newJavaxWebSocketFrameHandler(Object endpointI
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);
Expand All @@ -180,7 +179,7 @@ public JavaxWebSocketFrameHandler newJavaxWebSocketFrameHandler(Object endpointI
if (templatePathSpec != null)
{
String[] namedVariables = templatePathSpec.getVariables();
Map<String, String> pathParams = templatePathSpec.getPathParams(upgradeRequest.getRequestURI().getRawPath());
Map<String, String> pathParams = templatePathSpec.getPathParams(upgradeRequest.getPathInContext());

// Handle parameterized @PathParam entries
openHandle = bindTemplateVariables(openHandle, namedVariables, pathParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
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
Expand All @@ -47,4 +49,10 @@ public URI getRequestURI()
{
return requestURI;
}

@Override
public String getPathInContext()
{
return pathInContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void initSession() throws Exception
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public DummyFrameHandlerFactory(JavaxWebSocketContainer container)
}

@Override
public EndpointConfig newDefaultEndpointConfig(Class<?> endpointClass, String path)
public EndpointConfig newDefaultEndpointConfig(Class<?> endpointClass)
{
return ClientEndpointConfig.Builder.create().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public Principal getUserPrincipal()
@Override
public URI getRequestURI()
{
return this.servletRequest.getRequestURI();
return servletRequest.getRequestURI();
}

@Override
public String getPathInContext()
{
return servletRequest.getPathInContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void startContainer() throws Exception
_server.addConnector(_connector);

_context = new ServletContextHandler(ServletContextHandler.SESSIONS);
_context.setContextPath("/");
_context.setContextPath("/context");
_server.setHandler(_context);

JavaxWebSocketServletContainerInitializer.configure(_context, (context, container) ->
Expand All @@ -68,7 +68,7 @@ public void stopContainer() throws Exception
_server.stop();
}

@ServerEndpoint("/pathparam/echo/{name}")
@ServerEndpoint("/pathParam/echo/{name}")
public static class EchoParamSocket
{
private Session session;
Expand All @@ -92,7 +92,7 @@ public void testBasicPathParamSocket() throws Exception
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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public class JavaxWebSocketFrameHandlerOnMessageTextStreamTest extends AbstractJ
@SuppressWarnings("Duplicates")
private <T extends WSEventTracker> T performOnMessageInvocation(T socket, Consumer<JavaxWebSocketFrameHandler> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

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;
Expand Down Expand Up @@ -314,6 +315,14 @@ public URI getRequestURI()
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
Expand Down

0 comments on commit 625dfd1

Please sign in to comment.