Issue #4747 - Fix WS path params to work within a context path

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-04-07 12:18:29 +10:00
parent 54ee3f3939
commit 625dfd1a4d
11 changed files with 51 additions and 17 deletions

View File

@ -64,4 +64,10 @@ public class JavaxClientUpgradeRequest extends ClientUpgradeRequest implements U
{
return getURI();
}
@Override
public String getPathInContext()
{
throw new UnsupportedOperationException();
}
}

View File

@ -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();
}

View File

@ -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<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);

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -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();
}

View File

@ -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();
}
}

View File

@ -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");

View File

@ -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);

View File

@ -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