Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-dev] Programmatic WebSocket upgrade on Jetty 10

Rossen,

It does look like we need to expose some more API from websocket-core to support this use case without using internal classes. I have opened a new issue on the Jetty project github for this. See https://github.com/eclipse/jetty.project/issues/5866

Cheers,
Lachlan

On Fri, Jan 8, 2021 at 8:09 PM Rossen Stoyanchev <rstoyanchev@xxxxxxxxxx> wrote:
Hi Lachlan, 

Thanks for your reply.

The upgrade needs to be performed within a web framework which has its own Servlet, mappings, and infrastructure so it's not feasible to extend JettyWebSocketServlet. In addition, WebSocket upgrades are supported on multiple servers from the same place in the code which necessitates a programmatic API. Last but not least, the framework can also run on an abstraction layer above the Servlet API, with support for non-Servlet containers, which makes it very challenging to devise a solution based on use of a Servlet or Filter.

Note that this doesn't need to be anything very extensive. Just a basic entry point that a framework can use that's guaranteed to not change. For what it's worth here are comparable SPI facades exposed in Tomcat [1]. Undertow [2], and WebSphere/OpenLiberty [3].

Best regards,
Rossen



From: jetty-dev-bounces@xxxxxxxxxxx <jetty-dev-bounces@xxxxxxxxxxx> on behalf of Lachlan Roberts <lachlan@xxxxxxxxxxx>
Sent: Friday, January 8, 2021 6:29 AM
To: Jetty @ Eclipse developer discussion list <jetty-dev@xxxxxxxxxxx>
Subject: Re: [jetty-dev] Programmatic WebSocket upgrade on Jetty 10
 
Rossen,

Is there any reason you can't extend the JettyWebSocketServlet instead of just HttpServlet? This way you could use only the Jetty WebSocket API and avoid these websocket-core and internal classes you would need to use otherwise. You can use the JettyWebSocketCreator to do any logic to select which WebSocket Endpoint you will use, and you can map the creator to "/*" so it matches everything.

public static class CustomUpgradeServlet extends JettyWebSocketServlet
{
    @Override
    protected void configure(JettyWebSocketServletFactory factory)
    {
        factory.addMapping("/*", (req, resp) ->
        {
            if (condition1)
                return new MyEchoSocket1();
            if (condition2)
                return new MyEchoSocket2();
            if (condition3)
                return new MyEchoSocket3();
        });
    }
}

Cheers,
Lachlan


On Fri, Jan 8, 2021 at 9:24 AM Rossen Stoyanchev <rstoyanchev@xxxxxxxxxx> wrote:
Hi,

I need to support WebSocket upgrades on Jetty 10 from within a web framework. Like what JettyWebSocketServlet or WebSocketUpgradeFilter do internally but not actually using them nor relying on their mapping mechanism.

My initial attempt at a minimal custom Servlet to do this:

@SuppressWarnings("serial")
public class MyServlet extends HttpServlet {

    private final Handshaker handshaker = new HandshakerSelector();

    private WebSocketComponents components;

    private JettyServerFrameHandlerFactory frameHandlerFactory;


    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.components = WebSocketServerComponents.getWebSocketComponents(getServletContext());
        this.frameHandlerFactory = JettyServerFrameHandlerFactory.getFactory(getServletContext());
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Configuration.Customizer customizer = new Configuration.ConfigurationCustomizer();
        WebSocketCreator creator = (req1, resp1) -> new MyEchoSocket();
        CreatorNegotiator negotiator = new CreatorNegotiator(creator, this.frameHandlerFactory, customizer);
        this.handshaker.upgradeRequest(negotiator, req, resp, this.components, null);
    }
}
 
However, several types are from "internal" packages. Is there a better to do this? It would be very useful to have some basic API for programmatic WebSocket upgrades from a framework or application Servlet.

Thanks,
Rossen
_______________________________________________
jetty-dev mailing list
jetty-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-dev
_______________________________________________
jetty-dev mailing list
jetty-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-dev

Back to the top