Class ChannelHandlerUtils

java.lang.Object
dev.tachyonmcp.core.transport.netty.ChannelHandlerUtils

public final class ChannelHandlerUtils extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    captureInitRequest(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpRequest req, ServerEngine server)
    When a custom SessionIdGenerator is configured on a stateful server, stashes a detached copy of the request (method/URI/headers) on the channel's interaction context so the generator can read it at session-creation time.
    static @Nullable ChannelContext
    getInteractionContext(io.netty.channel.ChannelHandlerContext ctx)
    Returns the protocol interaction context bound to this channel, or null.
    static @Nullable Session
    getSession(io.netty.channel.Channel channel)
    Returns the session bound to this channel, or null.
    requireInteractionContext(io.netty.channel.ChannelHandlerContext ctx)
     
    static void
    sendAccepted(io.netty.channel.ChannelHandlerContext ctx, @Nullable String origin)
     
    static io.netty.channel.ChannelFuture
    sendPlainTextAndClose(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponseStatus status, String message)
    Writes a text/plain response and closes the connection.
    static io.netty.channel.ChannelFuture
    sendPlainTextAndClose(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponseStatus status, String message, @Nullable String origin)
    static io.netty.channel.ChannelFuture
    sendResponseAndClose(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponseStatus status, String contentType, byte[] body, @Nullable String origin)
    Zero-copy overload for GC-managed bodies: wraps the byte[] at send time on the event loop.
    static io.netty.channel.ChannelFuture
    sendResponseAndClose(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponseStatus status, String contentType, io.netty.buffer.ByteBuf body, @Nullable String origin)
    Writes a response with the given content type and body, then closes the connection with a Connection: close header.
    static void
    setSession(io.netty.channel.ChannelHandlerContext ctx, Session session)
    Binds a session to the channel and installs the SessionTouchHandler if not already present.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • setSession

      public static void setSession(io.netty.channel.ChannelHandlerContext ctx, Session session)
      Binds a session to the channel and installs the SessionTouchHandler if not already present. Every outbound byte written to this channel will refresh the session's liveness.
      Parameters:
      ctx - the channel handler context
      session - the session to bind
    • getSession

      public static @Nullable Session getSession(io.netty.channel.Channel channel)
      Returns the session bound to this channel, or null.
      Parameters:
      channel - the channel
      Returns:
      the session, or null
    • getInteractionContext

      public static @Nullable ChannelContext getInteractionContext(io.netty.channel.ChannelHandlerContext ctx)
      Returns the protocol interaction context bound to this channel, or null.
      Parameters:
      ctx - the channel handler context
      Returns:
      the interaction context, or null
    • requireInteractionContext

      public static ChannelContext requireInteractionContext(io.netty.channel.ChannelHandlerContext ctx)
    • captureInitRequest

      public static void captureInitRequest(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpRequest req, ServerEngine server)
      When a custom SessionIdGenerator is configured on a stateful server, stashes a detached copy of the request (method/URI/headers) on the channel's interaction context so the generator can read it at session-creation time. A copy is required because the pooled request is released before the async dispatch runs.
      Parameters:
      ctx - the channel handler context
      req - the HTTP request to capture
      server - the server engine
    • sendAccepted

      public static void sendAccepted(io.netty.channel.ChannelHandlerContext ctx, @Nullable String origin)
    • sendPlainTextAndClose

      public static io.netty.channel.ChannelFuture sendPlainTextAndClose(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponseStatus status, String message)
      Writes a text/plain response and closes the connection. The response carries Connection: close so the client does not return the socket to its keep-alive pool (reusing a socket the server is about to close causes intermittent "other side closed" errors, e.g. with undici on Linux). Prefer this over sendPlainText(...).addListener(CLOSE).
    • sendPlainTextAndClose

      public static io.netty.channel.ChannelFuture sendPlainTextAndClose(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponseStatus status, String message, @Nullable String origin)
    • sendResponseAndClose

      public static io.netty.channel.ChannelFuture sendResponseAndClose(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponseStatus status, String contentType, io.netty.buffer.ByteBuf body, @Nullable String origin)
      Writes a response with the given content type and body, then closes the connection with a Connection: close header. See sendPlainTextAndClose(io.netty.channel.ChannelHandlerContext, io.netty.handler.codec.http.HttpResponseStatus, java.lang.String) for why the header matters.
    • sendResponseAndClose

      public static io.netty.channel.ChannelFuture sendResponseAndClose(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponseStatus status, String contentType, byte[] body, @Nullable String origin)
      Zero-copy overload for GC-managed bodies: wraps the byte[] at send time on the event loop.