diff src/goodjava/webserver/handlers/DirHandler.java @ 1402:27efb1fcbcb5

move luan.lib to goodjava
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 17 Sep 2019 01:35:01 -0400
parents src/luan/lib/webserver/handlers/DirHandler.java@643cf1c37723
children 55d7b60c074d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/webserver/handlers/DirHandler.java	Tue Sep 17 01:35:01 2019 -0400
@@ -0,0 +1,68 @@
+package goodjava.webserver.handlers;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.Writer;
+import java.io.OutputStreamWriter;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import goodjava.webserver.Handler;
+import goodjava.webserver.Request;
+import goodjava.webserver.Response;
+import goodjava.webserver.ResponseOutputStream;
+
+
+public final class DirHandler implements Handler {
+	private final FileHandler fileHandler;
+
+	public DirHandler(FileHandler fileHandler) {
+		this.fileHandler = fileHandler;
+	}
+
+	private static final Comparator<File> sorter = new Comparator<File>() {
+		public int compare(File f1, File f2) {
+			return f1.getName().compareTo(f2.getName());
+		}
+	};
+
+	public Response handle(Request request) {
+		try {
+			File file = fileHandler.file(request);
+			if( request.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>" );
+				File[] a = file.listFiles();
+				Arrays.sort(a,sorter);
+				for( File child : a ) {
+					String name = child.getName();
+					if( child.isDirectory() )
+						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( "</table>" );
+				writer.write( "</body>" );
+				writer.write( "</html>" );
+				writer.close();
+				return response;
+			}
+			return null;
+		} catch(IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+}