Class HTTPServer

java.lang.Object
org.nanohttpd.protocols.http.NanoHTTPD
ch.autumo.beetroot.server.HTTPServer
Direct Known Subclasses:
BeetRootWebServer

public class HTTPServer extends org.nanohttpd.protocols.http.NanoHTTPD
Nano HTTP server.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Base implementation of a route prioritizer that owns the route mappings.
    static class 
    General nanolet to inherit from if you provide text or html data, only fixed size responses will be generated.
    static class 
    Orders routes by the priority assigned when they were added.
    static class 
    General nanolet to inherit from if you provide stream data, only chucked responses will be generated.
    static class 
    Handling error 404 - unrecognized urls
    static class 
    General nanolet to print debug info's as a html page.
    static class 
    Handling index
    static class 
    Keeps routes in the order in which they were added.
    static interface 
    Holds the registered routes and defines their processing order.
    static class 
    Fallback handler for routes that are registered without an implementation.
    static class 
    Uses the priority that was passed to addRoute unchanged.
    static class 
    Serves static files from a directory that is passed as init parameter.
    static class 
    A single route: URI pattern, handler class, priority and init parameters.
    static interface 
    Responder for a routed URI.
    static class 
    Resolves a request URI to a registered route and processes the request with that route's handler.

    Nested classes/interfaces inherited from class org.nanohttpd.protocols.http.NanoHTTPD

    org.nanohttpd.protocols.http.NanoHTTPD.ResponseException
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final org.slf4j.Logger
    Logger.
     

    Fields inherited from class org.nanohttpd.protocols.http.NanoHTTPD

    asyncRunner, CONTENT_DISPOSITION_ATTRIBUTE_PATTERN, CONTENT_DISPOSITION_ATTRIBUTE_REGEX, CONTENT_DISPOSITION_PATTERN, CONTENT_DISPOSITION_REGEX, CONTENT_TYPE_PATTERN, CONTENT_TYPE_REGEX, hostname, interceptors, MIME_HTML, MIME_PLAINTEXT, MIME_TYPES, myPort, SOCKET_READ_TIMEOUT
  • Constructor Summary

    Constructors
    Constructor
    Description
    HTTPServer(int port)
    Creates the server on the given port.
    HTTPServer(String hostname, int port)
    Creates the server on the given host name and port.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Default routings, they are over writable.
    void
    addRoute(String url, Class<?> handler, Object... initParameter)
    Adds a route with the default priority 100.
    static String
    Removes a leading and a trailing slash from the given URI.
    void
    Removes the route with the given URI pattern.
    org.nanohttpd.protocols.http.response.Response
    serve(org.nanohttpd.protocols.http.IHTTPSession session)
     
    <T extends HTTPServer.UriResponder>
    void
    Sets the handler for URIs that match no route.
    <T extends HTTPServer.UriResponder>
    void
    Sets the handler for routes without an implementation.
    void
    Replaces the route prioritizer.

    Methods inherited from class org.nanohttpd.protocols.http.NanoHTTPD

    addHTTPInterceptor, closeAllConnections, createClientHandler, createServerRunnable, decodeParameters, decodeParameters, decodePercent, getHostname, getListeningPort, getMimeTypeForFile, getMyServerSocket, getServerSocketFactory, getTempFileManagerFactory, handle, isAlive, makeSecure, makeSSLSocketFactory, makeSSLSocketFactory, makeSSLSocketFactory, mimeTypes, safeClose, setAsyncRunner, setHTTPHandler, setServerSocketFactory, setTempFileManagerFactory, start, start, start, stop, wasStarted

    Methods inherited from class Object

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

    • LOG

      protected static final org.slf4j.Logger LOG
      Logger.
    • router

      protected HTTPServer.UriRouter router
  • Constructor Details

    • HTTPServer

      public HTTPServer(int port)
      Creates the server on the given port.
      Parameters:
      port - the port to listen on
    • HTTPServer

      public HTTPServer(String hostname, int port)
      Creates the server on the given host name and port.
      Parameters:
      hostname - the host name to bind to
      port - the port to listen on
  • Method Details

    • normalizeUri

      public static String normalizeUri(String value)
      Removes a leading and a trailing slash from the given URI.
      Parameters:
      value - the URI value, may be null
      Returns:
      the normalized URI, or null if the input was null
    • addMappings

      public void addMappings()
      Default routings, they are over writable.
      router.setNotFoundHandler(GeneralHandler.class);
      
    • addRoute

      public void addRoute(String url, Class<?> handler, Object... initParameter)
      Adds a route with the default priority 100.
      Parameters:
      url - the URI pattern
      handler - the handler class
      initParameter - constructor parameters for the handler
    • setNotImplementedHandler

      public <T extends HTTPServer.UriResponder> void setNotImplementedHandler(Class<T> handler)
      Sets the handler for routes without an implementation.
      Type Parameters:
      T - the responder type
      Parameters:
      handler - the handler class
    • setNotFoundHandler

      public <T extends HTTPServer.UriResponder> void setNotFoundHandler(Class<T> handler)
      Sets the handler for URIs that match no route.
      Type Parameters:
      T - the responder type
      Parameters:
      handler - the handler class
    • removeRoute

      public void removeRoute(String url)
      Removes the route with the given URI pattern.
      Parameters:
      url - the URI pattern
    • setRoutePrioritizer

      public void setRoutePrioritizer(HTTPServer.IRoutePrioritizer routePrioritizer)
      Replaces the route prioritizer.
      Parameters:
      routePrioritizer - the new prioritizer
    • serve

      public org.nanohttpd.protocols.http.response.Response serve(org.nanohttpd.protocols.http.IHTTPSession session)
      Overrides:
      serve in class org.nanohttpd.protocols.http.NanoHTTPD