Skip to content

How to remove a websocket mapping #13579

@gabrielandrade2

Description

@gabrielandrade2

Jetty Version
Jetty 12.1.0

Jetty Environment
ee10

Java Version
Eclipse Adoptium OpenJDK 64-Bit Server VM 17.0.14+7;

Question
I have recently upgraded my project from Jetty 9.4 to 12.
One of the features of my system spawns/removes a websocket enpoint whenever the user enables/disables it.

Previously (in Jetty 9.4) the websocket mapping was being removed with the following code snipped without any issues:

ServletContextHandler contextHandler = getContextHandler(webSocket);
if (contextHandler != null) {
    ServletContext servletContext = contextHandler.getServletContext();
    ServerContainer container = (ServerContainer) servletContext.getAttribute(
            ServerContainer.class.getName());
    if (container == null) {
        return;
    }

    if (container instanceof ContainerLifeCycle) {
        ContainerLifeCycle containerLifeCycle = (ContainerLifeCycle) container;
        NativeWebSocketConfiguration configuration = containerLifeCycle.getBean(
                NativeWebSocketConfiguration.class);
        if (configuration != null) {
            configuration.removeMapping("uri-template|" + webSocket.getPath());
        }
    }
}

Upon the migration to Jetty 12, I had to make a few adjustments to have it running, because NativeWebSocketConfiguration is no longer available. Thus, my refactored code looks like the following:

ServletContextHandler contextHandler = getContextHandler(webSocket);
if (contextHandler != null) {
    boolean removed = false;
    WebSocketMappings webSocketMappings = contextHandler.getBean(
            WebSocketMappings.class);
    if (webSocketMappings != null) {
        PathSpec pathSpec = new UriTemplatePathSpec(webSocket.getPath());
        webSocketMappings.removeMapping(pathSpec);
    }
}

This, however, does not work as I'd expect. I can confirm that the mapping is removed (by dumping the running server config), but the rest of the websocket mappings that I have on the same context do not work anymore.

I am using Jakarta rather than Jetty's implementation for the websocket container, and upon exploring a little of the code, I don't see why manually removing the mapping would break other websocket endpoints.

Is there something I am doing wrong? Is there a better way to remove such endpoints at runtime?
(Preferably, I'd like to continue using Jakarta classes, to keep the same standard throughout the project)

Metadata

Metadata

Labels

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions