Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/DirHandler.java @ 1598:c78d6a4e2d9a
web logging
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 04 Apr 2021 15:09:51 -0600 |
parents | 27efb1fcbcb5 |
children | 55d7b60c074d |
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; |
1273 | 10 import java.util.Arrays; |
11 import java.util.Comparator; | |
1140
bf03d687eaff
webserver - add dir handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1139
diff
changeset
|
12 import java.util.Date; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
13 import goodjava.webserver.Handler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
14 import goodjava.webserver.Request; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
15 import goodjava.webserver.Response; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
16 import goodjava.webserver.ResponseOutputStream; |
1139
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 |
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
1168 | 19 public final class DirHandler implements Handler { |
20 private final FileHandler fileHandler; | |
1139
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 |
1168 | 22 public DirHandler(FileHandler fileHandler) { |
23 this.fileHandler = fileHandler; | |
1139
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 } |
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 |
1273 | 26 private static final Comparator<File> sorter = new Comparator<File>() { |
27 public int compare(File f1, File f2) { | |
28 return f1.getName().compareTo(f2.getName()); | |
29 } | |
30 }; | |
31 | |
1168 | 32 public Response handle(Request request) { |
33 try { | |
34 File file = fileHandler.file(request); | |
35 if( request.path.endsWith("/") && file.isDirectory() ) { | |
36 DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz"); | |
37 Response response = new Response(); | |
38 response.headers.put( "content-type", "text/html; charset=utf-8" ); | |
39 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) ); | |
40 writer.write( "<!doctype html><html>" ); | |
41 writer.write( "<head><style>td{padding: 2px 8px}</style></head>" ); | |
42 writer.write( "<body>" ); | |
43 writer.write( "<h1>Directory: "+request.path+"</h1>" ); | |
44 writer.write( "<table border=0>" ); | |
1273 | 45 File[] a = file.listFiles(); |
46 Arrays.sort(a,sorter); | |
47 for( File child : a ) { | |
1168 | 48 String name = child.getName(); |
49 if( child.isDirectory() ) | |
50 name += '/'; | |
51 writer.write( "<tr>" ); | |
52 writer.write( "<td><a href='"+name+"'>"+name+"</a></td>" ); | |
53 writer.write( "<td>"+child.length()+" bytes</td>" ); | |
54 writer.write( "<td>"+fmt.format(new Date(child.lastModified()))+"</td>" ); | |
55 writer.write( "</tr>" ); | |
56 } | |
57 writer.write( "</table>" ); | |
58 writer.write( "</body>" ); | |
59 writer.write( "</html>" ); | |
60 writer.close(); | |
61 return response; | |
1139
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 } |
1168 | 63 return null; |
64 } catch(IOException e) { | |
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 } |