2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
32 package mircoders.servlet;
\r
34 import java.io.File;
\r
35 import java.io.FileReader;
\r
36 import java.io.FileWriter;
\r
37 import java.io.FilenameFilter;
\r
38 import java.io.StringReader;
\r
39 import java.io.StringWriter;
\r
40 import java.util.Arrays;
\r
41 import java.util.List;
\r
42 import java.util.Map;
\r
43 import java.util.Vector;
\r
44 import javax.servlet.http.HttpServletRequest;
\r
45 import javax.servlet.http.HttpServletResponse;
\r
47 import mir.log.LoggerWrapper;
\r
48 import mir.servlet.ServletModule;
\r
49 import mir.servlet.ServletModuleExc;
\r
50 import mir.servlet.ServletModuleFailure;
\r
51 import mir.util.FileFunctions;
\r
52 import mir.util.HTTPRequestParser;
\r
53 import mir.util.URLBuilder;
\r
56 * ServletModuleFileEdit -
\r
57 * Allows one to do a basic edit of a file in a directory specified
\r
58 * in the config file.
\r
60 * @author $Author: zapata $
\r
61 * @version $Revision: 1.8 $ $Date: 2003/03/17 20:47:04 $
\r
65 public class ServletModuleFileEdit extends ServletModule
\r
67 private static ServletModuleFileEdit instance = new ServletModuleFileEdit();
\r
68 public static ServletModule getInstance() { return instance; }
\r
70 private File rootDirectory;
\r
71 private FilenameFilter filter;
\r
72 private FilenameFilter dirFilter;
\r
73 private boolean recurse;
\r
75 private ServletModuleFileEdit() {
\r
78 logger = new LoggerWrapper("ServletModule.FileEdit");
\r
80 rootDirectory = new File(configuration.getString("ServletModule.FileEdit.FileDirectory"));
\r
81 recurse = configuration.getString("ServletModule.FileEdit.Recursive", "").equals("1");
\r
83 filter = new FileFunctions.RegExpFileFilter(configuration.getString("ServletModule.FileEdit.ExtFilter"));
\r
84 dirFilter = new FileFunctions.DirectoryFilter();
\r
86 templateListString =configuration.getString("ServletModule.FileEdit.ListTemplate");
\r
87 templateObjektString =configuration.getString("ServletModule.FileEdit.ObjektTemplate");
\r
88 templateConfirmString =configuration.getString("ServletModule.FileEdit.ConfirmTemplate");
\r
91 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
93 listSubDirectory("/", aRequest, aResponse);
\r
96 public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
99 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
100 String filename = requestParser.getParameter("filename");
\r
101 String subDirectory = requestParser.getParameterWithDefault("subdirectory", "");
\r
103 if (filename == null)
\r
104 throw new ServletModuleExc("No filename specified");
\r
106 editFile(filename, subDirectory, aRequest, aResponse);
\r
108 catch (Throwable e) {
\r
109 throw new ServletModuleFailure(e);
\r
113 public void enter(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
116 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
117 String directoryName = requestParser.getParameter("directory");
\r
118 String subDirectoryName = requestParser.getParameter("subdirectory");
\r
120 if (directoryName==null | subDirectoryName==null)
\r
121 throw new ServletModuleExc("No directory/subDirectory specified");
\r
123 listSubDirectory(subDirectoryName+File.separator+directoryName, aRequest, aResponse);
\r
125 catch (Throwable e) {
\r
126 throw new ServletModuleFailure(e);
\r
129 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
131 String filename = aRequest.getParameter("filename");
\r
132 String subDirectory = aRequest.getParameter("subdirectory");
\r
133 String text = aRequest.getParameter("text");
\r
136 File f = new File(new File(rootDirectory, subDirectory), filename);
\r
138 if (validateDirectory(f)) {
\r
139 StringReader in = new StringReader(text);
\r
140 FileWriter out = new FileWriter(f);
\r
143 while ( (c = in.read()) != -1)
\r
148 editFile(filename, subDirectory, aRequest, aResponse);
\r
151 catch (Throwable e) {
\r
152 throw new ServletModuleFailure(e);
\r
156 public void listSubDirectory(String aSubDirectory, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
159 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
\r
160 File dir = new File(rootDirectory, aSubDirectory);
\r
162 if (!validateDirectory(dir) || !dir.isDirectory()) {
\r
163 dir = rootDirectory;
\r
164 aSubDirectory = "";
\r
167 responseData.put("filelist", Arrays.asList(dir.list(filter)));
\r
170 List dirs = new Vector();
\r
171 if (!dir.getCanonicalPath().equals(rootDirectory.getCanonicalPath()))
\r
172 responseData.put("updir", new File(aSubDirectory).getParent());
\r
174 dirs.addAll(Arrays.asList(dir.list(dirFilter)));
\r
176 responseData.put("dirlist", dirs);
\r
179 responseData.put("dirlist", null);
\r
180 responseData.put("updir", null);
\r
183 responseData.put("subdirectory", aSubDirectory);
\r
185 ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateListString);
\r
187 catch (Throwable e) {
\r
188 throw new ServletModuleFailure(e);
\r
192 public void editFile(String aFileName, String aSubDirectory, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
195 File f = new File(new File(rootDirectory, aSubDirectory), aFileName);
\r
197 if (!validateDirectory(f) || f.isDirectory() || !validateFile(f)) {
\r
198 listSubDirectory("", aRequest, aResponse);
\r
201 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
\r
202 URLBuilder urlBuilder = new URLBuilder();
\r
204 urlBuilder.setValue("module", "FileEdit");
\r
205 urlBuilder.setValue("do", "enter");
\r
206 urlBuilder.setValue("directory", "");
\r
207 urlBuilder.setValue("subdirectory", aSubDirectory);
\r
209 FileReader in = new FileReader(f);
\r
210 StringWriter out = new StringWriter();
\r
213 while ( (c = in.read()) != -1)
\r
218 responseData.put("text", out.toString());
\r
219 responseData.put("filename", aFileName);
\r
220 responseData.put("subdirectory", aSubDirectory);
\r
221 responseData.put("returnurl", urlBuilder.getQuery());
\r
223 ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);
\r
226 catch (Throwable e) {
\r
227 throw new ServletModuleFailure(e);
\r
231 protected boolean validateDirectory(File aFile) {
\r
233 return (aFile.getCanonicalPath().startsWith(rootDirectory.getCanonicalPath()));
\r
235 catch (Throwable t) {
\r
240 protected boolean validateFile(File aFile) {
\r
242 return filter.accept(aFile.getParentFile(), aFile.getName());
\r
244 catch (Throwable t) {
\r