Class Server

java.lang.Object
java.lang.Thread
net.bjmsw.hda.vs.Server
All Implemented Interfaces:
Runnable

public class Server extends Thread
VS-Frontend Server

A custom-built HTTP server for the VS-Frontend project. Uses no external libraries or frameworks. Implemented on top of Java's built-in ServerSocket and Socket classes.

This server tries to implement as much of the HTTP specification as possible.

Currently supports: - GET requests - POST requests with form data - POST requests with file uploads - Responses with status codes and headers - Responses with HTML files - Responses with binary files - Custom Routing Helper (e.g. @Route("/path") annotation) - Wildcard routes (e.g. /wctest/*) - Files in the webroot can be accessed with /fs/* (for example) (wildcard with file retrieval) - Simple HTML templating with variables ({VARNAME}) (BHTML)

See Also:
  • Field Details

    • WEB_ROOT

      public static String WEB_ROOT
      The root directory of the web servers files. (e.g. HTML, CSS, JS, images, etc.) (like /public, /var/www/html, etc.)
    • VERBOSE

      public static final boolean VERBOSE
      Enable verbose logging.
      See Also:
    • PORT

      public static int PORT
      The port the server listens on.
    • messageClient

      public static MessageClient messageClient
    • config

      private static Config config
    • server

      private static Server server
    • port

      private final int port
    • routeHandler

      private final RouteHandler routeHandler
    • runningRPCServer

      private Thread runningRPCServer
    • loggedInRpcUsers

      private final Map<UUID,Benutzer> loggedInRpcUsers
    • loggedInWebUsers

      private final Map<UUID,Benutzer> loggedInWebUsers
    • dbConnection

      Connection dbConnection
  • Constructor Details

    • Server

      public Server(int port, RouteHandler routeHandler)
  • Method Details

    • startRPCServer

      private Thread startRPCServer(int port)
    • run

      public void run()
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • getServer

      public static Server getServer()
    • getDbConnection

      public Connection getDbConnection()
    • getLoggedInRpcUsers

      public Map<UUID,Benutzer> getLoggedInRpcUsers()
    • getLoggedInWebUsers

      public Map<UUID,Benutzer> getLoggedInWebUsers()
    • getConfig

      public static Config getConfig()
    • checkWebrootFileExists

      private static boolean checkWebrootFileExists(String file)
      Checks whether a file exists in the webroot
      Parameters:
      file - The file to check
      Returns:
      If the file exists
    • checkWebrootFileExists

      private static boolean checkWebrootFileExists(String file, Request r)
      Checks whether a file exists in the webroot Sends a 404 - Not Found error automatically
      Parameters:
      file - The file to check
      r - If the file exists
      Returns:
    • main

      public static void main(String[] args)
    • loadConfig

      private static void loadConfig()
    • index

      public static void index(Request request) throws IOException
      Handles the index route. Respond with a simple HTML file from the webroot.
      Parameters:
      request - The request object containing the client's request information.
      Throws:
      IOException - if an IO error occurs while responding to the request.
    • test

      public static void test(Request request) throws IOException
      Respond with a image file from the webroot.
      Parameters:
      request - The request object containing the client's request information.
      Throws:
      IOException - if an IO error occurs while responding to the request.
    • vibin

      public static void vibin(Request request) throws IOException
      Respond with a video file from the webroot.
      Parameters:
      request - The request object containing the client's request information.
      Throws:
      IOException - if an IO error occurs while responding to the request.
    • testForm

      public static void testForm(Request request)
      Handles the "/testForm" route. This method handles POST requests and expects form data with "name" and "age" fields. If the form data contains both fields, it responds with a plain text message containing the name and age. If the form data is invalid or missing, it responds with a BAD_REQUEST error. If the request method is not POST, it responds with a METHOD_NOT_ALLOWED error.
      Parameters:
      request - The request object containing the client's request information.
    • echoFile

      public static void echoFile(Request r)
      Handles the "/echoFile" route. This method handles POST requests and processes form data with a file field named "file". It responds with the file that was uploaded. (Simply echoes the file back to the client)
      Parameters:
      r - The request object containing the client's request information.
    • echoForm

      public static void echoForm(Request r)
    • upload

      public static void upload(Request r)
      Implements a simple file upload route. This method handles POST requests and processes form data with a file field named "file".
      Parameters:
      r - The request object containing the client's request information.
    • wildcardTest

      public static void wildcardTest(Request r)
      Handles a wildcard route with path matching "/wctest/*".
      Parameters:
      r - The request object containing the client's request information.
    • wildcardTest2

      public static void wildcardTest2(Request r)
      Handles a wildcard route with path matching "/wctest/test/*".
      Parameters:
      r -
    • ping

      public static void ping(Request request)
      Sends a ping request to the server and receives a pong response.
      Parameters:
      request - The request object containing the client's request information.
    • getFromWebroot

      public static void getFromWebroot(Request r)
      Retrieves a file from the webroot and sends it as a response.
      Parameters:
      r - The request object containing the client's request information.
    • bhtmlTest

      public static void bhtmlTest(Request r)
      Handles the "/bhtml" route. This method is annotated with the Route annotation and is called when the "/bhtml" path is requested. It takes a Request object as a parameter and responds with a BHTML (Binary HTML) file from the webroot. The BHTML file to be returned is "index.html" and it is populated with the data from the given Map. The data in the Map is used to substitute placeholders in the BHTML file with actual values.
      Parameters:
      r - The Request object containing the client's request information.
    • notFound

      public static void notFound(Request r)
      Gives access to the webroot files.
      Parameters:
      r - The request object containing the client's request information.
    • login

      public static void login(Request request)
    • simulation

      public static void simulation(Request r)
    • route

      public static void route(Request r)
    • createGame

      public static void createGame(Request r)
    • getActiveBuchmacher

      public static void getActiveBuchmacher(Request r)
    • getSpiele

      public static void getSpiele(Request r)
    • getWettangeboteForSpiel

      public static void getWettangeboteForSpiel(Request r)
    • checkToken

      public static void checkToken(Request r)
    • wetten

      public static void wetten(Request r)
    • getWetterInfo

      public static void getWetterInfo(Request r)
    • spielEvent

      public static void spielEvent(Request r)
    • endGame

      public static void endGame(Request r)
    • leaderTrigger

      public static void leaderTrigger(Request r)