Skip to content

Commit 3d10e11

Browse files
committed
Add logger to FileProvider / remove trash
1 parent d5f5d45 commit 3d10e11

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/express/Express.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ public void listen(ExpressListener onStart, int port) throws IOException {
294294
onStart.action();
295295

296296
} catch (IOException e) {
297-
// TODO: Handle errors
298297
e.printStackTrace();
299298
}
300299
}).start();

src/express/ExpressException.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public class ExpressException extends RuntimeException {
1010
* Constructs a new exception with {@code null} as its detail message.
1111
*/
1212
public ExpressException() {
13-
super();
1413
}
1514

1615
/**

src/express/http/request/RequestUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import java.util.HashMap;
99
import java.util.List;
1010

11-
class RequestUtils {
11+
final class RequestUtils {
12+
13+
private RequestUtils() {}
1214

1315
/**
1416
* Extract the cookies from the 'Cookie' header.
@@ -20,7 +22,7 @@ static HashMap<String, Cookie> parseCookies(Headers headers) {
2022
HashMap<String, Cookie> cookieList = new HashMap<>();
2123
List<String> headerCookies = headers.get("Cookie");
2224

23-
if (headerCookies == null || headerCookies.size() == 0) {
25+
if (headerCookies == null || headerCookies.isEmpty()) {
2426
return cookieList;
2527
}
2628

src/express/middleware/FileProvider.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
import java.nio.file.Path;
1313
import java.nio.file.Paths;
1414
import java.util.Date;
15+
import java.util.Optional;
16+
import java.util.logging.Level;
17+
import java.util.logging.Logger;
1518

1619
/**
1720
* @author Simon Reinisch
1821
* An middleware to provide access to static server-files.
1922
*/
2023
final class FileProvider implements HttpRequest {
2124

25+
private final Logger LOGGER;
2226
private FileProviderOptions OPTIONS;
2327
private String ROOT;
2428

@@ -30,6 +34,7 @@ final class FileProvider implements HttpRequest {
3034

3135
this.ROOT = rootDir.getAbsolutePath();
3236
this.OPTIONS = options;
37+
this.LOGGER = Logger.getLogger(this.getClass().getSimpleName());
3338
}
3439

3540
@Override
@@ -49,9 +54,13 @@ public void handle(Request req, Response res) {
4954
String name = reqFile.getFileName().toString();
5055

5156
try {
52-
reqFile = Files.walk(reqFile.getParent()).filter(sub -> getBaseName(sub).equals(name)).findFirst().get();
57+
Optional<Path> founded = Files.walk(reqFile.getParent()).filter(sub -> getBaseName(sub).equals(name)).findFirst();
58+
59+
if (founded.isPresent())
60+
reqFile = founded.get();
61+
5362
} catch (IOException e) {
54-
// TODO: Handle error
63+
this.LOGGER.log(Level.WARNING, "Cannot walg file tree.", e);
5564
}
5665
}
5766

@@ -93,11 +102,14 @@ private void finish(Path file, Request req, Response res) {
93102
OPTIONS.getHandler().handle(req, res);
94103

95104
try {
105+
96106
// Apply header
97107
if (OPTIONS.isLastModified())
98108
res.setHeader("Last-Modified", Utils.getGMTDate(new Date(Files.getLastModifiedTime(file).toMillis())));
99109
} catch (IOException e) {
100-
// TODO: Handle error
110+
res.sendStatus(Status._500);
111+
this.LOGGER.log(Level.WARNING, "Cannot read LastModifiedTime from file " + file.toString(), e);
112+
return;
101113
}
102114

103115
res.setHeader("Cache-Control", String.valueOf(OPTIONS.getMaxAge()));
@@ -109,4 +121,11 @@ private String getBaseName(Path path) {
109121
int index = name.lastIndexOf('.');
110122
return index == -1 ? name : name.substring(0, index);
111123
}
124+
125+
/**
126+
* @return The logger from this FileProvider instance.
127+
*/
128+
public Logger getLogger() {
129+
return LOGGER;
130+
}
112131
}

0 commit comments

Comments
 (0)