/*
- * Copyright (C) 2001, 2002 The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
*
* This file is part of Mir.
*
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two. You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries. If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so. If you do
- * not wish to do so, delete this exception statement from your version.
+ * the code of this program with any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two. You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries. If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
*/
package mircoders.servlet;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
-import java.io.IOException;
+import java.io.FilenameFilter;
import java.io.StringReader;
import java.io.StringWriter;
-
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mir.log.LoggerWrapper;
-import mir.misc.FileExtFilter;
-import mir.misc.HTMLTemplateProcessor;
import mir.servlet.ServletModule;
-import mir.servlet.ServletModuleException;
-import freemarker.template.SimpleHash;
-import freemarker.template.SimpleList;
+import mir.servlet.ServletModuleExc;
+import mir.servlet.ServletModuleFailure;
+import mir.util.FileFunctions;
+import mir.util.HTTPRequestParser;
+import mir.util.StringRoutines;
+import mir.util.URLBuilder;
/*
* ServletModuleFileEdit -
* Allows one to do a basic edit of a file in a directory specified
* in the config file.
*
- * @author $Author: idfx $
- * @version $Revision: 1.4 $ $Date: 2003/01/25 17:50:36 $
+ * @author $Author: zapata $
+ * @version $Revision: 1.13 $ $Date: 2003/05/08 02:43:42 $
*
*/
public class ServletModuleFileEdit extends ServletModule
{
+ private static ServletModuleFileEdit instance = new ServletModuleFileEdit();
+ public static ServletModule getInstance() { return instance; }
-// Singelton / Kontruktor
+ private Map directories;
+ private List directoryNames;
- private static ServletModuleFileEdit instance =
- new ServletModuleFileEdit();
- public static ServletModule getInstance() { return instance; }
+ private FilenameFilter dirFilter;
+
+ private class FileEditDirectory {
+ private String name;
+ private FileFunctions.RegExpFileFilter filter;
+ private File rootDirectory;
+ private boolean recursive;
+
+ public FileEditDirectory(String aName, String aRootDirectory, String aFilter, boolean aRecursive) {
+ name = aName;
+ rootDirectory = new File(aRootDirectory);
+ filter = new FileFunctions.RegExpFileFilter(aFilter);
+ recursive = aRecursive;
+ }
- private String _dirName;
- private String _extName;
+ public String getName() {
+ return name;
+ }
+
+ public FileFunctions.RegExpFileFilter getFilter() {
+ return filter;
+ }
+
+ public File getRootDirectory() {
+ return rootDirectory;
+ }
+
+ public boolean getRecursive() {
+ return recursive;
+ }
+ }
private ServletModuleFileEdit() {
super();
logger = new LoggerWrapper("ServletModule.FileEdit");
- _dirName = configuration.getString("ServletModule.FileEdit.FileDirectory");
- _extName = configuration.getString("ServletModule.FileEdit.ExtFilter");
+ directories = new HashMap();
+ directoryNames = new Vector();
+
+ String settings[] = configuration.getStringArray("ServletModule.FileEdit.Configuration");
+
+ if (settings!=null) {
+ for (int i = 0; i < settings.length; i++) {
+ String setting = settings[i].trim();
+
+ if (setting.length() > 0) {
+ List parts = StringRoutines.splitString(setting, ":");
+ if (parts.size() != 4) {
+ logger.error("config error: " + settings[i] + ", 4 parts expected");
+ }
+ else {
+ String name = (String) parts.get(0);
+ String directory = (String) parts.get(1);
+ String filter = (String) parts.get(2);
+ String recursive = (String) parts.get(3);
+
+ directories.put(name, new FileEditDirectory(name, directory, filter,
+ recursive.equals("1") || recursive.toLowerCase().equals("y")));
+ directoryNames.add(name);
+ }
+ }
+ }
+ }
+
+ dirFilter = new FileFunctions.DirectoryFilter();
templateListString =configuration.getString("ServletModule.FileEdit.ListTemplate");
templateObjektString =configuration.getString("ServletModule.FileEdit.ObjektTemplate");
templateConfirmString =configuration.getString("ServletModule.FileEdit.ConfirmTemplate");
}
- public void list(HttpServletRequest req, HttpServletResponse res)
- throws ServletModuleException
+ public List getEntries() {
+ return directoryNames;
+ }
+
+ public FileEditDirectory getDirectory(HttpServletRequest aRequest) throws ServletModuleExc {
+ FileEditDirectory result = (FileEditDirectory) directories.get(aRequest.getParameter("entry"));
+ if (result == null)
+ throw new ServletModuleExc("Unknown entry: " + aRequest.getParameter("entry"));
+
+ return result;
+ }
+
+ public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
+ {
+ listSubDirectory(getDirectory(aRequest), "/", aRequest, aResponse);
+ }
+
+ public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
{
-// fetch and deliver
try {
- SimpleHash mergeData = new SimpleHash();
- String offset = req.getParameter("offset");
- if (offset==null || offset.equals("")) offset="0";
- mergeData.put("offset",offset);
- File dir = new File(_dirName);
- System.out.println("DIRNAME: "+_dirName);
- FileExtFilter extFilter = new FileExtFilter(_extName);
- String[] dirEntries = dir.list(extFilter);
- SimpleList theList = new SimpleList();
- for ( int i = 0; i < dirEntries.length; ++i ) {
- System.out.println(" FILE: "+dirEntries[i]);
- theList.add(dirEntries[i]);
- }
- mergeData.put("filelist",theList);
+ HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
+
+ String filename = requestParser.getParameter("filename");
+ String subDirectory = requestParser.getParameterWithDefault("subdirectory", "");
+
+ if (filename == null)
+ throw new ServletModuleExc("No filename specified");
+
+ editFile(getDirectory(aRequest), filename, subDirectory, aRequest, aResponse);
+ }
+ catch (Throwable e) {
+ throw new ServletModuleFailure(e);
+ }
+ }
+
+ public void enter(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
+ {
+ try {
+ HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
+
+ String directoryName = requestParser.getParameter("directory");
+ String subDirectoryName = requestParser.getParameter("subdirectory");
+
+ if (directoryName==null | subDirectoryName==null)
+ throw new ServletModuleExc("No directory/subDirectory specified");
- HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
+ listSubDirectory(getDirectory(aRequest), subDirectoryName+File.separator+directoryName, aRequest, aResponse);
+ }
+ catch (Throwable e) {
+ throw new ServletModuleFailure(e);
}
- catch (IOException e) {throw new ServletModuleException(e.toString());}
- catch (Exception e) {throw new ServletModuleException(e.toString());}
}
- public void edit(HttpServletRequest req, HttpServletResponse res)
- throws ServletModuleException
+ public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
{
- String filename = req.getParameter("filename");
- if (filename == null) throw new ServletModuleException("No filename specified");
+ HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
+ String filename = requestParser.getParameter("filename");
+ String subDirectory = requestParser.getParameter("subdirectory");
+ String text = requestParser.getParameter("text");
+ FileEditDirectory directory = getDirectory(aRequest);
+
try {
+ File f = new File(new File(directory.getRootDirectory(), subDirectory), filename);
+
+ if (validateDirectory(directory, f)) {
+ StringReader in = new StringReader(text);
+ FileWriter out = new FileWriter(f);
+
+ int c;
+ while ( (c = in.read()) != -1)
+ out.write(c);
+ in.close();
+ out.close();
+
+ editFile(directory, filename, subDirectory, aRequest, aResponse);
+ }
+ }
+ catch (Throwable e) {
+ throw new ServletModuleFailure(e);
+ }
+ }
+
+ public void listSubDirectory(FileEditDirectory aDirectory, String aSubDirectory, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
+ {
+ try {
+ Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
+ File dir = new File(aDirectory.getRootDirectory(), aSubDirectory);
+
+ if (!validateDirectory(aDirectory, dir) || !dir.isDirectory()) {
+ dir = aDirectory.getRootDirectory();
+ aSubDirectory = "";
+ }
+
+ responseData.put("filelist", FileFunctions.getDirectoryContentsAsList(dir, aDirectory.getFilter()));
+
+ if (aDirectory.getRecursive()) {
+ List dirs = new Vector();
+ if (!dir.getCanonicalPath().equals(aDirectory.getRootDirectory().getCanonicalPath()))
+ responseData.put("updir", new File(aSubDirectory).getParent());
- File f = new File(_dirName, filename);
- FileReader in = new FileReader(f);
- StringWriter out = new StringWriter();
+ dirs.addAll(FileFunctions.getDirectoryContentsAsList(dir, dirFilter));
- int c;
- while ((c = in.read()) != -1)
- out.write(c);
- in.close();
- out.close();
- SimpleHash withValues = new SimpleHash();
- withValues.put("text", out.toString());
- withValues.put("filename", filename);
+ responseData.put("dirlist", dirs);
+ }
+ else {
+ responseData.put("dirlist", null);
+ responseData.put("updir", null);
+ }
+ responseData.put("subdirectory", aSubDirectory);
+ responseData.put("entry", aDirectory.getName());
- deliver(req, res, withValues, null, templateObjektString);
- } catch (Exception e) {
- throw new ServletModuleException(e.toString());
+ ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateListString);
+ }
+ catch (Throwable e) {
+ throw new ServletModuleFailure(e);
}
}
- public void update(HttpServletRequest req, HttpServletResponse res)
- throws ServletModuleException
+ public void editFile(FileEditDirectory aDirectory, String aFileName, String aSubDirectory, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
{
- String filename = req.getParameter("filename");
- if (filename == null) throw new ServletModuleException("No filename specified");
try {
+ File f = new File(new File(aDirectory.getRootDirectory(), aSubDirectory), aFileName);
+
+ if (!validateDirectory(aDirectory, f) || f.isDirectory() || !validateFile(aDirectory, f)) {
+ listSubDirectory(aDirectory, "", aRequest, aResponse);
+ }
+ else {
+ Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
+ URLBuilder urlBuilder = new URLBuilder();
- File f = new File(_dirName, filename);
- StringReader in = new StringReader(req.getParameter("text"));
- FileWriter out = new FileWriter(f);
+ urlBuilder.setValue("module", "FileEdit");
+ urlBuilder.setValue("do", "enter");
+ urlBuilder.setValue("entry", aDirectory.getName());
+ urlBuilder.setValue("directory", "");
+ urlBuilder.setValue("subdirectory", aSubDirectory);
- int c;
- while ((c = in.read()) != -1)
- out.write(c);
- in.close();
- out.close();
+ FileReader in = new FileReader(f);
+ StringWriter out = new StringWriter();
- edit(req, res);
- } catch (Exception e) {
- throw new ServletModuleException(e.toString());
+ int c;
+ while ( (c = in.read()) != -1)
+ out.write(c);
+ in.close();
+ out.close();
+
+ responseData.put("entry", aDirectory.getName());
+ responseData.put("text", out.toString());
+ responseData.put("filename", aFileName);
+ responseData.put("subdirectory", aSubDirectory);
+ responseData.put("returnurl", urlBuilder.getQuery());
+
+ ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);
+ }
+ }
+ catch (Throwable e) {
+ throw new ServletModuleFailure(e);
+ }
+ }
+
+ protected boolean validateDirectory(FileEditDirectory aDirectory, File aFile) {
+ try {
+ return (aFile.getCanonicalPath().startsWith(aDirectory.getRootDirectory().getCanonicalPath()));
+ }
+ catch (Throwable t) {
+ return false;
+ }
+ }
+
+ protected boolean validateFile(FileEditDirectory aDirectory, File aFile) {
+ try {
+ return aDirectory.getFilter().accept(aFile.getParentFile(), aFile.getName());
+ }
+ catch (Throwable t) {
+ return false;
}
}
}