Class JakartaWebSocketUpgrader

java.lang.Object
io.micronaut.servlet.websocket.JakartaWebSocketUpgrader
All Implemented Interfaces:
ServletWebSocketUpgrader

@Internal @Singleton @Requires(classes={jakarta.websocket.server.ServerContainer.class,io.micronaut.websocket.annotation.ServerWebSocket.class}) @Requires(property="micronaut.servlet.websocket.enabled",notEquals="false",defaultValue="true") public final class JakartaWebSocketUpgrader extends Object implements ServletWebSocketUpgrader
Switches a matched upgrade request over to the servlet container's Jakarta WebSocket implementation.

No endpoint is ever registered with the container, so the container's own upgrade filter never matches and can never bypass the Micronaut filter chain. Instead a ServerEndpointConfig is built for the connection at hand and handed to ServerContainer.upgradeHttpToWebSocket(Object, Object, ServerEndpointConfig, Map), which Jetty, Tomcat and Undertow all implement as of Jakarta WebSocket 2.1.

Since:
6.2.0
Author:
graemerocher
  • Constructor Details

    • JakartaWebSocketUpgrader

      public JakartaWebSocketUpgrader(io.micronaut.context.BeanContext beanContext, ServletWebSocketSupport support, ServletWebSocketConfiguration configuration, io.micronaut.http.server.HttpServerConfiguration serverConfiguration, io.micronaut.http.server.util.HttpHostResolver httpHostResolver)
      Default constructor.
      Parameters:
      beanContext - The bean context
      support - The shared WebSocket services
      configuration - The WebSocket configuration
      serverConfiguration - The server configuration, used for the CORS policy
      httpHostResolver - Resolves the host of the handshake, so a same-origin socket is recognised
  • Method Details

    • upgrade

      public void upgrade(ServletExchange<?,?> exchange, io.micronaut.http.HttpRequest<?> request, io.micronaut.web.router.UriRouteMatch<?,?> routeMatch, io.micronaut.http.HttpResponse<?> handshakeResponse) throws Exception
      Description copied from interface: ServletWebSocketUpgrader
      Switch the given exchange over to the WebSocket protocol.

      Implementations are invoked on the container thread that is dispatching the request and before any asynchronous processing has been started, since servlet containers require the upgrade to happen while the original request is still being dispatched.

      Specified by:
      upgrade in interface ServletWebSocketUpgrader
      Parameters:
      exchange - The exchange being upgraded
      request - The originating HTTP request
      routeMatch - The matched WebSocket route
      handshakeResponse - The response produced by the Micronaut filter chain, any headers of which should be carried over to the handshake response
      Throws:
      Exception - if the upgrade cannot be performed