Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/FileHandler.java @ 1727:63ab02188e9d
change priority
author | Vadim Filimonov <fffilimonov@yandex.ru> |
---|---|
date | Thu, 11 Aug 2022 13:11:53 +0200 |
parents | fa066aaa068c |
children |
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.IOException; |
bf03d687eaff
webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1139
diff
changeset
|
6 import java.text.DateFormat; |
bf03d687eaff
webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1139
diff
changeset
|
7 import java.text.SimpleDateFormat; |
1604
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
8 import java.text.ParseException; |
1140
bf03d687eaff
webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1139
diff
changeset
|
9 import java.util.Date; |
1604
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
10 import java.util.TimeZone; |
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; |
1604
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
14 import goodjava.webserver.Status; |
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(); | |
1604
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
43 |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
44 DateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
45 fmt.setTimeZone(TimeZone.getTimeZone("GMT")); |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
46 String lastModified = fmt.format(new Date(file.lastModified())); |
1607 | 47 String ifMod = (String)request.headers.get("If-Modified-Since"); |
1604
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
48 if( ifMod != null ) { |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
49 try { |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
50 Date ifModDate = fmt.parse(ifMod); |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
51 if( ifModDate != null && ifModDate.getTime() >= fmt.parse(lastModified).getTime() ) { |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
52 response.status = Status.NOT_MODIFIED; |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
53 return response; |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
54 } |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
55 |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
56 } catch(ParseException e) {} |
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
57 } |
1607 | 58 response.headers.put("Last-Modified",lastModified); |
1604
8a7519dee55c
FileHandler handles if-modified-since
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
59 |
1168 | 60 response.body = new Response.Body( file.length(), new FileInputStream(file) ); |
61 return response; | |
62 } | |
63 return null; | |
1140
bf03d687eaff
webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1139
diff
changeset
|
64 } catch(IOException e) { |
bf03d687eaff
webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1139
diff
changeset
|
65 throw new RuntimeException(e); |
1139
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
66 } |
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
67 } |
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
68 } |