annotate src/goodjava/webserver/handlers/FileHandler.java @ 1601:de5a81327d9c

fix deadlock
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 09 Apr 2021 22:39:03 -0600
parents 27efb1fcbcb5
children 8a7519dee55c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
1 package goodjava.webserver.handlers;
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.File;
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.FileInputStream;
1140
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
5 import java.io.Writer;
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
6 import java.io.OutputStreamWriter;
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
7 import java.io.IOException;
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
8 import java.text.DateFormat;
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
9 import java.text.SimpleDateFormat;
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
10 import java.util.Date;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
11 import goodjava.webserver.Handler;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
12 import goodjava.webserver.Request;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
13 import goodjava.webserver.Response;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
14 import goodjava.webserver.ResponseOutputStream;
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
1167
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1165
diff changeset
17 public class FileHandler implements Handler {
7e6f28c769a1 better handlers
Franklin Schmidt <fschmidt@gmail.com>
parents: 1165
diff changeset
18 final File dir;
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 public FileHandler() {
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 this(".");
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public FileHandler(String pathname) {
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 this(new File(pathname));
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 public FileHandler(File dir) {
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 if( !dir.isDirectory() )
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 throw new RuntimeException("must be a directory");
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 this.dir = dir;
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 }
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
34 File file(Request request) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
35 return new File(dir,request.path);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
36 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
37
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 public Response handle(Request request) {
1140
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
39 try {
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
40 File file = file(request);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
41 if( file.isFile() ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
42 Response response = new Response();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
43 response.body = new Response.Body( file.length(), new FileInputStream(file) );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
44 return response;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
45 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
46 return null;
1140
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
47 } catch(IOException e) {
bf03d687eaff webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 1139
diff changeset
48 throw new RuntimeException(e);
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 }
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 }