Mercurial Hosting > luan
annotate src/goodjava/webserver/handlers/DirHandler.java @ 1703:3a61451f8130
add bbcode video
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 Jul 2022 00:12:05 -0600 |
parents | d88fb2eb15aa |
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.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" ); |
1617 | 50 writer.write( "\t\t<meta name=\"robots\" content=\"noindex,nofollow\">\n" ); |
1602
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
51 writer.write( "\t\t<style>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
52 writer.write( "\t\t\ttd {padding: 2px 8px}\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
53 writer.write( "\t\t\ta {text-decoration: none}\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
54 writer.write( "\t\t\ta:hover {text-decoration: underline}\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
55 writer.write( "\t\t</style>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
56 writer.write( "\t</head>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
57 writer.write( "\t<body>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
58 writer.write( "\t\t<h1>Directory: "+path+"</h1>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
59 writer.write( "\t\t<table border=0>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
60 if( !path.equals("/") ) { |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
61 writer.write( "\t\t\t<tr>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
62 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
|
63 writer.write( "\t\t\t</tr>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
64 } |
1273 | 65 File[] a = file.listFiles(); |
66 Arrays.sort(a,sorter); | |
67 for( File child : a ) { | |
1168 | 68 String name = child.getName(); |
1602
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
69 boolean isDir = child.isDirectory(); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
70 if( isDir ) |
1168 | 71 name += '/'; |
1602
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
72 writer.write( "\t\t\t<tr>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
73 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
|
74 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
|
75 if( !isDir ) |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
76 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
|
77 writer.write( "\t\t\t</tr>\n" ); |
1168 | 78 } |
1602
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
79 writer.write( "\t\t</table>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
80 writer.write( "\t</body>\n" ); |
55d7b60c074d
add DirHandler to WebHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
81 writer.write( "</html>\n" ); |
1168 | 82 writer.close(); |
83 return response; | |
1139
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
84 } |
1168 | 85 return null; |
86 } catch(IOException e) { | |
87 throw new RuntimeException(e); | |
1139
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 } |
8126370ea8c0
webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
90 } |