Mercurial Hosting > luan
view src/goodjava/webserver/handlers/DirHandler.java @ 1762:7d2297155ee3
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 19 May 2023 18:08:46 -0600 |
parents | d88fb2eb15aa |
children |
line wrap: on
line source
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) { boolean f1IsDir = f1.isDirectory(); boolean f2IsDir = f2.isDirectory(); if( f1IsDir && !f2IsDir ) return -1; if( !f1IsDir && f2IsDir ) return 1; return f1.getName().compareTo(f2.getName()); } }; public Response handle(Request request) { try { File file = fileHandler.file(request); 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>\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<meta name=\"robots\" content=\"noindex,nofollow\">\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(); boolean isDir = child.isDirectory(); if( isDir ) name += '/'; 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( "\t\t</table>\n" ); writer.write( "\t</body>\n" ); writer.write( "</html>\n" ); writer.close(); return response; } return null; } catch(IOException e) { throw new RuntimeException(e); } } }