changeset 1602:55d7b60c074d

add DirHandler to WebHandler
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 09 Apr 2021 23:46:12 -0600
parents de5a81327d9c
children bd125ee375d2
files src/goodjava/webserver/handlers/DirHandler.java src/luan/host/WebHandler.java
diffstat 2 files changed, 41 insertions(+), 16 deletions(-) [+]
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;
 			}
--- a/src/luan/host/WebHandler.java	Fri Apr 09 22:39:03 2021 -0600
+++ b/src/luan/host/WebHandler.java	Fri Apr 09 23:46:12 2021 -0600
@@ -14,6 +14,8 @@
 import goodjava.webserver.handlers.ContentTypeHandler;
 import goodjava.webserver.handlers.SafeHandler;
 import goodjava.webserver.handlers.LogHandler;
+import goodjava.webserver.handlers.FileHandler;
+import goodjava.webserver.handlers.DirHandler;
 import luan.Luan;
 import luan.LuanException;
 import luan.LuanTable;
@@ -63,7 +65,9 @@
 
 			Handler notFoundHander = new NotFound(luanHandler);
 			Handler handler = new IndexHandler(luanHandler);
-			handler = new ListHandler( handler, notFoundHander );
+			FileHandler fileHandler = new FileHandler(dirStr+"/site/");
+			DirHandler dirHandler = new DirHandler(fileHandler);
+			handler = new ListHandler( handler, dirHandler, notFoundHander );
 			handler = new ContentTypeHandler(handler);
 			handler = new SafeHandler(handler);
 			handler = new LogHandler(handler,LogHandler.dirLogger(new File(logDir),days30));