This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
FileServer.java
112 lines (101 loc) · 3.56 KB
/
FileServer.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;
import java.util.Collections;
class FileHelpers {
static List<File> getFiles(Path start) throws IOException {
File f = start.toFile();
List<File> result = new ArrayList<>();
if(f.isDirectory()) {
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 {
return new String(Files.readAllBytes(f.toPath()));
}
static String repeat(String s, int n) {
String result = "";
for(int i = 0; i < n; i += 1) {
result += s;
}
return result;
}
}
class Handler implements URLHandler {
List<File> paths;
Handler(String directory) throws IOException {
this.paths = FileHelpers.getFiles(Paths.get(directory));
}
public String handleRequest(URI url) throws IOException {
if (url.getPath().equals("/")) {
return String.format("Use /search?q=... to search the files!", paths.size());
} else if (url.getPath().equals("/search")) {
String[] parameters = url.getQuery().split("=");
if (parameters[0].equals("q")) {
List<File> foundPaths = new ArrayList<>();
for(File f: paths) {
if(f.toString().contains(parameters[1])) {
foundPaths.add(f);
}
}
Collections.sort(foundPaths);
String result = "";
String border = FileHelpers.repeat("=", 72);
for (File f: foundPaths) {
String contents = FileHelpers.readFile(f);
contents = contents.length() > 100 ? contents.substring(0, 100) : contents;
result += border + "\n" + f.toString() + ":\n" + contents + "...\n" + border + "\n";
}
if(foundPaths.size() == 0) {
return "No files found";
}
else {
return String.format("Found %d files:\n%s", foundPaths.size(), result);
}
}
else {
return "Couldn't find query parameter q";
}
}
else {
return "Don't know how to handle that path!";
}
}
}
/**
Usage:
java FileServer <port> <directory>
Starts the server on <port>, with a Handler configured to search the given
directory when processing /search requests.
The server is designed to process requests like /search?q=<string> and
respond with the paths and contents of all files whose path matches the given
string. So `q=ch` as the query would return files with names like "ch1.txt"
or "march.txt".
*/
class FileServer {
public static void main(String[] args) throws IOException {
if(args.length < 1){
System.out.println("Missing port number! Try any number between 1024 to 49151 as the first argument");
return;
}
if(args.length < 2){
System.out.println("Missing directory to search! Give the path to a directory of text files to search.");
return;
}
int port = Integer.parseInt(args[0]);
Server.start(port, new Handler(args[1]));
}
}