Class HTTPServer.UriResource

java.lang.Object
ch.autumo.beetroot.server.HTTPServer.UriResource
All Implemented Interfaces:
Comparable<HTTPServer.UriResource>
Enclosing class:
HTTPServer

public static class HTTPServer.UriResource extends Object implements Comparable<HTTPServer.UriResource>
A single route: URI pattern, handler class, priority and init parameters. URI parameters such as :id are parsed into a regular expression.
  • Constructor Details

    • UriResource

      public UriResource(String uri, int priority, Class<?> handler, Object... initParameter)
      Creates a route with an explicit priority.
      Parameters:
      uri - the URI pattern
      priority - the route priority; lower values match first
      handler - the handler class
      initParameter - constructor parameters for the handler
    • UriResource

      public UriResource(String uri, Class<?> handler, Object... initParameter)
      Creates a route whose priority is derived from its parameter count.
      Parameters:
      uri - the URI pattern
      handler - the handler class
      initParameter - constructor parameters for the handler
  • Method Details

    • process

      public org.nanohttpd.protocols.http.response.Response process(Map<String,String> urlParams, org.nanohttpd.protocols.http.IHTTPSession session)
      Instantiates the handler of this route and dispatches the request.
      Parameters:
      urlParams - the parameters extracted from the URI
      session - the HTTP session
      Returns:
      the response, or an internal error response if the handler could not be created or invoked
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getUri

      public String getUri()
      Returns the URI pattern of this route.
      Returns:
      the URI pattern, or null for the not-found route
    • initParameter

      public <T> T initParameter(Class<T> paramClazz)
      Returns the first init parameter of the requested type.
      Type Parameters:
      T - the parameter type
      Parameters:
      paramClazz - the expected type
      Returns:
      the init parameter, or null if it is not available
    • initParameter

      public <T> T initParameter(int parameterIndex, Class<T> paramClazz)
      Returns the init parameter at the given index.
      Type Parameters:
      T - the parameter type
      Parameters:
      parameterIndex - the index of the parameter
      paramClazz - the expected type
      Returns:
      the init parameter, or null if the index is not available
    • match

      public Map<String,String> match(String url)
      Matches a URL against this route.
      Parameters:
      url - the normalized URL
      Returns:
      the extracted URI parameters, an empty map if the route has no parameters, or null if the URL does not match
    • compareTo

      public int compareTo(HTTPServer.UriResource that)
      Specified by:
      compareTo in interface Comparable<HTTPServer.UriResource>
    • setPriority

      public void setPriority(int priority)
      Sets the priority of this route; lower values match first.
      Parameters:
      priority - the priority
    • hasLanguage

      public boolean hasLanguage()
      Has this URI an language in it or not? Unused atm.
      Returns:
      true if this URI has a language route, otherwise false