Various fixes + Upgrades to the FileEdit module:
[mir.git] / source / mircoders / servlet / ServletModuleFileEdit.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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
30  */\r
31 \r
32 package mircoders.servlet;\r
33 \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
46 \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
54 \r
55 /*\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
59  *\r
60  * @author $Author: zapata $\r
61  * @version $Revision: 1.8 $ $Date: 2003/03/17 20:47:04 $\r
62  *\r
63  */\r
64 \r
65 public class ServletModuleFileEdit extends ServletModule\r
66 {\r
67   private static ServletModuleFileEdit instance = new ServletModuleFileEdit();\r
68   public static ServletModule getInstance() { return instance; }\r
69 \r
70   private File rootDirectory;\r
71   private FilenameFilter filter;\r
72   private FilenameFilter dirFilter;\r
73   private boolean recurse;\r
74 \r
75   private ServletModuleFileEdit() {\r
76     super();\r
77 \r
78     logger = new LoggerWrapper("ServletModule.FileEdit");\r
79 \r
80     rootDirectory = new File(configuration.getString("ServletModule.FileEdit.FileDirectory"));\r
81     recurse = configuration.getString("ServletModule.FileEdit.Recursive", "").equals("1");\r
82 \r
83     filter = new FileFunctions.RegExpFileFilter(configuration.getString("ServletModule.FileEdit.ExtFilter"));\r
84     dirFilter = new FileFunctions.DirectoryFilter();\r
85 \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
89   }\r
90 \r
91   public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc\r
92   {\r
93     listSubDirectory("/", aRequest, aResponse);\r
94   }\r
95 \r
96   public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc\r
97   {\r
98     try {\r
99       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);\r
100       String filename = requestParser.getParameter("filename");\r
101       String subDirectory = requestParser.getParameterWithDefault("subdirectory", "");\r
102 \r
103       if (filename == null)\r
104         throw new ServletModuleExc("No filename  specified");\r
105 \r
106       editFile(filename, subDirectory, aRequest, aResponse);\r
107     }\r
108     catch (Throwable e) {\r
109       throw new ServletModuleFailure(e);\r
110     }\r
111   }\r
112 \r
113   public void enter(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc\r
114   {\r
115     try {\r
116       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);\r
117       String directoryName = requestParser.getParameter("directory");\r
118       String subDirectoryName = requestParser.getParameter("subdirectory");\r
119 \r
120       if (directoryName==null | subDirectoryName==null)\r
121         throw new ServletModuleExc("No directory/subDirectory specified");\r
122 \r
123       listSubDirectory(subDirectoryName+File.separator+directoryName, aRequest, aResponse);\r
124     }\r
125     catch (Throwable e) {\r
126       throw new ServletModuleFailure(e);\r
127     }\r
128   }\r
129   public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc\r
130   {\r
131     String filename = aRequest.getParameter("filename");\r
132     String subDirectory = aRequest.getParameter("subdirectory");\r
133     String text = aRequest.getParameter("text");\r
134 \r
135     try {\r
136       File f = new File(new File(rootDirectory, subDirectory), filename);\r
137 \r
138       if (validateDirectory(f)) {\r
139         StringReader in = new StringReader(text);\r
140         FileWriter out = new FileWriter(f);\r
141 \r
142         int c;\r
143         while ( (c = in.read()) != -1)\r
144           out.write(c);\r
145         in.close();\r
146         out.close();\r
147 \r
148         editFile(filename, subDirectory, aRequest, aResponse);\r
149       }\r
150     }\r
151     catch (Throwable e) {\r
152       throw new ServletModuleFailure(e);\r
153     }\r
154   }\r
155 \r
156   public void listSubDirectory(String aSubDirectory, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc\r
157   {\r
158     try {\r
159       Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));\r
160       File dir = new File(rootDirectory, aSubDirectory);\r
161 \r
162       if (!validateDirectory(dir) || !dir.isDirectory()) {\r
163         dir = rootDirectory;\r
164         aSubDirectory = "";\r
165       }\r
166 \r
167       responseData.put("filelist", Arrays.asList(dir.list(filter)));\r
168 \r
169       if (recurse) {\r
170         List dirs = new Vector();\r
171         if (!dir.getCanonicalPath().equals(rootDirectory.getCanonicalPath()))\r
172           responseData.put("updir", new File(aSubDirectory).getParent());\r
173 \r
174         dirs.addAll(Arrays.asList(dir.list(dirFilter)));\r
175 \r
176         responseData.put("dirlist", dirs);\r
177       }\r
178       else {\r
179         responseData.put("dirlist", null);\r
180         responseData.put("updir", null);\r
181       }\r
182 \r
183       responseData.put("subdirectory", aSubDirectory);\r
184 \r
185       ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateListString);\r
186     }\r
187     catch (Throwable e) {\r
188       throw new ServletModuleFailure(e);\r
189     }\r
190   }\r
191 \r
192   public void editFile(String aFileName, String aSubDirectory, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc\r
193   {\r
194     try {\r
195       File f = new File(new File(rootDirectory, aSubDirectory), aFileName);\r
196 \r
197       if (!validateDirectory(f) || f.isDirectory() || !validateFile(f)) {\r
198         listSubDirectory("", aRequest, aResponse);\r
199       }\r
200       else {\r
201         Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));\r
202         URLBuilder urlBuilder = new URLBuilder();\r
203 \r
204         urlBuilder.setValue("module", "FileEdit");\r
205         urlBuilder.setValue("do", "enter");\r
206         urlBuilder.setValue("directory", "");\r
207         urlBuilder.setValue("subdirectory", aSubDirectory);\r
208 \r
209         FileReader in = new FileReader(f);\r
210         StringWriter out = new StringWriter();\r
211 \r
212         int c;\r
213         while ( (c = in.read()) != -1)\r
214           out.write(c);\r
215         in.close();\r
216         out.close();\r
217 \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
222 \r
223         ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);\r
224       }\r
225     }\r
226     catch (Throwable e) {\r
227       throw new ServletModuleFailure(e);\r
228     }\r
229   }\r
230 \r
231   protected boolean validateDirectory(File aFile) {\r
232     try {\r
233       return (aFile.getCanonicalPath().startsWith(rootDirectory.getCanonicalPath()));\r
234     }\r
235     catch (Throwable t) {\r
236       return false;\r
237     }\r
238   }\r
239 \r
240   protected boolean validateFile(File aFile) {\r
241     try {\r
242       return filter.accept(aFile.getParentFile(), aFile.getName());\r
243     }\r
244     catch (Throwable t) {\r
245       return false;\r
246     }\r
247   }\r
248 }\r