Package net.bjmsw.hda.vs
Class Server
java.lang.Object
java.lang.Thread
net.bjmsw.hda.vs.Server
- All Implemented Interfaces:
Runnable
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static Config(package private) Connectionstatic MessageClientprivate final intstatic intThe port the server listens on.private final RouteHandlerprivate Threadprivate static Serverstatic final booleanEnable verbose logging.static StringThe root directory of the web servers files.Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidHandles the "/bhtml" route.static voidprivate static booleancheckWebrootFileExists(String file) Checks whether a file exists in the webrootprivate static booleancheckWebrootFileExists(String file, Request r) Checks whether a file exists in the webroot Sends a 404 - Not Found error automaticallystatic voidstatic voidHandles the "/echoFile" route.static voidstatic voidstatic voidstatic Configstatic voidRetrieves a file from the webroot and sends it as a response.static Serverstatic voidstatic voidstatic voidstatic voidHandles the index route.static voidprivate static voidstatic voidstatic voidstatic voidGives access to the webroot files.static voidSends a ping request to the server and receives a pong response.static voidvoidrun()static voidstatic voidprivate ThreadstartRPCServer(int port) static voidRespond with a image file from the webroot.static voidHandles the "/testForm" route.static voidImplements a simple file upload route.static voidRespond with a video file from the webroot.static voidstatic voidHandles a wildcard route with path matching "/wctest/*".static voidHandles a wildcard route with path matching "/wctest/test/*".Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
Field Details
-
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 VERBOSEEnable verbose logging.- See Also:
-
PORT
public static int PORTThe port the server listens on. -
messageClient
-
config
-
server
-
port
private final int port -
routeHandler
-
runningRPCServer
-
loggedInRpcUsers
-
loggedInWebUsers
-
dbConnection
Connection dbConnection
-
-
Constructor Details
-
Server
-
-
Method Details
-
startRPCServer
-
run
public void run() -
getServer
-
getDbConnection
-
getLoggedInRpcUsers
-
getLoggedInWebUsers
-
getConfig
-
checkWebrootFileExists
Checks whether a file exists in the webroot- Parameters:
file- The file to check- Returns:
- If the file exists
-
checkWebrootFileExists
Checks whether a file exists in the webroot Sends a 404 - Not Found error automatically- Parameters:
file- The file to checkr- If the file exists- Returns:
-
main
-
loadConfig
private static void loadConfig() -
index
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
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
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
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
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
-
upload
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
Handles a wildcard route with path matching "/wctest/*".- Parameters:
r- The request object containing the client's request information.
-
wildcardTest2
Handles a wildcard route with path matching "/wctest/test/*".- Parameters:
r-
-
ping
Sends a ping request to the server and receives a pong response.- Parameters:
request- The request object containing the client's request information.
-
getFromWebroot
Retrieves a file from the webroot and sends it as a response.- Parameters:
r- The request object containing the client's request information.
-
bhtmlTest
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
Gives access to the webroot files.- Parameters:
r- The request object containing the client's request information.
-
login
-
simulation
-
route
-
createGame
-
getActiveBuchmacher
-
getSpiele
-
getWettangeboteForSpiel
-
checkToken
-
wetten
-
getWetterInfo
-
spielEvent
-
endGame
-
leaderTrigger
-