Skip to content

Commit 8d1ae53

Browse files
committed
Added documentation
1 parent 53e899a commit 8d1ae53

17 files changed

+136
-1
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

webserver/logs/webserver.log

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2022-08-24 20:44:15 WARN App:47 - Getting ready to shutdown ...

webserver/src/main/java/com/ot/webserver/maven_ot_webserver/App.java

+23
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public void run() {
8181

8282
}
8383

84+
/**
85+
* Performs basic authentication on incoming requests
86+
*
87+
* @param socket Connection to perform authentication on
88+
* @return Returns a 2 element array of booleans.
89+
* The first for if the request header contains
90+
* the Authorization header and the second for
91+
* if the supplied credentials are valid.
92+
* @throws IOException
93+
*/
8494
public static boolean[] authStatus(Socket socket) throws IOException {
8595
boolean[] auth = new boolean[2];
8696
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
@@ -96,6 +106,14 @@ public static boolean[] authStatus(Socket socket) throws IOException {
96106
return auth;
97107
}
98108

109+
/**
110+
* Make a copy of the incoming request object
111+
*
112+
* @param socket The incoming request
113+
* @return The copy of the request object
114+
* @throws IOException
115+
* @throws ClassNotFoundException
116+
*/
99117
public static Socket cloneSocket(Socket socket) throws IOException, ClassNotFoundException {
100118
ByteArrayOutputStream baos = new ByteArrayOutputStream();
101119
ObjectOutputStream oos = new ObjectOutputStream(baos);
@@ -106,6 +124,11 @@ public static Socket cloneSocket(Socket socket) throws IOException, ClassNotFoun
106124
return (Socket) new ObjectInputStream(bais).readObject();
107125
}
108126

127+
/**
128+
* Sets the port the web server should listen on
129+
*
130+
* @param args
131+
*/
109132
public static void setPort(String[] args) {
110133
if(args.length == 1 && Integer.parseInt(args[0]) >= Config.PORT_MIN && Integer.parseInt(args[0]) <= Config.PORT_MAX) {
111134
Config.PORT = Integer.parseInt(args[0]);

webserver/src/main/java/com/ot/webserver/maven_ot_webserver/Authenticator.java

+38
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,27 @@
1010

1111
import at.favre.lib.crypto.bcrypt.BCrypt;
1212

13+
/**
14+
* Handles HTTP basic authentication
15+
*
16+
* @author tomiwa
17+
*
18+
*/
1319
public class Authenticator {
1420

1521
private static ArrayList<String> headers = new ArrayList<String>();
1622

23+
/**
24+
* Validate if authorization credentials are valid
25+
*
26+
* @param s The incoming connection object
27+
* @param base64 The authorization header value
28+
* extracted from the connection
29+
* object
30+
* @return Returns true if authentication
31+
* is successful and false if
32+
* otherwise
33+
*/
1734
public static boolean authenticate(Socket s, String base64) {
1835
String username = new String(Base64.getDecoder().decode(base64)).split(":")[0];
1936
String password = new String(Base64.getDecoder().decode(base64)).split(":")[1];
@@ -29,6 +46,12 @@ public static boolean authenticate(Socket s, String base64) {
2946
}
3047
}
3148

49+
/**
50+
* Builds the view for a failed basic authentication
51+
*
52+
* @param socket The connection to send the response to
53+
* @throws IOException
54+
*/
3255
public static void failedAuthenticationView(Socket socket) throws IOException {
3356
failedAuthenticationHeaders(401);
3457
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
@@ -41,6 +64,11 @@ public static void failedAuthenticationView(Socket socket) throws IOException {
4164
outputStream.flush();
4265
}
4366

67+
/**
68+
* Build the headers for a failed basic authentication
69+
*
70+
* @param statusCode The HTTP response code
71+
*/
4472
public static void failedAuthenticationHeaders(int statusCode) {
4573
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
4674
headers.add("HTTP/1.0 " + Integer.toString(statusCode) + " " + Config.STATUS_CODES.get(statusCode));
@@ -49,6 +77,11 @@ public static void failedAuthenticationHeaders(int statusCode) {
4977
headers.add("Server: Simple Java Web Server");
5078
}
5179

80+
/**
81+
*
82+
* @param socket The incoming request
83+
* @throws IOException
84+
*/
5285
public static void responseView(Socket socket) throws IOException {
5386
responseHeaders(401);
5487
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
@@ -61,6 +94,11 @@ public static void responseView(Socket socket) throws IOException {
6194
outputStream.flush();
6295
}
6396

97+
/**
98+
* Builds a HTTP header that requests for basic authentication
99+
*
100+
* @param statusCode The HTTP response code
101+
*/
64102
public static void responseHeaders(int statusCode) {
65103
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
66104
headers.add("HTTP/1.0 " + Integer.toString(statusCode) + " " + Config.STATUS_CODES.get(statusCode));

webserver/src/main/java/com/ot/webserver/maven_ot_webserver/Config.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
import org.json.simple.parser.JSONParser;
1212
import org.json.simple.parser.ParseException;
1313

14-
14+
/**
15+
* Parse the web server configurations
16+
*
17+
* @author tomiwa
18+
*
19+
*/
1520
public class Config {
1621

1722
public static String username = null;
@@ -48,6 +53,12 @@ public static Config getInstance() {
4853
return instance;
4954
}
5055

56+
/**
57+
* Loads the web server configurations
58+
*
59+
* @throws FileNotFoundException
60+
* @throws IOException
61+
*/
5162
public void loadConfigurations() throws FileNotFoundException, IOException {
5263
JSONParser jsonParser = new JSONParser();
5364
File properties = new File("src/main/resources/properties.json");

webserver/src/main/java/com/ot/webserver/maven_ot_webserver/request/Handler.java

+18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
import com.ot.webserver.maven_ot_webserver.Config;
1515
import com.ot.webserver.maven_ot_webserver.response.Response;
1616

17+
18+
/**
19+
* Processes request on its in thread and returns a response to the client
20+
*
21+
* @author tomiwa
22+
*
23+
*/
1724
public class Handler implements Runnable {
1825

1926
private Socket socket;
@@ -29,6 +36,11 @@ public void run() {
2936
processRequest(this.socket);
3037
}
3138

39+
/**
40+
* Build the response header and body
41+
*
42+
* @param s The incoming connection object
43+
*/
3244
public void processRequest(Socket s) {
3345
try {
3446
BufferedReader in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
@@ -74,6 +86,12 @@ public void processRequest(Socket s) {
7486
}
7587
}
7688

89+
/**
90+
* List files and directories contained in the requested URI
91+
*
92+
* @param uri The URI in the incoming request
93+
* @param file The requested file or directory
94+
*/
7795
public void directoryListing(String uri, File file) {
7896
StringBuilder output = new StringBuilder("<html><head><title>Index of " + uri);
7997
output.append("</title></head><body><h1>Index of " + uri);

webserver/src/main/java/com/ot/webserver/maven_ot_webserver/request/Listener.java

+16
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@
99

1010
import com.ot.webserver.maven_ot_webserver.Config;
1111

12+
/**
13+
* Handles the connection queue
14+
*
15+
* @author tomiwa
16+
*
17+
*/
1218
public class Listener {
1319

1420
private static final Listener instance = new Listener();
1521
private static BlockingQueue<Socket> connectionQueue = new ArrayBlockingQueue<Socket>(Config.CONNECTION_QUEUE);
1622
private static final Logger logger = LogManager.getLogger(Listener.class);
1723

24+
/**
25+
* Adds request to connection buffer
26+
*
27+
* @param s Incoming connection
28+
*/
1829
public void addRequestToQueue(Socket s) {
1930
try {
2031
logger.info("New connection from " + s.getRemoteSocketAddress() + " added to connection queue");
@@ -24,6 +35,11 @@ public void addRequestToQueue(Socket s) {
2435
}
2536
}
2637

38+
/**
39+
* Removes request from the connection buffer
40+
*
41+
* @return The connection object or null if connection buffer is empty
42+
*/
2743
public Socket handleRequest() {
2844
try {
2945
Socket s = connectionQueue.take();

webserver/src/main/java/com/ot/webserver/maven_ot_webserver/response/Response.java

+28
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
import com.ot.webserver.maven_ot_webserver.Config;
1414

15+
/**
16+
* Builds HTTP responses
17+
*
18+
* @author tomiwa
19+
*
20+
*/
1521
public class Response {
1622

1723
private byte[] bytes;
@@ -41,6 +47,9 @@ public Response(int statusCode, Socket socket, byte[] bytes, boolean isFile) {
4147
this.isFile = isFile;
4248
}
4349

50+
/**
51+
* Sets HTTP response header
52+
*/
4453
public void responseHeaders() {
4554
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
4655
headers.add("HTTP/1.0 " + Integer.toString(this.statusCode) + " " + Config.STATUS_CODES.get(this.statusCode));
@@ -50,6 +59,9 @@ public void responseHeaders() {
5059
headers.add("Server: Simple Java Web Server");
5160
}
5261

62+
/**
63+
* Selects the appropriate response view based on the status code
64+
*/
5365
public void responseView() {
5466
responseHeaders();
5567
try {
@@ -83,6 +95,14 @@ public void responseView() {
8395

8496
}
8597

98+
/**
99+
* Builds the response view
100+
*
101+
* @param outputStream HTTP response object
102+
* @param bytes The message to added in the
103+
* response body
104+
* @throws IOException
105+
*/
86106
public void responseBytes(DataOutputStream outputStream, byte[] bytes) throws IOException {
87107
for(String header : headers) {
88108
outputStream.writeBytes(header + "\r\n");
@@ -95,6 +115,14 @@ public void responseBytes(DataOutputStream outputStream, byte[] bytes) throws IO
95115
outputStream.flush();
96116
}
97117

118+
/**
119+
* Builds the response view
120+
*
121+
* @param outputStream HTTP response object
122+
* @param response The message to be added in the
123+
* response body
124+
* @throws IOException
125+
*/
98126
public void responseBytes(DataOutputStream outputStream, String response) throws IOException {
99127
for(String header : headers) {
100128
outputStream.writeBytes(header + "\r\n");

0 commit comments

Comments
 (0)