This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
forked from ucsd-cse15l-f22/wavelet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchEngine.java
53 lines (47 loc) · 1.87 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
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
class Handler1 implements URLHandler {
// The one bit of state on the server: a number that will be manipulated by
// various requests.
List<String> stringList = new ArrayList<String>();
List<String> foundList = new ArrayList<String>();
public String handleRequest(URI url) {
if (url.getPath().equals("/")) {
return String.format("Genevieve's List: %s \n", stringList);
} else if (url.getPath().equals("/search")) {
foundList.clear();
String[] parameters = url.getQuery().split("=");
if (parameters[0].equals("s")) {
for (int i = 0; i < stringList.size(); i++) {
if (stringList.get(i).contains(parameters[1])) {
foundList.add(stringList.get(i));
}
}
return String.format("The strings in this list: %s contain %s \n", foundList, parameters[1]);
}
} else {
System.out.println("Path: " + url.getPath());
if (url.getPath().contains("/add")) {
String[] parameters = url.getQuery().split("=");
if (parameters[0].equals("s")) {
stringList.add(parameters[1]);
return String.format("Added %s! It's Genevieve's list is now %s \n", parameters[1], stringList);
}
}
return "404 Not Found!";
}
return null;
}
}
public 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 Handler1());
}
}