View Javadoc
1 /* Reattore HTTP Server 2 3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 $Id: AutoIndexInterceptor.java,v 1.3 2003/03/05 04:31:57 michaelh Exp $ 20 */ 21 22 package juju.reattore.server.intercept.impl; 23 24 import java.io.File; 25 import java.text.DateFormat; 26 import java.text.SimpleDateFormat; 27 import java.util.Date; 28 29 import juju.reattore.protocol.http.*; 30 import juju.reattore.server.intercept.Interceptor; 31 32 import org.apache.velocity.VelocityContext; 33 34 /*** An interceptor that will generate a pretty index of the requested 35 directory. 36 37 @tag autoindex 38 @group Interceptor 39 */ 40 public class AutoIndexInterceptor 41 extends VelocityInterceptor { 42 43 private File baseDir; 44 45 private String timestampFormat = "dd-MMM-yyyy HH:mm z"; 46 47 /*** Passed to the template to be used for various formatting 48 tasks. 49 */ 50 public class Helper { 51 private SimpleDateFormat ts = new SimpleDateFormat(timestampFormat); 52 53 /*** Formats the timestamp to the style set through 54 #setTimestampFormat. 55 56 @param stamp The timestamp in milliseconds. 57 @return The formatted timestamp. 58 */ 59 public String formatTimestamp(long stamp) { 60 return ts.format(new Date(stamp)); 61 } 62 } 63 64 /*** Sets the SimpleDateFormat format used when displaying the 65 directory. 66 67 @param fmt The format to use. 68 */ 69 public void setTimestampFormat(String fmt) { 70 this.timestampFormat = fmt; 71 } 72 73 /*** Set the base directory. 74 75 @param baseDir Base directory for all requests. 76 */ 77 public void setBaseDir(String baseDir) { 78 this.baseDir = new File(baseDir); 79 } 80 81 /*** @see VelocityInterceptor */ 82 protected boolean populateContext(VelocityContext ctx, HttpRequest req, HttpResponse resp) { 83 String path = req.getPath(); 84 85 File serve = new File(baseDir, path); 86 87 if (serve.isDirectory()) { 88 ctx.put("entries", serve.listFiles()); 89 ctx.put("helper", new Helper()); 90 91 return false; 92 } 93 else { 94 /* Didn't find it */ 95 resp.setStatus(HttpResponse.SC_NOT_FOUND); 96 return true; 97 } 98 99 } 100 }

This page was automatically generated by Maven