Class HttpServerPageHandler

All Implemented Interfaces:
ChannelHandler, ChannelInboundHandler

public class HttpServerPageHandler extends SimpleChannelInboundHandler<FullHttpRequest>
Processes HTTP requests and passes successful ones to the appropriate registered final handler
Author:
gmt2001
  • Method Details

    • channelRead0

      protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception
      Handles incoming HTTP requests and passes valid ones to the appropriate HttpRequestHandler If a handler is not available for the requested path, then 404 NOT FOUND is sent back to the client
      Specified by:
      channelRead0 in class SimpleChannelInboundHandler<FullHttpRequest>
      Parameters:
      ctx - The ChannelHandlerContext of the session
      req - The FullHttpRequest containing the request
      Throws:
      Exception - Passes any thrown exceptions up the stack
    • exceptionCaught

      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
      Handles exceptions that are thrown up the stack
      Specified by:
      exceptionCaught in interface ChannelHandler
      Specified by:
      exceptionCaught in interface ChannelInboundHandler
      Overrides:
      exceptionCaught in class ChannelInboundHandlerAdapter
      Parameters:
      ctx - The ChannelHandlerContext of the session
      cause - The exception
    • detectContentType

      public static String detectContentType(String fileNameOrType)
      Detects the content MIME type based on the filename or manually provided type extension NOTE: This method ignores everything before the last . in the filename
      Parameters:
      fileNameOrType - The filename or type extension to check
      Returns:
      The valid MIME type, or text/plain if not recognized
    • prepareHttpResponse

      public static FullHttpResponse prepareHttpResponse(HttpResponseStatus status)
      Creates and prepares a FullHttpResponse for transmission of a status code only If the value of status is in the CLIENT ERROR 4xx, SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response
      Parameters:
      status - The HttpResponseStatus to return
      Returns:
      A FullHttpRequest that is ready to transmit
    • prepareHttpResponse

      public static FullHttpResponse prepareHttpResponse(HttpResponseStatus status, String content)
      Creates and prepares a FullHttpResponse for transmission as text/plain This method automatically sets the Content-Type and Content-Length headers If the value of status is in the CLIENT ERROR 4xx, SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set to text/html, unless fileNameOrType ends with json or xml
      Parameters:
      status - The HttpResponseStatus to return
      content - The content to send
      Returns:
      A FullHttpRequest that is ready to transmit
    • prepareHttpResponse

      public static FullHttpResponse prepareHttpResponse(HttpResponseStatus status, String content, String fileNameOrType)
      Creates and prepares a FullHttpResponse for transmission This method automatically sets the Content-Type and Content-Length headers If the value of status is in the CLIENT ERROR 4xx, SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set to text/html, unless fileNameOrType ends with json or xml
      Parameters:
      status - The HttpResponseStatus to return
      content - The content to send
      fileNameOrType - The filename or type extension for MIME type detection
      Returns:
      A FullHttpRequest that is ready to transmit
    • prepareHttpResponse

      public static FullHttpResponse prepareHttpResponse(HttpResponseStatus status, byte[] content, String fileNameOrType)
      Creates and prepares a FullHttpResponse for transmission This method automatically sets the Content-Type and Content-Length headers If the value of status is in the CLIENT ERROR 4xx, SERVER ERROR 5xx, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set to text/html, unless fileNameOrType ends with json or xml
      Parameters:
      status - The HttpResponseStatus to return
      content - The content to send
      fileNameOrType - The filename or type extension for MIME type detection
      Returns:
      A FullHttpRequest that is ready to transmit
    • sendHttpResponse

      public static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res)
      Transmits a FullHttpResponse back to the client
      Parameters:
      ctx - The ChannelHandlerContext of the session
      req - The FullHttpRequest containing the request
      res - The FullHttpResponse to transmit
    • sendHttpResponse

      public static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res, boolean forceclose)
      Transmits a FullHttpResponse back to the client
      Parameters:
      ctx - The ChannelHandlerContext of the session
      req - The FullHttpRequest containing the request
      res - The FullHttpResponse to transmit
      forceclose - If true, connection is closed regardless of status code; otherwise, only errors or unknown status codes will explicitly close the connection
    • listDirectory

      public static void listDirectory(ChannelHandlerContext ctx, FullHttpRequest req, Path p)
      Transmits a FullHttpResponse back to the client that lists the contents of the directory pointed to by p
      Parameters:
      ctx - The ChannelHandlerContext of the session
      req - The FullHttpRequest containing the request
      p - The Path to the directory to list
    • checkFilePermissions

      public static boolean checkFilePermissions(ChannelHandlerContext ctx, FullHttpRequest req, Path p, boolean directoryAllowed)
      Checks if the file or directory pointed to by p exists, is not hidden, is not a symlink, and is readable Sends back a 404 NOT FOUND or 403 FORBIDDEN on failure
      Parameters:
      ctx - The ChannelHandlerContext of the session
      req - The FullHttpRequest containing the request
      p - The Path to the file or directory to check
      directoryAllowed - Indicates if directories are allowed. If set to false, will cause a 403 FORBIDDEN if p is a directory
      Returns:
      and passed the same tests. false otherwise
    • registerHttpHandler

      public static void registerHttpHandler(String path, HttpRequestHandler handler)
      Registers a HTTP URI path to a HttpRequestHandler
      Parameters:
      path - The URI path to bind the handler to
      handler - The HttpRequestHandler that will handle the requests
      Throws:
      IllegalArgumentException - If path is either already registered, or illegal
      See Also:
      • validateUriPath
    • deregisterHttpHandler

      public static void deregisterHttpHandler(String path)
      Deregisters a HTTP URI path
      Parameters:
      path - The path to deregister
    • parseCookies

      public static Map<String,String> parseCookies(HttpHeaders headers)
      Parses out cookies and converts them to a Map
      Parameters:
      headers - The FullHttpRequest containing the request
      Returns:
      A Map of cookies
    • parsePost

      public static Map<String,String> parsePost(FullHttpRequest req)
      Parses out post data and converts it to a Map
      Parameters:
      req - The FullHttpRequest containing the request
      Returns:
      A Map of post data