2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mircoders.servlet;
35 import java.io.FileReader;
36 import java.io.FileWriter;
37 import java.io.FilenameFilter;
38 import java.io.StringReader;
39 import java.io.StringWriter;
40 import java.util.Arrays;
41 import java.util.List;
43 import java.util.Vector;
44 import java.util.Locale;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import mir.log.LoggerWrapper;
49 import mir.servlet.ServletModule;
50 import mir.servlet.ServletModuleExc;
51 import mir.servlet.ServletModuleFailure;
52 import mir.util.FileFunctions;
53 import mir.util.HTTPRequestParser;
54 import mir.util.URLBuilder;
57 * ServletModuleFileEdit -
58 * Allows one to do a basic edit of a file in a directory specified
61 * @author $Author: zapata $
62 * @version $Revision: 1.9 $ $Date: 2003/04/09 02:06:09 $
66 public class ServletModuleFileEdit extends ServletModule
68 private static ServletModuleFileEdit instance = new ServletModuleFileEdit();
69 public static ServletModule getInstance() { return instance; }
71 private File rootDirectory;
72 private FilenameFilter filter;
73 private FilenameFilter dirFilter;
74 private boolean recurse;
76 private ServletModuleFileEdit() {
79 logger = new LoggerWrapper("ServletModule.FileEdit");
81 rootDirectory = new File(configuration.getString("ServletModule.FileEdit.FileDirectory"));
82 recurse = configuration.getString("ServletModule.FileEdit.Recursive", "").equals("1");
84 filter = new FileFunctions.RegExpFileFilter(configuration.getString("ServletModule.FileEdit.ExtFilter"));
85 dirFilter = new FileFunctions.DirectoryFilter();
87 templateListString =configuration.getString("ServletModule.FileEdit.ListTemplate");
88 templateObjektString =configuration.getString("ServletModule.FileEdit.ObjektTemplate");
89 templateConfirmString =configuration.getString("ServletModule.FileEdit.ConfirmTemplate");
92 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
94 listSubDirectory("/", aRequest, aResponse);
97 public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
100 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
101 String filename = requestParser.getParameter("filename");
102 String subDirectory = requestParser.getParameterWithDefault("subdirectory", "");
104 if (filename == null)
105 throw new ServletModuleExc("No filename specified");
107 editFile(filename, subDirectory, aRequest, aResponse);
109 catch (Throwable e) {
110 throw new ServletModuleFailure(e);
114 public void enter(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
117 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
118 String directoryName = requestParser.getParameter("directory");
119 String subDirectoryName = requestParser.getParameter("subdirectory");
121 if (directoryName==null | subDirectoryName==null)
122 throw new ServletModuleExc("No directory/subDirectory specified");
124 listSubDirectory(subDirectoryName+File.separator+directoryName, aRequest, aResponse);
126 catch (Throwable e) {
127 throw new ServletModuleFailure(e);
130 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
132 String filename = aRequest.getParameter("filename");
133 String subDirectory = aRequest.getParameter("subdirectory");
134 String text = aRequest.getParameter("text");
137 File f = new File(new File(rootDirectory, subDirectory), filename);
139 if (validateDirectory(f)) {
140 StringReader in = new StringReader(text);
141 FileWriter out = new FileWriter(f);
144 while ( (c = in.read()) != -1)
149 editFile(filename, subDirectory, aRequest, aResponse);
152 catch (Throwable e) {
153 throw new ServletModuleFailure(e);
157 public void listSubDirectory(String aSubDirectory, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
160 Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
161 File dir = new File(rootDirectory, aSubDirectory);
163 if (!validateDirectory(dir) || !dir.isDirectory()) {
168 responseData.put("filelist", Arrays.asList(dir.list(filter)));
171 List dirs = new Vector();
172 if (!dir.getCanonicalPath().equals(rootDirectory.getCanonicalPath()))
173 responseData.put("updir", new File(aSubDirectory).getParent());
175 dirs.addAll(Arrays.asList(dir.list(dirFilter)));
177 responseData.put("dirlist", dirs);
180 responseData.put("dirlist", null);
181 responseData.put("updir", null);
184 responseData.put("subdirectory", aSubDirectory);
186 ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateListString);
188 catch (Throwable e) {
189 throw new ServletModuleFailure(e);
193 public void editFile(String aFileName, String aSubDirectory, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
196 File f = new File(new File(rootDirectory, aSubDirectory), aFileName);
198 if (!validateDirectory(f) || f.isDirectory() || !validateFile(f)) {
199 listSubDirectory("", aRequest, aResponse);
202 Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
203 URLBuilder urlBuilder = new URLBuilder();
205 urlBuilder.setValue("module", "FileEdit");
206 urlBuilder.setValue("do", "enter");
207 urlBuilder.setValue("directory", "");
208 urlBuilder.setValue("subdirectory", aSubDirectory);
210 FileReader in = new FileReader(f);
211 StringWriter out = new StringWriter();
214 while ( (c = in.read()) != -1)
219 responseData.put("text", out.toString());
220 responseData.put("filename", aFileName);
221 responseData.put("subdirectory", aSubDirectory);
222 responseData.put("returnurl", urlBuilder.getQuery());
224 ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);
227 catch (Throwable e) {
228 throw new ServletModuleFailure(e);
232 protected boolean validateDirectory(File aFile) {
234 return (aFile.getCanonicalPath().startsWith(rootDirectory.getCanonicalPath()));
236 catch (Throwable t) {
241 protected boolean validateFile(File aFile) {
243 return filter.accept(aFile.getParentFile(), aFile.getName());
245 catch (Throwable t) {