forked from ucsd-cse15l-f22/wavelet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchEngine.java
More file actions
46 lines (41 loc) · 1.61 KB
/
SearchEngine.java
File metadata and controls
46 lines (41 loc) · 1.61 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
40
41
42
43
44
45
46
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.URI;
import java.util.ArrayList;
class StringHandler implements URLHandler {
// The one bit of state on the server: a number that will be manipulated by
// various requests.
ArrayList<String> storedStrings = new ArrayList<>();
public String handleRequest(URI url) {
if (url.getPath().equals("/")) {
return String.format("/add?s=anewstringtoadd to add a new string; /search?s=app to query stored results.");
} else if (url.getPath().contains("/add")) {
String stringToAdd = url.getQuery().split("=")[1];
storedStrings.add(stringToAdd);
return String.format(stringToAdd + " was added to the stored list!");
} else {
System.out.println("Path: " + url.getPath());
if (url.getPath().contains("/search")) {
String result = "";
String substringToMatch = url.getQuery().split("=")[1];
for(String s : storedStrings){
if(s.indexOf(substringToMatch) >= 0){
result += s + " ";
}
}
return String.format(result);
}
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());
}
}