X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fservlet%2FServletModuleFileEdit.java;h=3b4ca3bf34205c9c5c8cbbf429c94c0e811c312e;hb=a0e0629d9b38c2eb4eba6d9420ccbc6d1b44ab00;hp=1981aeb404d3beb1ab369e5a1912ab8d3be58aec;hpb=97a33333e6920e0b2578e506475f04c6d1d45a38;p=mir.git diff --git a/source/mircoders/servlet/ServletModuleFileEdit.java b/source/mircoders/servlet/ServletModuleFileEdit.java index 1981aeb4..3b4ca3bf 100755 --- a/source/mircoders/servlet/ServletModuleFileEdit.java +++ b/source/mircoders/servlet/ServletModuleFileEdit.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2001, 2002 The Mir-coders group * * This file is part of Mir. * @@ -18,31 +18,40 @@ * 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.*; -import java.net.*; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +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 javax.servlet.http.*; -import javax.servlet.*; - -import freemarker.template.*; - -import mir.servlet.*; -import mir.misc.*; -import mir.misc.FileExtFilter; -import mir.log.*; +import mir.log.LoggerWrapper; +import mir.servlet.ServletModule; +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 - @@ -50,110 +59,265 @@ import mir.log.*; * in the config file. * * @author $Author: zapata $ - * @version $Revision: 1.3 $ $Date: 2002/11/29 13:43:42 $ + * @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; + } + + public String getName() { + return name; + } + + public FileFunctions.RegExpFileFilter getFilter() { + return filter; + } + + public File getRootDirectory() { + return rootDirectory; + } - private String _dirName; - private String _extName; + public boolean getRecursive() { + return recursive; + } + } private ServletModuleFileEdit() { + super(); logger = new LoggerWrapper("ServletModule.FileEdit"); - _dirName = MirConfig.getProp("ServletModule.FileEdit.FileDirectory"); - _extName = MirConfig.getProp("ServletModule.FileEdit.ExtFilter"); + directories = new HashMap(); + directoryNames = new Vector(); - templateListString = - MirConfig.getProp("ServletModule.FileEdit.ListTemplate"); - templateObjektString = - MirConfig.getProp("ServletModule.FileEdit.ObjektTemplate"); - templateConfirmString = - MirConfig.getProp("ServletModule.FileEdit.ConfirmTemplate"); + 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 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 list(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleException + 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"); - HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req)); + editFile(getDirectory(aRequest), filename, subDirectory, 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 enter(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc { - String filename = req.getParameter("filename"); - if (filename == null) throw new ServletModuleException("No filename specified"); try { + HTTPRequestParser requestParser = new HTTPRequestParser(aRequest); - File f = new File(_dirName, filename); - FileReader in = new FileReader(f); - StringWriter out = new StringWriter(); + String directoryName = requestParser.getParameter("directory"); + String subDirectoryName = requestParser.getParameter("subdirectory"); - 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); + if (directoryName==null | subDirectoryName==null) + throw new ServletModuleExc("No directory/subDirectory specified"); + listSubDirectory(getDirectory(aRequest), subDirectoryName+File.separator+directoryName, aRequest, aResponse); + } + catch (Throwable e) { + throw new ServletModuleFailure(e); + } + } + + public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc + { + HTTPRequestParser requestParser = new HTTPRequestParser(aRequest); + String filename = requestParser.getParameter("filename"); + String subDirectory = requestParser.getParameter("subdirectory"); + String text = requestParser.getParameter("text"); + FileEditDirectory directory = getDirectory(aRequest); - deliver(req, res, withValues, null, templateObjektString); - } catch (Exception e) { - throw new ServletModuleException(e.toString()); + 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()); + + dirs.addAll(FileFunctions.getDirectoryContentsAsList(dir, dirFilter)); + + responseData.put("dirlist", dirs); + } + else { + responseData.put("dirlist", null); + responseData.put("updir", null); + } + + responseData.put("subdirectory", aSubDirectory); + responseData.put("entry", aDirectory.getName()); + + 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; } } }