Class WebSocketUpgradeHandler

java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.ContainerLifeCycle
org.eclipse.jetty.server.Handler.Abstract
org.eclipse.jetty.server.Handler.AbstractContainer
org.eclipse.jetty.server.Handler.Wrapper
org.eclipse.jetty.websocket.server.WebSocketUpgradeHandler
All Implemented Interfaces:
org.eclipse.jetty.server.Handler, org.eclipse.jetty.server.Handler.Container, org.eclipse.jetty.server.Handler.Singleton, org.eclipse.jetty.server.Request.Handler, org.eclipse.jetty.util.component.Container, org.eclipse.jetty.util.component.Destroyable, org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.component.Dumpable.DumpableContainer, org.eclipse.jetty.util.component.LifeCycle, org.eclipse.jetty.util.thread.Invocable

public class WebSocketUpgradeHandler extends org.eclipse.jetty.server.Handler.Wrapper

A Handler that may perform the upgrade from HTTP to WebSocket.

The upgrade is performed only if the request matches all the requisites necessary for the upgrade (which vary upon the HTTP protocol version), otherwise the request handling is forwarded to the Handler child of this Handler.

WebSocketUpgradeHandler may be a descendant of a ContextHandler, typically as a direct child, but possibly also further down the Handler's tree, to enable WebSocket upgrades for that ContextHandler only.

WebSocketUpgradeHandler may be a descendant of the Server, typically as a direct child, but possibly also further down the Handler's tree, to enable WebSocket upgrades for possibly multiple ContextHandlers.

Typical usage:


 Server server = ...;

 ContextHandler context = new ContextHandler("/app");

 // Create the WebSocketUpgradeHandler.
 WebSocketUpgradeHandler wsHandler = WebSocketUpgradeHandler.from(server, context, container ->
 {
     // Map upgrade requests to "/app/ws" to an echo WebSocket endpoint.
     container.addMapping("/ws", (upgradeRequest, upgradeResponse, callback) -> new EchoEndPoint());
 });

 // Link WebSocketUpgradeHandler as a child of ContextHandler.
 context.setHandler(wsHandler);

 server.setHandler(context);
 server.start();
 

A WebSocketUpgradeHandler is associated with a ServerWebSocketContainer that is exported as a request context attribute and can be retrieved in this way:


 public boolean process(Request request)
 {
     // Retrieve the WebSocket container from the context attributes.
     ServerWebSocketContainer container = (ServerWebSocketContainer)request.getContext().getAttribute(WebSocketContainer.class.getName());
 }