Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/DirHandler.java @ 1610:94ea190714dd
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 13 May 2021 17:26:34 -0600 |
parents | fa066aaa068c |
children | d88fb2eb15aa |
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) { | |
1602
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
28 boolean f1IsDir = f1.isDirectory(); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
29 boolean f2IsDir = f2.isDirectory(); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
30 if( f1IsDir && !f2IsDir ) |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
31 return -1; |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
32 if( !f1IsDir && f2IsDir ) |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
33 return 1; |
1273 | 34 return f1.getName().compareTo(f2.getName()); |
35 } | |
36 }; | |
37 | |
1168 | 38 public Response handle(Request request) { |
39 try { | |
40 File file = fileHandler.file(request); | |
1602
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
41 String path = request.path; |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
42 if( path.endsWith("/") && file.isDirectory() ) { |
1168 | 43 DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz"); |
44 Response response = new Response(); | |
1607 | 45 response.headers.put( "Content-Type", "text/html; charset=utf-8" ); |
1168 | 46 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) ); |
1602
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
47 writer.write( "<!doctype html>\n<html>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
48 writer.write( "\t<head>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
49 writer.write( "\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
50 writer.write( "\t\t<style>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
51 writer.write( "\t\t\ttd {padding: 2px 8px}\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
52 writer.write( "\t\t\ta {text-decoration: none}\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
53 writer.write( "\t\t\ta:hover {text-decoration: underline}\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
54 writer.write( "\t\t</style>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
55 writer.write( "\t</head>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
56 writer.write( "\t<body>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
57 writer.write( "\t\t<h1>Directory: "+path+"</h1>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
58 writer.write( "\t\t<table border=0>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
59 if( !path.equals("/") ) { |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
60 writer.write( "\t\t\t<tr>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
61 writer.write( "\t\t\t\t<td><a href='..'>../</a></td>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
62 writer.write( "\t\t\t</tr>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
63 } |
1273 | 64 File[] a = file.listFiles(); |
65 Arrays.sort(a,sorter); | |
66 for( File child : a ) { | |
1168 | 67 String name = child.getName(); |
1602
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
68 boolean isDir = child.isDirectory(); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
69 if( isDir ) |
1168 | 70 name += '/'; |
1602
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
71 writer.write( "\t\t\t<tr>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
72 writer.write( "\t\t\t\t<td><a href='"+name+"'>"+name+"</a></td>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
73 writer.write( "\t\t\t\t<td>"+fmt.format(new Date(child.lastModified()))+"</td>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
74 if( !isDir ) |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
75 writer.write( "\t\t\t\t<td align=right>"+child.length()+" bytes</td>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
76 writer.write( "\t\t\t</tr>\n" ); |
1168 | 77 } |
1602
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
78 writer.write( "\t\t</table>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
79 writer.write( "\t</body>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
80 writer.write( "</html>\n" ); |
1168 | 81 writer.close(); |
82 return response; | |
1139
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
83 } |
1168 | 84 return null; |
85 } catch(IOException e) { | |
86 throw new RuntimeException(e); | |
1139
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
87 } |
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
88 } |
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
89 } |