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
  • From: Rossen Stoyanchev <rstoyanchev@xxxxxxxxxx>
  • Date: Fri, 8 Jan 2021 09:09:26 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; 1; spf=pass; dmarc=pass action=none; dkim=pass; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed;; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uB21LaIrzSIdaPtVGm2bcIzVUaEfA/KQK+1CC/TEyn0=; b=bkNT0rgueNCuQWcO3k5DEqoJek2hMZX3BL9rOuof1wtlneru8g84GtkcF/Oy7K1/W170Tq6LH685NbrFeFlucNQQrgi8njWPbcKRLpJN2SmCkX3qFGG6agPN8BFqI7Q/HB/HgyRlLkLKXCs8pxE68yPJGuadMxXIt7Q6sGerLs/8uUDIvfirbMjGz6urEd01onjsSymEbxFPNQgwmg8M0gtsQZuWO4103NIH3bTdCIeIl8Q8YfgUz7zr/PJvJbMrztWjTd665RoSkkOTqdKqDnWw9xVjK17eej6lCEOqVVjg+u5U5H5oR+2KlFfbBQZrYabCIUYLWM0NepJ4qgbRFQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901;; cv=none; b=ieV5SELJbE+FoHeMsydn8u7nRnSp5igxThOFB0gMEfQr3t1IhsHnrIEbk/50CvjRNlyP+6UzCAhekV/tIDcdilvvptFFDEQ2YZAcLAxphIJagvbc4kUSxfY8iZCCCYBve9Ef85EH8Q/S+arSdMEdpvVOAqUZdh4vN6H8+21RxwQmYh8vfhFYHn7hIENmZMEewvXtQeCbZUsm/tKMsHXnFR92g1apAaIWxTDBCvjyCeH8ucLvvn+AusA4BL+ocrlqWN5ZchyoTuTJt6vByyiSQKEWYi8rpyGNYi/cyLZeSsjmyHQCjgSkCXJVvrXbaGdUvjx4eDkjv6/hvLUI5sim5Q==
  • Delivered-to: jetty-dev@xxxxxxxxxxx
  • List-archive: <>
  • List-help: <>
  • List-subscribe: <>, <>
  • List-unsubscribe: <>, <>
  • Thread-index: AQHW5UPQGT46lHN4z0a479iXmND026odRMeAgAAhtL8=
  • Thread-topic: [jetty-dev] Programmatic WebSocket upgrade on Jetty 10

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,

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

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
    protected void configure(JettyWebSocketServletFactory factory)
        factory.addMapping("/*", (req, resp) ->
            if (condition1)
                return new MyEchoSocket1();
            if (condition2)
                return new MyEchoSocket2();
            if (condition3)
                return new MyEchoSocket3();


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

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:

public class MyServlet extends HttpServlet {

    private final Handshaker handshaker = new HandshakerSelector();

    private WebSocketComponents components;

    private JettyServerFrameHandlerFactory frameHandlerFactory;

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

    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.

jetty-dev mailing list
To unsubscribe from this list, visit

Back to the top