Class BeetRootWebServer

java.lang.Object
org.nanohttpd.protocols.http.NanoHTTPD
ch.autumo.beetroot.server.HTTPServer
ch.autumo.beetroot.BeetRootWebServer
All Implemented Interfaces:
BeetRootService

public class BeetRootWebServer extends HTTPServer implements BeetRootService
The beetRoot Web Server and Template Engine.
  • Field Details

    • LOG

      protected static final org.slf4j.Logger LOG
      Logger for this class.
    • VIRTUAL_TMP_PATH

      public static final String VIRTUAL_TMP_PATH
      Virtual relative path for temporary stored files to server (e.g. generated images such as the 2FA QR code.
      See Also:
  • Constructor Details

    • BeetRootWebServer

      public BeetRootWebServer() throws Exception
      Server.
      Throws:
      Exception - exception
    • BeetRootWebServer

      public BeetRootWebServer(int port) throws Exception
      Server.
      Parameters:
      port - port
      Throws:
      Exception - exception
  • Method Details

    • createClientHandler

      protected org.nanohttpd.protocols.http.ClientHandler createClientHandler(Socket finalAccept, InputStream inputStream)
      Overrides:
      createClientHandler in class org.nanohttpd.protocols.http.NanoHTTPD
    • getAsyncRunner

      public org.nanohttpd.protocols.http.threading.IAsyncRunner getAsyncRunner()
      Get async runner.
      Returns:
      async runner
    • setBaseServer

      public void setBaseServer(BaseServer baseServer)
      Set base server.
      Parameters:
      baseServer - base server
    • start

      public void start() throws IOException
      Start the server.
      Overrides:
      start in class org.nanohttpd.protocols.http.NanoHTTPD
      Throws:
      IOException
    • start

      public void start(int timeout) throws IOException
      Starts the server (in setDaemon(true) mode).
      Overrides:
      start in class org.nanohttpd.protocols.http.NanoHTTPD
      Throws:
      IOException
    • start

      public void start(boolean daemon) throws IOException
      Start the server.
      Parameters:
      daemon - start the web server as a daemon thread?
      Throws:
      IOException - IO exception
    • start

      public void start(int timeout, boolean daemon) throws IOException
      Start the server.
      Overrides:
      start in class org.nanohttpd.protocols.http.NanoHTTPD
      Parameters:
      timeout - timeout to use for socket connections.
      daemon - start the thread daemon or not.
      Throws:
      IOException - if the socket is in use.
    • stop

      public void stop()
      Stop the server.
      Overrides:
      stop in class org.nanohttpd.protocols.http.NanoHTTPD
    • serve

      public org.nanohttpd.protocols.http.response.Response serve(org.nanohttpd.protocols.http.IHTTPSession session)
      Main serve method for the beetRoot-engine.
      Overrides:
      serve in class HTTPServer
      Parameters:
      session - nano session.
      Returns:
      response response
    • newTempFileManager

      public org.nanohttpd.protocols.http.tempfiles.ITempFileManager newTempFileManager()
      Description copied from interface: BeetRootService
      Get a new temporary file manager.
      Specified by:
      newTempFileManager in interface BeetRootService
      Returns:
      new temporary file manager
    • destroy

      public void destroy()
      Description copied from interface: BeetRootService
      Cleanup method. Close what needs to be closed and free resources.
      Specified by:
      destroy in interface BeetRootService
    • serve

      public org.nanohttpd.protocols.http.response.Response serve(BeetRootHTTPSession session, jakarta.servlet.http.HttpServletRequest request)
      Main serve method for the beetroot-engine in a servlet context
      Parameters:
      session - session.
      request - servlet request
      Returns:
      response response
    • serverCommandResponse

      public static org.nanohttpd.protocols.http.response.Response serverCommandResponse(BeetRootHTTPSession session, ClientAnswer answer)
      Creates the JSON response for a client answer.
      Parameters:
      session - the HTTP session
      answer - the client answer
      Returns:
      the response
    • serverResponse

      public static org.nanohttpd.protocols.http.response.Response serverResponse(BeetRootHTTPSession session, Class<?> handlerClass, Object... initParameter)
      Creates the response by instantiating and calling the given handler.
      Parameters:
      session - the HTTP session
      handlerClass - the handler class
      initParameter - the handler constructor parameters
      Returns:
      the response
    • getHandlerClass

      public final Class<?> getHandlerClass(String handlerName)
      Get a handler class by handler name.
      Parameters:
      handlerName - handler name
      Returns:
      handler class or null if not found
    • getDefaultHandlerClass

      public Class<?> getDefaultHandlerClass()
      Get default handler class. Overwrite for customization.
      Returns:
      default handler class
    • getDefaultHandlerEntity

      public String getDefaultHandlerEntity()
      Get default handler entity. Overwrite for customization.
      Returns:
      default handler entity
    • serveAtLast

      public org.nanohttpd.protocols.http.response.Response serveAtLast(BeetRootHTTPSession session)
      Last call before the routed website is served. At this point, templates have been parsed and compiled. Overwrite this method, if you still need to do something. In any case and at the end, it must call HTTPServer.serve(IHTTPSession)
      Parameters:
      session - HTTP sessiom
      Returns:
      response
    • addRoute

      public void addRoute(String url, int priority, Class<?> handler, Object... initParameter)
      New public method to add routes with priority. Beetroot has an own generic router where priorities are pre-defined.
      Parameters:
      url - url
      priority - priority
      handler - handler
      initParameter - init parameter for handler
    • addMappings

      public final void addMappings()
      Add mappings respectively set web routes.
      Overrides:
      addMappings in class HTTPServer