-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDmdbHomeHandler.java
More file actions
105 lines (96 loc) · 3.09 KB
/
Copy pathDmdbHomeHandler.java
File metadata and controls
105 lines (96 loc) · 3.09 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
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
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.nio.file.Files;
public class DmdbHomeHandler implements HttpHandler {
protected String baseHtml;
public DmdbHomeHandler() {
try {
this.baseHtml = loadHtmlFile("resources/dmdb.html");
}
catch (IOException ioe) {
this.baseHtml = "<html><body><p>Error loading HTML file.</p></body></html>";
ioe.printStackTrace();
}
}
public void handle(HttpExchange exchange) throws IOException {
String requestPath = exchange.getRequestURI().getPath();
String requestMethod = exchange.getRequestMethod();
if("/".equals(requestPath)&&("GET".equals(requestMethod)||"HEAD".equals(requestMethod))) {
exchange.getResponseHeaders().set("Content-Type", "text/html; charset=UTF-8");
if("GET".equals(requestMethod)) {
exchange.sendResponseHeaders(200, baseHtml.length());
try(OutputStream stream = exchange.getResponseBody()) {
stream.write(baseHtml.getBytes());
}
}
else if("HEAD".equals(requestMethod)) {
exchange.sendResponseHeaders(200, -1);
}
}
else if("HEAD".equals(requestMethod)) exchange.sendResponseHeaders(404, -1);
else if("GET".equals(requestMethod)) {
File fil = new File("resources" + requestPath);
if(fil.exists()) {
String fileContentType = getContentType(fil);
exchange.getResponseHeaders().set("Content-Type", fileContentType);
byte[] fileBytes = Files.readAllBytes(fil.toPath());
exchange.sendResponseHeaders(200, fileBytes.length);
try(OutputStream stream = exchange.getResponseBody()) {
stream.write(fileBytes);
}
}
else {
String errorMsg = "404 Not Found";
exchange.sendResponseHeaders(404, errorMsg.length());
try(OutputStream stream = exchange.getResponseBody()) {
stream.write(errorMsg.getBytes());
}
}
}
else {
String errorMsg = "Method not supported.";
exchange.sendResponseHeaders(501, errorMsg.length());
try(OutputStream stream = exchange.getResponseBody()) {
stream.write(errorMsg.getBytes());
}
}
exchange.close();
}
public String loadHtmlFile(String filePath) throws IOException {
StringBuilder bobTheHtmlBuilder = new StringBuilder();
try(BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while((line = reader.readLine()) != null) {
bobTheHtmlBuilder.append(line).append("\n");
}
}
return bobTheHtmlBuilder.toString();
}
public String getContentType(File file) {
String fileName = file.getName().toLowerCase();
if(fileName.endsWith(".html")) {
return "text/html; charset=UTF-8";
}
else if(fileName.endsWith(".jpg")) {
return "image/jpeg";
}
else if(fileName.endsWith(".png")) {
return "image/png";
}
else if(fileName.endsWith(".webp")) {
return "image/webp";
}
else if(fileName.endsWith(".css")) {
return "text/css";
}
else if(fileName.endsWith(".js")) {
return "application/javascript";
}
else return "application/octet-stream";
}
}