Class BaseWebSocketFilter

java.lang.Object
org.glassfish.grizzly.filterchain.BaseFilter
org.glassfish.grizzly.websockets.BaseWebSocketFilter
All Implemented Interfaces:
org.glassfish.grizzly.filterchain.Filter
Direct Known Subclasses:
WebSocketClientFilter, WebSocketFilter

public abstract class BaseWebSocketFilter extends org.glassfish.grizzly.filterchain.BaseFilter
WebSocket Filter implementation, which supposed to be placed into a FilterChain right after HTTP Filter: HttpServerFilter, HttpClientFilter; depending whether it's server or client side. The BaseWebSocketFilter handles websocket connection, handshake phases and, when receives a websocket frame - redirects it to appropriate connection (WebSocketApplication, WebSocket) for processing.
Author:
Alexey Stashok
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new BaseWebSocketFilter with a default idle connection timeout of 15 minutes;
    BaseWebSocketFilter(long wsTimeoutInSeconds)
    Constructs a new BaseWebSocketFilter with a default idle connection timeout of 15 minutes;
  • Method Summary

    Modifier and Type
    Method
    Description
    org.glassfish.grizzly.filterchain.NextAction
    handleClose(org.glassfish.grizzly.filterchain.FilterChainContext ctx)
    Method handles Grizzly Connection close phase.
    protected abstract org.glassfish.grizzly.filterchain.NextAction
    handleHandshake(org.glassfish.grizzly.filterchain.FilterChainContext ctx, org.glassfish.grizzly.http.HttpContent content)
    Handle websocket handshake
    org.glassfish.grizzly.filterchain.NextAction
    handleRead(org.glassfish.grizzly.filterchain.FilterChainContext ctx)
    Handle Grizzly Connection read phase.
    org.glassfish.grizzly.filterchain.NextAction
    handleWrite(org.glassfish.grizzly.filterchain.FilterChainContext ctx)
    Handle Grizzly Connection write phase.
    protected void
    onHandshakeFailure(org.glassfish.grizzly.Connection connection, HandshakeException e)
    The method is called when WebSocket handshake fails for the Connection.
    protected void
    setIdleTimeout(org.glassfish.grizzly.filterchain.FilterChainContext ctx)
     
    protected static boolean
    webSocketInProgress(org.glassfish.grizzly.Connection connection)
     

    Methods inherited from class org.glassfish.grizzly.filterchain.BaseFilter

    createContext, exceptionOccurred, handleAccept, handleConnect, handleEvent, onAdded, onFilterChainChanged, onRemoved, toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • BaseWebSocketFilter

      public BaseWebSocketFilter()
      Constructs a new BaseWebSocketFilter with a default idle connection timeout of 15 minutes;
    • BaseWebSocketFilter

      public BaseWebSocketFilter(long wsTimeoutInSeconds)
      Constructs a new BaseWebSocketFilter with a default idle connection timeout of 15 minutes;
  • Method Details

    • handleClose

      public org.glassfish.grizzly.filterchain.NextAction handleClose(org.glassfish.grizzly.filterchain.FilterChainContext ctx) throws IOException
      Method handles Grizzly Connection close phase. Check if the Connection is a WebSocket, if yes - tries to close the websocket gracefully (sending close frame) and calls WebSocket.onClose(DataFrame). If the Grizzly Connection is not websocket - passes processing to the next filter in the chain.
      Specified by:
      handleClose in interface org.glassfish.grizzly.filterchain.Filter
      Overrides:
      handleClose in class org.glassfish.grizzly.filterchain.BaseFilter
      Parameters:
      ctx - FilterChainContext
      Returns:
      NextAction instruction for FilterChain, how it should continue the execution
      Throws:
      IOException
    • handleRead

      public org.glassfish.grizzly.filterchain.NextAction handleRead(org.glassfish.grizzly.filterchain.FilterChainContext ctx) throws IOException
      Handle Grizzly Connection read phase. If the Connection has associated WebSocket object (websocket connection), we check if websocket handshake has been completed for this connection, if not - initiate/validate handshake. If handshake has been completed - parse websocket DataFrames one by one and pass processing to appropriate WebSocket: WebSocketApplication for server- and client- side connections.
      Specified by:
      handleRead in interface org.glassfish.grizzly.filterchain.Filter
      Overrides:
      handleRead in class org.glassfish.grizzly.filterchain.BaseFilter
      Parameters:
      ctx - FilterChainContext
      Returns:
      NextAction instruction for FilterChain, how it should continue the execution
      Throws:
      IOException
    • handleWrite

      public org.glassfish.grizzly.filterchain.NextAction handleWrite(org.glassfish.grizzly.filterchain.FilterChainContext ctx) throws IOException
      Handle Grizzly Connection write phase. If the Connection has associated WebSocket object (websocket connection), we assume that message is websocket DataFrame and serialize it into a Buffer.
      Specified by:
      handleWrite in interface org.glassfish.grizzly.filterchain.Filter
      Overrides:
      handleWrite in class org.glassfish.grizzly.filterchain.BaseFilter
      Parameters:
      ctx - FilterChainContext
      Returns:
      NextAction instruction for FilterChain, how it should continue the execution
      Throws:
      IOException
    • handleHandshake

      protected abstract org.glassfish.grizzly.filterchain.NextAction handleHandshake(org.glassfish.grizzly.filterchain.FilterChainContext ctx, org.glassfish.grizzly.http.HttpContent content) throws IOException
      Handle websocket handshake
      Parameters:
      ctx - FilterChainContext
      content - HTTP message
      Returns:
      NextAction instruction for FilterChain, how it should continue the execution
      Throws:
      IOException
    • onHandshakeFailure

      protected void onHandshakeFailure(org.glassfish.grizzly.Connection connection, HandshakeException e)
      The method is called when WebSocket handshake fails for the Connection.
      Parameters:
      connection -
      e -
    • webSocketInProgress

      protected static boolean webSocketInProgress(org.glassfish.grizzly.Connection connection)
    • setIdleTimeout

      protected void setIdleTimeout(org.glassfish.grizzly.filterchain.FilterChainContext ctx)