forked from ucsd-cse15l-f22/wavelet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringServer.java
More file actions
39 lines (34 loc) · 1.13 KB
/
StringServer.java
File metadata and controls
39 lines (34 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.IOException;
import java.net.URI;
class Handler implements URLHandler {
// The String message that is manipulated and saved on the server
String message = "";
public String handleRequest(URI url) {
//Check the path of the url
if (url.getPath().equals("/add-message")) {
//Check if there is a valid query
String[] query = url.getQuery().split("=");
if (query.length == 2 && query[0].equals("s")) {
//Add the new message on and return it
if (message.equals("")) {
message += query[1];
}
else {
message += "\n" + query[1];
}
return message;
}
}
return "404 Not Found!";
}
}
class StringServer {
public static void main(String[] args) throws IOException {
if(args.length == 0){
System.out.println("Missing port number! Try any number between 1024 to 49151");
return;
}
int port = Integer.parseInt(args[0]);
Server.start(port, new Handler());
}
}