Class PathHandler

  • All Implemented Interfaces:
    HttpHandler

    public class PathHandler
    extends java.lang.Object
    implements HttpHandler
    Handler that dispatches to a given handler based of a prefix match of the path.

    This only matches a single level of a request, e.g if you have a request that takes the form:

    /foo/bar

    Author:
    Stuart Douglas
    • Constructor Detail

      • PathHandler

        public PathHandler​(HttpHandler defaultHandler)
      • PathHandler

        public PathHandler​(HttpHandler defaultHandler,
                           int cacheSize)
      • PathHandler

        public PathHandler()
      • PathHandler

        public PathHandler​(int cacheSize)
    • Method Detail

      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • handleRequest

        public void handleRequest​(HttpServerExchange exchange)
                           throws java.lang.Exception
        Description copied from interface: HttpHandler
        Handle the request.
        Specified by:
        handleRequest in interface HttpHandler
        Parameters:
        exchange - the HTTP request/response exchange
        Throws:
        java.lang.Exception
      • addPrefixPath

        public PathHandler addPrefixPath​(java.lang.String path,
                                         HttpHandler handler)
        Adds a path prefix and a handler for that path. If the path does not start with a / then one will be prepended.

        The match is done on a prefix bases, so registering /foo will also match /foo/bar. Though exact path matches are taken into account before prefix path matches. So if an exact path match exists it's handler will be triggered.

        If / is specified as the path then it will replace the default handler.

        Parameters:
        path - If the request contains this prefix, run handler.
        handler - The handler which is activated upon match.
        Returns:
        The resulting PathHandler after this path has been added to it.
      • addExactPath

        public PathHandler addExactPath​(java.lang.String path,
                                        HttpHandler handler)
        If the request path is exactly equal to the given path, run the handler.

        Exact paths are prioritized higher than prefix paths.

        Parameters:
        path - If the request path is exactly this, run handler.
        handler - Handler run upon exact path match.
        Returns:
        The resulting PathHandler after this path has been added to it.
      • removePath

        @Deprecated
        public PathHandler removePath​(java.lang.String path)
        Deprecated.
      • removePrefixPath

        public PathHandler removePrefixPath​(java.lang.String path)
      • removeExactPath

        public PathHandler removeExactPath​(java.lang.String path)