forked from ucsd-cse15l-f23/chat-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatServer.java
74 lines (68 loc) · 2.59 KB
/
ChatServer.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.IOException;
import java.net.URI;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
class ChatHandler implements URLHandler {
String chatHistory = "";
public String handleRequest(URI url) {
// expect /chat?user=<name>&message=<string>
if (url.getPath().equals("/chat")) {
String[] params = url.getQuery().split("&");
String[] shouldBeUser = params[0].split("=");
String[] shouldBeMessage = params[1].split("=");
if (shouldBeUser[0].equals("user") && shouldBeMessage[0].equals("message")) {
String user = shouldBeUser[1];
String message = shouldBeMessage[1];
this.chatHistory += user + ": " + message + "\n\n";
return this.chatHistory;
} else {
return "Invalid parameters: " + String.join("&", params);
}
} else if (url.getPath().equals("/")) {
return this.chatHistory;
}
// expect /retrieve-history?file=<name>
else if (url.getPath().equals("/retrieve-history")) {
String[] params = url.getQuery().split("&");
String[] shouldBeFile = params[0].split("=");
if (shouldBeFile[0].equals("file")) {
String fileName = shouldBeFile[1];
// String fileName = shouldBeFileName[0]; // bug4: should be shouldBeFile[1]
ChatHistoryReader reader = new ChatHistoryReader();
try {
String[] contents = reader.readFileAsArray("chathistory/" + fileName);
for (String line : contents) {
this.chatHistory += line + "\n\n";
}
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
}
return this.chatHistory;
}
// expect /save?name=<name>
else if (url.getPath().equals("/save")) {
String[] params = url.getQuery().split("&");
String[] shouldBeFileName = params[0].split("=");
if (shouldBeFileName[0].equals("name")) {
File directory = new File("chathistory");
File file = new File(directory, shouldBeFileName[1]);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(this.chatHistory);
return "Data written to " + shouldBeFileName[1] + "in 'chat-history' folder.";
} catch (IOException e) {
e.printStackTrace();
return "Error: Something wrong happen during file save, check StackTrace";
}
}
}
return "404 Not Found";
}
}
class ChatServer {
public static void main(String[] args) throws IOException {
int port = Integer.parseInt(args[0]);
Server.start(port, new ChatHandler());
}
}