annotate src/goodjava/webserver/handlers/DirHandler.java @ 1601:de5a81327d9c

fix deadlock
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 09 Apr 2021 22:39:03 -0600
parents 27efb1fcbcb5
children 55d7b60c074d
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) {
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
28 return f1.getName().compareTo(f2.getName());
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
29 }
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
30 };
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
31
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
32 public Response handle(Request request) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
33 try {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
34 File file = fileHandler.file(request);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
35 if( request.path.endsWith("/") && file.isDirectory() ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
36 DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
37 Response response = new Response();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
38 response.headers.put( "content-type", "text/html; charset=utf-8" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
39 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
40 writer.write( "<!doctype html><html>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
41 writer.write( "<head><style>td{padding: 2px 8px}</style></head>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
42 writer.write( "<body>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
43 writer.write( "<h1>Directory: "+request.path+"</h1>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
44 writer.write( "<table border=0>" );
1273
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
45 File[] a = file.listFiles();
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
46 Arrays.sort(a,sorter);
ed6e8bd78c11 sort in DirHandler
Franklin Schmidt <fschmidt@gmail.com>
parents: 1168
diff changeset
47 for( File child : a ) {
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
48 String name = child.getName();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
49 if( child.isDirectory() )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
50 name += '/';
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
51 writer.write( "<tr>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
52 writer.write( "<td><a href='"+name+"'>"+name+"</a></td>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
53 writer.write( "<td>"+child.length()+" bytes</td>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
54 writer.write( "<td>"+fmt.format(new Date(child.lastModified()))+"</td>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
55 writer.write( "</tr>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
56 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
57 writer.write( "</table>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
58 writer.write( "</body>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
59 writer.write( "</html>" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
60 writer.close();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
61 return response;
1139
8126370ea8c0 webserver - add FileHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 }
1168
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
63 return null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
64 } catch(IOException e) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
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 }