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