Class WebsocketHandlerDecorator

java.lang.Object
io.fluxzero.sdk.web.WebsocketHandlerDecorator
All Implemented Interfaces:
io.fluxzero.common.handling.ParameterResolver<HasMessage>, HandlerDecorator

public class WebsocketHandlerDecorator extends Object implements HandlerDecorator, io.fluxzero.common.handling.ParameterResolver<HasMessage>
Decorator that adds WebSocket session support to handler classes and enables parameter injection for SocketSession.

This decorator supports @HandleWeb-annotated methods that interact over WebSocket connections, and transparently manages the lifecycle of SocketSession instances. It implements both HandlerDecorator and ParameterResolver to provide the following capabilities:

  • Automatically upgrades incoming WS_HANDSHAKE requests to WebSocket sessions if the handler includes methods annotated with HandleSocketOpen or similar, but does not explicitly handle the handshake itself. This ensures that endpoints which declare websocket handlers still result in a successful connection upgrade even without a dedicated handshake method.
  • Delegates WS_MESSAGE requests to the associated SocketSession for message dispatch and response.
  • Performs cleanup on WS_CLOSE requests, closing the session and invoking any associated HandleSocketClose handlers.
  • Resolves SocketSession method parameters in applicable WebSocket handler methods.

This decorator also tracks open WebSocket sessions and ensures that message dispatch is session-aware. It works transparently with the Flux client’s message routing and tracking infrastructure.

  • Constructor Details

    • WebsocketHandlerDecorator

      public WebsocketHandlerDecorator()
  • Method Details

    • resolve

      public Function<HasMessage,Object> resolve(Parameter p, Annotation methodAnnotation)
      Resolves a SocketSession parameter from the current HasMessage context.

      The session is tracked per session ID and reused for subsequent requests over the same connection. If no session exists for the current message's session ID, a new one is created.

      Specified by:
      resolve in interface io.fluxzero.common.handling.ParameterResolver<HasMessage>
      Parameters:
      p - the method parameter being resolved
      methodAnnotation - the handler method's annotation (e.g. @HandleWeb)
      Returns:
      a function that resolves the SocketSession from the message
    • getOrCreateSocketSession

      protected DefaultSocketSession getOrCreateSocketSession(HasMessage m)
    • matches

      public boolean matches(Parameter parameter, Annotation methodAnnotation, HasMessage value)
      Determines whether this resolver supports injecting a SocketSession for the given method parameter.

      The parameter must be assignable from SocketSession, and the method must be annotated with HandleWeb (or a compatible meta-annotation).

      Specified by:
      matches in interface io.fluxzero.common.handling.ParameterResolver<HasMessage>
      Parameters:
      parameter - the parameter being resolved
      methodAnnotation - the annotation present on the handler method
      value - the current message
      Returns:
      true if the parameter should be resolved by this resolver
    • onAbort

      protected void onAbort(DefaultSocketSession session, int code)
    • wrap

      public io.fluxzero.common.handling.Handler<DeserializingMessage> wrap(io.fluxzero.common.handling.Handler<DeserializingMessage> handler)
      Wraps a websocket-compatible handler with websocket-specific functionality.

      If the target handler supports websocket patterns (e.g., WS_HANDSHAKE or WS_MESSAGE), it is wrapped with logic to support automatic handshake negotiation, websocket message dispatch, and session cleanup on close.

      Specified by:
      wrap in interface HandlerDecorator
      Parameters:
      handler - the original handler
      Returns:
      the wrapped handler with websocket support if applicable
    • enableHandshake

      protected io.fluxzero.common.handling.Handler<DeserializingMessage> enableHandshake(io.fluxzero.common.handling.Handler<DeserializingMessage> handler, List<WebPattern> socketPatterns)
    • handleRequest

      protected io.fluxzero.common.handling.Handler<DeserializingMessage> handleRequest(io.fluxzero.common.handling.Handler<DeserializingMessage> handler)
    • closeOnError

      protected io.fluxzero.common.handling.Handler<DeserializingMessage> closeOnError(io.fluxzero.common.handling.Handler<DeserializingMessage> handler)
    • cleanUpOnClose

      protected io.fluxzero.common.handling.Handler<DeserializingMessage> cleanUpOnClose(io.fluxzero.common.handling.Handler<DeserializingMessage> handler)