Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/FileHandler.java @ 1528:3bd4d7963456
use goodjava/lucene/api
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 26 Jul 2020 23:11:53 -0600 |
parents | 27efb1fcbcb5 |
children | 8a7519dee55c |
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 | 17 public class FileHandler implements Handler { |
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 | 34 File file(Request request) { |
35 return new File(dir,request.path); | |
36 } | |
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 | 40 File file = file(request); |
41 if( file.isFile() ) { | |
42 Response response = new Response(); | |
43 response.body = new Response.Body( file.length(), new FileInputStream(file) ); | |
44 return response; | |
45 } | |
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 } |