forked from ucsd-cse15l-f22/docsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocSearchServer.java
68 lines (61 loc) · 2.15 KB
/
DocSearchServer.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
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
class FileHelpers {
static List<File> getFiles(Path start) throws IOException {
File f = start.toFile();
List<File> result = new ArrayList<>();
if(f.isDirectory()) {
System.out.println("It's a folder");
File[] paths = f.listFiles();
for(File subFile: paths) {
result.addAll(getFiles(subFile.toPath()));
}
}
else {
result.add(start.toFile());
}
return result;
}
static String readFile(File f) throws IOException {
System.out.println(f.toString());
return new String(Files.readAllBytes(f.toPath()));
}
}
class Handler implements URLHandler {
List<File> files;
Handler(String directory) throws IOException {
this.files = FileHelpers.getFiles(Paths.get(directory));
}
public String handleRequest(URI url) throws IOException {
/*if (url.getPath().equals("/")) {
return String.format("There are %n files to search", paths);
}
else {
System.out.println("Path: " + url.getPath());
if (url.getPath().contains("/search")){
String[] args = url.getQuery().split("=");
String[] output = FileHelpers.getFiles(args[0]);
String outputString = String.join(", ", output);
return String.format("There were %n files found: %s", output.length, outputString);
}
}*/
return "Don't know how to handle that path!";
}
}
class DocSearchServer {
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("./technical/"));
}
}