Class ChannelUtils


  • public abstract class ChannelUtils
    extends Object
    A collection of helper methods around Netty channels.
    Author:
    [email protected]
    • Constructor Summary

      Constructors 
      Constructor Description
      ChannelUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String errorMessageWithRootCause​(Throwable e)
      Write detailed exception text.
      static String formatErrorMessage​(String message, Throwable e, io.netty.channel.ChannelHandlerContext ctx)
      Create a detailed error message from an exception, including current handle (port).
      static com.yammer.metrics.core.Counter getHttpStatusCounter​(io.netty.channel.ChannelHandlerContext ctx, int status)
      Get a counter for ~proxy.listeners.http-requests.status.###.count metric for a specific status code, with port= point tag for added context.
      static InetAddress getRemoteAddress​(io.netty.channel.ChannelHandlerContext ctx)
      Get InetAddress for the current channel.
      static String getRemoteName​(io.netty.channel.ChannelHandlerContext ctx)
      Get remote client's address as string (without rDNS lookup) and local port
      static io.netty.handler.codec.http.HttpResponse makeResponse​(io.netty.handler.codec.http.HttpResponseStatus status, Object contents)
      Create FullHttpResponse based on provided status and body contents.
      static void writeHttpResponse​(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponse response, boolean keepAlive)
      Writes HTTP response back to client.
      static void writeHttpResponse​(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponse response, io.netty.handler.codec.http.HttpMessage request)
      Writes HTTP response back to client.
      static void writeHttpResponse​(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponseStatus status, Object contents, boolean keepAlive)
      Writes HTTP response back to client.
      static void writeHttpResponse​(io.netty.channel.ChannelHandlerContext ctx, io.netty.handler.codec.http.HttpResponseStatus status, Object contents, io.netty.handler.codec.http.HttpMessage request)
      Writes HTTP response back to client.
    • Constructor Detail

      • ChannelUtils

        public ChannelUtils()
    • Method Detail

      • formatErrorMessage

        public static String formatErrorMessage​(String message,
                                                @Nullable
                                                Throwable e,
                                                @Nullable
                                                io.netty.channel.ChannelHandlerContext ctx)
        Create a detailed error message from an exception, including current handle (port).
        Parameters:
        message - the error message
        e - the exception (optional) that caused the error
        ctx - ChannelHandlerContext (optional) to extract remote client ip
        Returns:
        formatted error message
      • writeHttpResponse

        public static void writeHttpResponse​(io.netty.channel.ChannelHandlerContext ctx,
                                             io.netty.handler.codec.http.HttpResponseStatus status,
                                             Object contents,
                                             io.netty.handler.codec.http.HttpMessage request)
        Writes HTTP response back to client.
        Parameters:
        ctx - Channel handler context
        status - HTTP status to return with the response
        contents - Response body payload (JsonNode or CharSequence)
        request - Incoming request (used to get keep-alive header)
      • writeHttpResponse

        public static void writeHttpResponse​(io.netty.channel.ChannelHandlerContext ctx,
                                             io.netty.handler.codec.http.HttpResponseStatus status,
                                             Object contents,
                                             boolean keepAlive)
        Writes HTTP response back to client.
        Parameters:
        ctx - Channel handler context
        status - HTTP status to return with the response
        contents - Response body payload (JsonNode or CharSequence)
        keepAlive - Keep-alive requested
      • writeHttpResponse

        public static void writeHttpResponse​(io.netty.channel.ChannelHandlerContext ctx,
                                             io.netty.handler.codec.http.HttpResponse response,
                                             io.netty.handler.codec.http.HttpMessage request)
        Writes HTTP response back to client.
        Parameters:
        ctx - Channel handler context.
        response - HTTP response object.
        request - HTTP request object (to extract keep-alive flag).
      • writeHttpResponse

        public static void writeHttpResponse​(io.netty.channel.ChannelHandlerContext ctx,
                                             io.netty.handler.codec.http.HttpResponse response,
                                             boolean keepAlive)
        Writes HTTP response back to client.
        Parameters:
        ctx - Channel handler context.
        response - HTTP response object.
        keepAlive - Keep-alive requested.
      • makeResponse

        public static io.netty.handler.codec.http.HttpResponse makeResponse​(io.netty.handler.codec.http.HttpResponseStatus status,
                                                                            Object contents)
        Create FullHttpResponse based on provided status and body contents.
        Parameters:
        status - response status.
        contents - response body.
        Returns:
        http response object
      • errorMessageWithRootCause

        public static String errorMessageWithRootCause​(@Nonnull
                                                       Throwable e)
        Write detailed exception text.
        Parameters:
        e - Exceptions thrown
        Returns:
        error message
      • getRemoteName

        @Nonnull
        public static String getRemoteName​(@Nullable
                                           io.netty.channel.ChannelHandlerContext ctx)
        Get remote client's address as string (without rDNS lookup) and local port
        Parameters:
        ctx - Channel handler context
        Returns:
        remote client's address in a string form
      • getRemoteAddress

        public static InetAddress getRemoteAddress​(@Nonnull
                                                   io.netty.channel.ChannelHandlerContext ctx)
        Get InetAddress for the current channel.
        Parameters:
        ctx - Channel handler's context.
        Returns:
        remote address
      • getHttpStatusCounter

        public static com.yammer.metrics.core.Counter getHttpStatusCounter​(io.netty.channel.ChannelHandlerContext ctx,
                                                                           int status)
        Get a counter for ~proxy.listeners.http-requests.status.###.count metric for a specific status code, with port= point tag for added context.
        Parameters:
        ctx - channel handler context where a response is being sent.
        status - response status code.