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: FileInterceptor.java,v 1.18 2003/03/05 04:31:57 michaelh Exp $ 20 */ 21 22 package juju.reattore.server.intercept.impl; 23 24 import java.io.*; 25 import java.util.*; 26 import org.apache.commons.logging.*; 27 28 import juju.reattore.protocol.http.*; 29 import juju.reattore.server.intercept.Interceptor; 30 import juju.reattore.io.impl.ChannelFileSource; 31 import juju.reattore.io.ByteSource; 32 33 /*** Interceptor that serves up files. 34 35 @tag files 36 @group Interceptor 37 @children ExtMap 38 */ 39 public class FileInterceptor 40 implements Interceptor { 41 42 private static Log log = LogFactory.getLog(FileInterceptor.class); 43 44 private File baseDir; 45 46 /* Or is it indicies? :) */ 47 private List indexes = new ArrayList(); 48 49 private Map extmap = new HashMap(); 50 51 /*** Create a new FileInterceptor using the given directory as the 52 base directory. 53 54 @param baseDir Base directory for all requests. 55 */ 56 public FileInterceptor(String baseDir) { 57 this.baseDir = new File(baseDir); 58 } 59 60 /*** Default Bean constructor. 61 */ 62 public FileInterceptor() { 63 } 64 65 /*** Set the base directory. 66 67 @param baseDir Base directory for all requests. 68 */ 69 public void setBaseDir(String baseDir) { 70 this.baseDir = new File(baseDir); 71 } 72 73 /*** Adds a file that the server will try for if a directory is 74 requested. 75 76 @param name The path to try. 77 */ 78 public void addDirectoryIndex(String name) { 79 indexes.add(name); 80 } 81 82 /*** Adds a mapping between extension and mime type. 83 84 @param map The mapping to add/replace. 85 */ 86 public void addExtMap(ExtMap map) { 87 extmap.put(map.getName(), map.getValue()); 88 } 89 90 /*** Sets the file that the server will try for if a directory is 91 requested. Clears the list set by #addDirectoryIndex. 92 93 @param name The path to try. 94 */ 95 public void setDirectoryIndex(String name) { 96 indexes.clear(); 97 indexes.add(name); 98 } 99 100 private String guessContentType(File path) { 101 String name = path.getName(); 102 int idx = name.lastIndexOf('.'); 103 104 if (idx != -1) { 105 String ext = name.substring(idx + 1); 106 String type; 107 108 if ((type = (String)extmap.get(ext)) != null) { 109 return type; 110 } 111 else { 112 return null; 113 } 114 } 115 else { 116 return null; 117 } 118 } 119 120 private boolean tryLoad(HttpRequest req, HttpResponse resp, File path) { 121 log.debug("Opening " + path); 122 123 if (path.exists() == false) { 124 /* Can't find it */ 125 return false; 126 } 127 else if (path.canRead() == false) { 128 resp.setStatus(HttpResponse.SC_FORBIDDEN); 129 return false; 130 } 131 else { 132 try { 133 resp.setBody(new ChannelFileSource(path)); 134 resp.setStatus(HttpResponse.SC_OK); 135 136 String type = guessContentType(path); 137 138 if (type != null) { 139 resp.setHeader(HttpConstants.CONTENT_TYPE, type); 140 } 141 return true; 142 } 143 catch (IOException ex) { 144 log.info("Error while reading " + path, ex); 145 resp.setStatus(HttpResponse.SC_FORBIDDEN); 146 return false; 147 } 148 } 149 } 150 151 /*** @see Interceptor */ 152 public boolean process(HttpRequest req, HttpResponse resp) { 153 String path = req.getPath(); 154 155 ByteSource ret; 156 157 File serve = new File(baseDir, path); 158 159 if (serve.isDirectory()) { 160 /* Redirect if the request is incomplete. */ 161 if (path.endsWith("/") || path.equals("")) { 162 for (Iterator i = indexes.iterator(); i.hasNext();) { 163 if (tryLoad(req, resp, new File(serve, (String)i.next()))) { 164 return true; 165 } 166 } 167 168 /* Directory and didn't find the index */ 169 resp.setStatus(HttpResponse.SC_NOT_FOUND); 170 return false; 171 } 172 else { 173 resp.setStatus(HttpResponse.SC_MOVED_PERMANENTLY); 174 resp.setHeader(HttpConstants.LOCATION, req.getOriginalPath() + "/"); 175 return true; 176 } 177 } 178 else if (tryLoad(req, resp, serve)) { 179 /* Found the actual file */ 180 return true; 181 } 182 else { 183 /* Didn't find it */ 184 resp.setStatus(HttpResponse.SC_NOT_FOUND); 185 return false; 186 } 187 } 188 }

This page was automatically generated by Maven