Skip to content

Commit

Permalink
Merge pull request #11753 from jetty/jetty-12.0.x-11748-PathMappings-…
Browse files Browse the repository at this point in the history
…MiddleGlob

Issue #11748 - fix in PathMappings for websocket pathParam matching
  • Loading branch information
lachlan-roberts committed May 7, 2024
2 parents 9d21483 + 932c03b commit 40abe26
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,12 @@ public E put(PathSpec pathSpec, E resource)
_orderIsSignificant = true;
}
break;
case MIDDLE_GLOB:
if (!(pathSpec instanceof ServletPathSpec))
{
_orderIsSignificant = true;
}
break;
case DEFAULT:
if (pathSpec instanceof ServletPathSpec)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ public void startContainer() throws Exception
_context = new ServletContextHandler(ServletContextHandler.SESSIONS);
_context.setContextPath("/context");
_server.setHandler(_context);
}

private void start(Class<?> endpointClass) throws Exception
{
JakartaWebSocketServletContainerInitializer.configure(_context, (context, container) ->
container.addEndpoint(EchoParamSocket.class));

container.addEndpoint(endpointClass));
_server.start();
}

Expand Down Expand Up @@ -105,6 +107,24 @@ public void onMessage(String message, @PathParam("name") String name)
}
}

@ServerEndpoint("/pathParam/{middleParam}/echo")
public static class MiddleParamSocket
{
private Session session;

@OnOpen
public void onOpen(Session session)
{
this.session = session;
}

@OnMessage
public void onMessage(String message, @PathParam("middleParam") String name)
{
session.getAsyncRemote().sendText(message + "-" + name);
}
}

public static Stream<Arguments> pathParamEndpoints()
{
return Stream.of(
Expand Down Expand Up @@ -132,6 +152,7 @@ public static Stream<Arguments> pathParamEndpoints()
@MethodSource("pathParamEndpoints")
public void testPathParamSignatures(Class<?> endpointClass, String id) throws Exception
{
start(EchoParamSocket.class);
JakartaWebSocketServerContainer serverContainer = JakartaWebSocketServerContainer.getContainer(_context.getServletContext());
assertNotNull(serverContainer);
serverContainer.addEndpoint(endpointClass);
Expand All @@ -150,6 +171,7 @@ public void testPathParamSignatures(Class<?> endpointClass, String id) throws Ex
@Test
public void testBasicPathParamSocket() throws Exception
{
start(EchoParamSocket.class);
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
EventSocket clientEndpoint = new EventSocket();

Expand All @@ -162,4 +184,21 @@ public void testBasicPathParamSocket() throws Exception
session.close();
clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS);
}

@Test
public void testMiddlePathParamSocket() throws Exception
{
start(MiddleParamSocket.class);
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
EventSocket clientEndpoint = new EventSocket();

URI serverUri = URI.create("ws://localhost:" + _connector.getLocalPort() + "/context/pathParam/myParam/echo");
Session session = container.connectToServer(clientEndpoint, serverUri);
session.getBasicRemote().sendText("echo");

String resp = clientEndpoint.textMessages.poll(1, TimeUnit.SECONDS);
assertThat("Response echo", resp, is("echo-myParam"));
session.close();
clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS);
}
}

0 comments on commit 40abe26

Please sign in to comment.