Skip to content

Commit 515bc8c

Browse files
committed
Merge remote-tracking branch 'origin/issue36' into Server‑side-caching-issue36
2 parents b2ef693 + fffdebf commit 515bc8c

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

src/main/java/org/example/StaticFileHandler.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,17 @@
1111

1212
public class StaticFileHandler {
1313
private static final String WEB_ROOT = "www";
14-
private byte[] fileBytes;
14+
private final CacheFilter cacheFilter = new CacheFilter();
1515

16-
public StaticFileHandler(){}
17-
18-
private void handleGetRequest(String uri) throws IOException {
19-
20-
File file = new File(WEB_ROOT, uri);
21-
fileBytes = Files.readAllBytes(file.toPath());
22-
23-
}
24-
25-
public void sendGetRequest(OutputStream outputStream, String uri) throws IOException{
26-
handleGetRequest(uri);
16+
public void sendGetRequest(OutputStream outputStream, String uri) throws IOException {
17+
byte[] fileBytes = cacheFilter.getOrFetch(uri,
18+
path -> Files.readAllBytes(new File(WEB_ROOT, path).toPath())
19+
);
20+
2721
HttpResponseBuilder response = new HttpResponseBuilder();
2822
response.setHeaders(Map.of("Content-Type", "text/html; charset=utf-8"));
2923
response.setBody(fileBytes);
3024
PrintWriter writer = new PrintWriter(outputStream, true);
3125
writer.println(response.build());
32-
3326
}
34-
3527
}

0 commit comments

Comments
 (0)