Class WebSocketMappings

java.lang.Object
org.eclipse.jetty.websocket.core.server.WebSocketMappings
All Implemented Interfaces:
EventListener, Dumpable, LifeCycle.Listener

public class WebSocketMappings extends Object implements Dumpable, LifeCycle.Listener
Mapping of pathSpec to a tupple of WebSocketCreator, FrameHandlerFactory and Configuration.Customizer.

When the upgrade(WebSocketNegotiator, Request, Response, Callback, Configuration.Customizer) method is called, a match for the pathSpec is looked for. If one is found then the creator is used to create a POJO for the WebSocket endpoint, the factory is used to wrap that POJO with a FrameHandler and the customizer is used to configure the resulting CoreSession.

  • Field Details

    • WEBSOCKET_MAPPING_ATTRIBUTE

      public static final String WEBSOCKET_MAPPING_ATTRIBUTE
  • Constructor Details

    • WebSocketMappings

      public WebSocketMappings()
    • WebSocketMappings

      public WebSocketMappings(WebSocketComponents components)
  • Method Details

    • getMappings

      public static WebSocketMappings getMappings(ContextHandler contextHandler)
    • ensureMappings

      public static WebSocketMappings ensureMappings(ContextHandler contextHandler)
    • parsePathSpec

      public static PathSpec parsePathSpec(String rawSpec)
      Parse a PathSpec string into a PathSpec instance.

      Recognized Path Spec syntaxes:

      /path/to or / or *.ext or servlet|{spec}
      Servlet Syntax
      ^{spec} or regex|{spec}
      Regex Syntax
      uri-template|{spec}
      URI Template (see JSR356 and RFC6570 level 1)
      Parameters:
      rawSpec - the raw path spec as String to parse.
      Returns:
      the PathSpec implementation for the rawSpec
    • getWebSocketComponents

      public WebSocketComponents getWebSocketComponents()
    • getHandshaker

      public Handshaker getHandshaker()
    • lifeCycleStopping

      public void lifeCycleStopping(LifeCycle event)
      Specified by:
      lifeCycleStopping in interface LifeCycle.Listener
    • clear

      public void clear()
    • dump

      public void dump(Appendable out, String indent) throws IOException
      Description copied from interface: Dumpable
      Dump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.
      Specified by:
      dump in interface Dumpable
      Parameters:
      out - The appendable to dump to
      indent - The indent to apply after any new lines.
      Throws:
      IOException - if unable to write to Appendable
    • getWebSocketNegotiator

      public WebSocketNegotiator getWebSocketNegotiator(PathSpec pathSpec)
    • getWebSocketCreator

      public WebSocketCreator getWebSocketCreator(PathSpec pathSpec)
    • addMapping

      public void addMapping(PathSpec pathSpec, WebSocketCreator creator, FrameHandlerFactory factory, Configuration.Customizer customizer) throws WebSocketException
      Manually add a WebSocket mapping.

      If mapping is added before this configuration is started, then it is persisted through stop/start of this configuration's lifecycle. Otherwise it will be removed when this configuration is stopped.

      Parameters:
      pathSpec - the pathspec to respond on
      creator - the websocket creator to activate on the provided mapping.
      factory - the factory to use to create a FrameHandler for the websocket.
      customizer - the customizer to use to customize the WebSocket session.
      Throws:
      WebSocketException
    • addMapping

      public void addMapping(PathSpec pathSpec, WebSocketNegotiator negotiator) throws WebSocketException
      Manually add a WebSocket mapping.

      If mapping is added before this configuration is started, then it is persisted through stop/start of this configuration's lifecycle. Otherwise it will be removed when this configuration is stopped.

      Parameters:
      pathSpec - the pathspec to respond on
      negotiator - the WebSocketNegotiator to use to create a FrameHandler for the websocket.
      Throws:
      WebSocketException
    • removeMapping

      public boolean removeMapping(PathSpec pathSpec)
    • getMatchedNegotiator

      public WebSocketNegotiator getMatchedNegotiator(Request request, BiConsumer<Request,PathSpec> consumer)

      Returns the mapped WebSocketNegotiator if there is a match of given request against a registered mapping, otherwise returns null if there is no match.

      If there is a match, the given consumer is invoked with the PathSpec that matched so that, for example, it can be stored as a request attribute for later usage. This is important in case of UriTemplatePathSpec, where applications may want to extract the values of the template groups.

      Parameters:
      request - the request to match
      consumer - the consumer to invoke in case of match
      Returns:
      the WebSocketNegotiator if there is a match, or null if there is no match
    • upgrade

      public boolean upgrade(Request request, Response response, Callback callback, Configuration.Customizer defaultCustomizer) throws WebSocketException

      Attempts to find a WebSocket mapping and upgrade a request to WebSocket.

      Returns true if the WebSocket upgrade is successful and a successful response is generated and the callback eventually completed, or if the WebSocket upgrade failed and a failure response is generated and the callback eventually completed. Returns false if a response is not generated and the caller is responsible for generating a response and completing the callback.

      Parameters:
      request - the request
      response - the response
      callback - the callback
      defaultCustomizer - the customizer
      Returns:
      true if the WebSocket upgrade was accepted
      Throws:
      WebSocketException - there is an error during the upgrade
    • upgrade

      public boolean upgrade(WebSocketNegotiator negotiator, Request request, Response response, Callback callback, Configuration.Customizer defaultCustomizer) throws WebSocketException

      Attempts to find a WebSocket mapping and upgrade a request to WebSocket.

      Returns true if the WebSocket upgrade is successful and a successful response is generated and the callback eventually completed, or if the WebSocket upgrade failed and a failure response is generated and the callback eventually completed. Returns false if a response is not generated and the caller is responsible for generating a response and completing the callback.

      Parameters:
      negotiator - the negotiator
      request - the request
      response - the response
      callback - the callback
      defaultCustomizer - the customizer
      Returns:
      true if the WebSocket upgrade was accepted
      Throws:
      WebSocketException - there is an error during the upgrade