File tree Expand file tree Collapse file tree 1 file changed +6
-14
lines changed
src/main/java/org/example Expand file tree Collapse file tree 1 file changed +6
-14
lines changed Original file line number Diff line number Diff line change 1111
1212public 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}
You can’t perform that action at this time.
0 commit comments