comparison src/goodjava/webserver/handlers/DirHandler.java @ 1602:55d7b60c074d

add DirHandler to WebHandler
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 09 Apr 2021 23:46:12 -0600
parents 27efb1fcbcb5
children fa066aaa068c
comparison
equal deleted inserted replaced
1601:de5a81327d9c 1602:55d7b60c074d
23 this.fileHandler = fileHandler; 23 this.fileHandler = fileHandler;
24 } 24 }
25 25
26 private static final Comparator<File> sorter = new Comparator<File>() { 26 private static final Comparator<File> sorter = new Comparator<File>() {
27 public int compare(File f1, File f2) { 27 public int compare(File f1, File f2) {
28 boolean f1IsDir = f1.isDirectory();
29 boolean f2IsDir = f2.isDirectory();
30 if( f1IsDir && !f2IsDir )
31 return -1;
32 if( !f1IsDir && f2IsDir )
33 return 1;
28 return f1.getName().compareTo(f2.getName()); 34 return f1.getName().compareTo(f2.getName());
29 } 35 }
30 }; 36 };
31 37
32 public Response handle(Request request) { 38 public Response handle(Request request) {
33 try { 39 try {
34 File file = fileHandler.file(request); 40 File file = fileHandler.file(request);
35 if( request.path.endsWith("/") && file.isDirectory() ) { 41 String path = request.path;
42 if( path.endsWith("/") && file.isDirectory() ) {
36 DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz"); 43 DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
37 Response response = new Response(); 44 Response response = new Response();
38 response.headers.put( "content-type", "text/html; charset=utf-8" ); 45 response.headers.put( "content-type", "text/html; charset=utf-8" );
39 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) ); 46 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
40 writer.write( "<!doctype html><html>" ); 47 writer.write( "<!doctype html>\n<html>\n" );
41 writer.write( "<head><style>td{padding: 2px 8px}</style></head>" ); 48 writer.write( "\t<head>\n" );
42 writer.write( "<body>" ); 49 writer.write( "\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" );
43 writer.write( "<h1>Directory: "+request.path+"</h1>" ); 50 writer.write( "\t\t<style>\n" );
44 writer.write( "<table border=0>" ); 51 writer.write( "\t\t\ttd {padding: 2px 8px}\n" );
52 writer.write( "\t\t\ta {text-decoration: none}\n" );
53 writer.write( "\t\t\ta:hover {text-decoration: underline}\n" );
54 writer.write( "\t\t</style>\n" );
55 writer.write( "\t</head>\n" );
56 writer.write( "\t<body>\n" );
57 writer.write( "\t\t<h1>Directory: "+path+"</h1>\n" );
58 writer.write( "\t\t<table border=0>\n" );
59 if( !path.equals("/") ) {
60 writer.write( "\t\t\t<tr>\n" );
61 writer.write( "\t\t\t\t<td><a href='..'>../</a></td>\n" );
62 writer.write( "\t\t\t</tr>\n" );
63 }
45 File[] a = file.listFiles(); 64 File[] a = file.listFiles();
46 Arrays.sort(a,sorter); 65 Arrays.sort(a,sorter);
47 for( File child : a ) { 66 for( File child : a ) {
48 String name = child.getName(); 67 String name = child.getName();
49 if( child.isDirectory() ) 68 boolean isDir = child.isDirectory();
69 if( isDir )
50 name += '/'; 70 name += '/';
51 writer.write( "<tr>" ); 71 writer.write( "\t\t\t<tr>\n" );
52 writer.write( "<td><a href='"+name+"'>"+name+"</a></td>" ); 72 writer.write( "\t\t\t\t<td><a href='"+name+"'>"+name+"</a></td>\n" );
53 writer.write( "<td>"+child.length()+" bytes</td>" ); 73 writer.write( "\t\t\t\t<td>"+fmt.format(new Date(child.lastModified()))+"</td>\n" );
54 writer.write( "<td>"+fmt.format(new Date(child.lastModified()))+"</td>" ); 74 if( !isDir )
55 writer.write( "</tr>" ); 75 writer.write( "\t\t\t\t<td align=right>"+child.length()+" bytes</td>\n" );
76 writer.write( "\t\t\t</tr>\n" );
56 } 77 }
57 writer.write( "</table>" ); 78 writer.write( "\t\t</table>\n" );
58 writer.write( "</body>" ); 79 writer.write( "\t</body>\n" );
59 writer.write( "</html>" ); 80 writer.write( "</html>\n" );
60 writer.close(); 81 writer.close();
61 return response; 82 return response;
62 } 83 }
63 return null; 84 return null;
64 } catch(IOException e) { 85 } catch(IOException e) {