diff 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
line wrap: on
line diff
--- a/src/goodjava/webserver/handlers/DirHandler.java	Fri Apr 09 22:39:03 2021 -0600
+++ b/src/goodjava/webserver/handlers/DirHandler.java	Fri Apr 09 23:46:12 2021 -0600
@@ -25,6 +25,12 @@
 
 	private static final Comparator<File> sorter = new Comparator<File>() {
 		public int compare(File f1, File f2) {
+			boolean f1IsDir = f1.isDirectory();
+			boolean f2IsDir = f2.isDirectory();
+			if( f1IsDir && !f2IsDir )
+				return -1;
+			if( !f1IsDir && f2IsDir )
+				return 1;
 			return f1.getName().compareTo(f2.getName());
 		}
 	};
@@ -32,31 +38,46 @@
 	public Response handle(Request request) {
 		try {
 			File file = fileHandler.file(request);
-			if( request.path.endsWith("/") && file.isDirectory() ) {
+			String path = request.path;
+			if( path.endsWith("/") && file.isDirectory() ) {
 				DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
 				Response response = new Response();
 				response.headers.put( "content-type", "text/html; charset=utf-8" );
 				Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
-				writer.write( "<!doctype html><html>" );
-				writer.write( "<head><style>td{padding: 2px 8px}</style></head>" );
-				writer.write( "<body>" );
-				writer.write( "<h1>Directory: "+request.path+"</h1>" );
-				writer.write( "<table border=0>" );
+				writer.write( "<!doctype html>\n<html>\n" );
+				writer.write( "\t<head>\n" );
+				writer.write( "\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" );
+				writer.write( "\t\t<style>\n" );
+				writer.write( "\t\t\ttd {padding: 2px 8px}\n" );
+				writer.write( "\t\t\ta {text-decoration: none}\n" );
+				writer.write( "\t\t\ta:hover {text-decoration: underline}\n" );
+				writer.write( "\t\t</style>\n" );
+				writer.write( "\t</head>\n" );
+				writer.write( "\t<body>\n" );
+				writer.write( "\t\t<h1>Directory: "+path+"</h1>\n" );
+				writer.write( "\t\t<table border=0>\n" );
+				if( !path.equals("/") ) {
+					writer.write( "\t\t\t<tr>\n" );
+					writer.write( "\t\t\t\t<td><a href='..'>../</a></td>\n" );
+					writer.write( "\t\t\t</tr>\n" );
+				}
 				File[] a = file.listFiles();
 				Arrays.sort(a,sorter);
 				for( File child : a ) {
 					String name = child.getName();
-					if( child.isDirectory() )
+					boolean isDir = child.isDirectory();
+					if( isDir )
 						name += '/';
-					writer.write( "<tr>" );
-					writer.write( "<td><a href='"+name+"'>"+name+"</a></td>" );
-					writer.write( "<td>"+child.length()+" bytes</td>" );
-					writer.write( "<td>"+fmt.format(new Date(child.lastModified()))+"</td>" );
-					writer.write( "</tr>" );
+					writer.write( "\t\t\t<tr>\n" );
+					writer.write( "\t\t\t\t<td><a href='"+name+"'>"+name+"</a></td>\n" );
+					writer.write( "\t\t\t\t<td>"+fmt.format(new Date(child.lastModified()))+"</td>\n" );
+					if( !isDir )
+						writer.write( "\t\t\t\t<td align=right>"+child.length()+" bytes</td>\n" );
+					writer.write( "\t\t\t</tr>\n" );
 				}
-				writer.write( "</table>" );
-				writer.write( "</body>" );
-				writer.write( "</html>" );
+				writer.write( "\t\t</table>\n" );
+				writer.write( "\t</body>\n" );
+				writer.write( "</html>\n" );
 				writer.close();
 				return response;
 			}