This is a Maven project that uses the Maven standard project structure. It implements a webserver that serves files, which can be requested by HTTP requests of version 1.1 . As I focused on making a proof of concept, I only implemented the GET request. In particular, the GET request specifies the requested .txt file in the header "requestedFile". If the file is available in the directory "Data", the server will send it out.
The main function creates a server instance of the class Server that takes the port, number of workers, and the interface WebApplication as argument. In this case, the web-application FileServerApp is taken. Inside the main class, the server is started by executing the method run, which in turn lets the server wait for incoming requests. If a request comes, the server forwards it to the thread pool by wrapping it in a new instance of the class ClientHandler, which implements the interface Runnable. The ClientHandler defines how to process the request. In particular, the input stream of the request is parsed to an instance of HttpRequest. Then, the request is forwarded to the web-application, which returns a instance of a extension of the abstract class HttpResponse. That object sends then the response to the client back. Depending on the header "Connection" of the request, the ClientHandler keeps the request alive, or closes it. If it is kept alive, it will be closed by timeout.
The main resource for this implementation is this repository. I adjusted the code a little, in order to contribute. The major changes are:
- Reduced functionality
- Request file by header
- Different logging
- New project structure
- Added keep-alive behaviour
Make a Simple HTTP Server in Java (Youtube series)
Apache Maven Tutorial: Einführung in Build-Prozess & Abhängigkeiten für Anfänger ohne Vorkenntnisse
Java für Fortgeschrittene (Youtube series)
- I did not know what a Maven project is.
- My Java skills were from a university course in 2019, so my code is very "junior".
- I used an REST API via Python once
- I did not know the HTTP protocol
Now, I know all of them. So, thank you very much for the challenge.