annotate src/goodjava/webserver/handlers/DirHandler.java @ 1640:570f3d483a31

host - move https.luan into src
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 21 Dec 2021 16:54:26 -0700
parents d88fb2eb15aa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
10 import java.util.Arrays;
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
19 public final class DirHandler implements Handler {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
20 private final FileHandler fileHandler;
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
22 public DirHandler(FileHandler fileHandler) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
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
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
26 private static final Comparator<File> sorter = new Comparator<File>() {
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
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
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
34 return f1.getName().compareTo(f2.getName());
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
35 }
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
36 };
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
37
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
38 public Response handle(Request request) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
39 try {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
43 DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
44 Response response = new Response();
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1602
diff changeset
45 response.headers.put( "Content-Type", "text/html; charset=utf-8" );
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1607
diff changeset
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
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
65 File[] a = file.listFiles();
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
66 Arrays.sort(a,sorter);
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
67 for( File child : a ) {
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
82 writer.close();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
83 return response;
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 }
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
85 return null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
86 } catch(IOException e) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
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 }