forked from ucsd-cse15l-f22/wavelet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchEngine.java
59 lines (48 loc) · 1.81 KB
/
SearchEngine.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
import java.io.IOException;
import java.net.URI;
class StringHandler implements URLHandler {
String str = "";
String[] arrStr = new String[999999999];
int num = 0;
public String handleRequest(URI url) {
if (url.getPath().equals("/")) {
return String.format("List of Words: \n%s", str);
}
else if (url.getPath().contains("/add")){
System.out.println("Path: " + url.getPath());
String[] parameters1 = url.getQuery().split("=");
if (parameters1[0].equals("s") && num < 999999999) {
str = str + parameters1[1] + "\n";
arrStr[num] = parameters1[1];
num++;
return parameters1[1] + " has been added.";
}
}
else if (url.getPath().contains("/search")) {
System.out.println("Path: " + url.getPath());
String[] parameters2 = url.getQuery().split("=");
if (parameters2[0].equals("s")) {
String returnStr = "The added words containing " + "\"" + parameters2[1] + "\" are: \n";
for (String string: arrStr) {
if (string != null) {
if (string.contains(parameters2[1])) {
returnStr += (string + "\n");
}
}
}
return returnStr;
}
}
return "404 Not Found!";
}
}
class SearchEngine {
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 StringHandler());
}
}