X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fservlet%2FServletModuleFileEdit.java;h=6543c11ce2ea2e9e1900ab618a5531a4aaf55a47;hb=42680c1f9fe3250bcbd0f9ed5d9dee6188333b15;hp=1981aeb404d3beb1ab369e5a1912ab8d3be58aec;hpb=97a33333e6920e0b2578e506475f04c6d1d45a38;p=mir.git diff --git a/source/mircoders/servlet/ServletModuleFileEdit.java b/source/mircoders/servlet/ServletModuleFileEdit.java index 1981aeb4..6543c11c 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,32 @@ * 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 javax.servlet.http.*; -import javax.servlet.*; - -import freemarker.template.*; +import mir.servlet.AdminServletModule; +import mir.servlet.ServletModuleExc; +import mir.servlet.ServletModuleFailure; +import mir.util.FileRoutines; +import mir.util.HTTPRequestParser; +import mir.util.StringRoutines; +import mir.util.URLBuilder; +import mir.changetracker.ChangeType; +import mircoders.global.MirGlobal; -import mir.servlet.*; -import mir.misc.*; -import mir.misc.FileExtFilter; -import mir.log.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.util.*; /* * ServletModuleFileEdit - @@ -50,110 +51,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.2.14 $ $Date: 2006/12/25 20:10:23 $ * */ -public class ServletModuleFileEdit extends ServletModule -{ +public class ServletModuleFileEdit extends AdminServletModule { + private Map directories; + private List directoryNames; + + private FilenameFilter dirFilter; -// Singelton / Kontruktor + public ServletModuleFileEdit() { + directories = new HashMap(); + directoryNames = new ArrayList(); - private static ServletModuleFileEdit instance = - new ServletModuleFileEdit(); - public static ServletModule getInstance() { return instance; } + String settings[] = + getConfiguration().getStringArray("ServletModule.FileEdit.Configuration"); - private String _dirName; - private String _extName; + if (settings!=null) { + for (int i = 0; i < settings.length; i++) { + String setting = settings[i].trim(); - private ServletModuleFileEdit() { + if (setting.length() > 0) { + List parts = StringRoutines.splitStringWithEscape(setting, ':', '\\'); + if (parts.size() != 4) { + getLogger().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); - logger = new LoggerWrapper("ServletModule.FileEdit"); + directories.put(name, new FileEditDirectory(name, directory, filter, + "1".equals(recursive) || "y".equals(recursive.toLowerCase()))); + directoryNames.add(name); + } + } + } + } - _dirName = MirConfig.getProp("ServletModule.FileEdit.FileDirectory"); - _extName = MirConfig.getProp("ServletModule.FileEdit.ExtFilter"); + dirFilter = new FileRoutines.DirectoryFilter(); + } - templateListString = - MirConfig.getProp("ServletModule.FileEdit.ListTemplate"); - templateObjektString = - MirConfig.getProp("ServletModule.FileEdit.ObjektTemplate"); - templateConfirmString = - MirConfig.getProp("ServletModule.FileEdit.ConfirmTemplate"); + public List getEntries() { + return directoryNames; } - public void list(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleException - { -// fetch and deliver + 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 { 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); - HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req)); + 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); } - 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); + } + } + + /** + * Called when an edited file is saved by the user + */ + 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 = + StringRoutines.performRegularExpressionReplacement( + requestParser.getParameter("text"), + "\r\n", + System.getProperty("line.separator")); + FileEditDirectory directory = getDirectory(aRequest); + + try { + File f = new File(new File(directory.getRootDirectory(), subDirectory), filename); - deliver(req, res, withValues, null, templateObjektString); - } catch (Exception e) { - throw new ServletModuleException(e.toString()); + if (isDirectoryValid(directory, f)) { + FileWriter out = new FileWriter(f); + try { + out.write(text.toCharArray()); + } + finally { + out.close(); + } + + logAdminUsage(aRequest, f.getAbsolutePath(), "object modified"); + + MirGlobal.getChangeEngine().getTracker().addChange(f.getCanonicalPath(), ChangeType.MODIFICATION); + + editFile(directory, filename, subDirectory, aRequest, aResponse); + } + } + catch (Throwable e) { + throw new ServletModuleFailure(e); } } - public void update(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleException + public void listSubDirectory(FileEditDirectory aDirectory, String aSubDirectory, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc { - String filename = req.getParameter("filename"); - if (filename == null) throw new ServletModuleException("No filename specified"); try { + Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}); + File dir = new File(aDirectory.getRootDirectory(), aSubDirectory); + + if (!isDirectoryValid(aDirectory, dir) || !dir.isDirectory()) { + dir = aDirectory.getRootDirectory(); + aSubDirectory = ""; + } - File f = new File(_dirName, filename); - StringReader in = new StringReader(req.getParameter("text")); - FileWriter out = new FileWriter(f); + responseData.put("filelist", FileRoutines.getDirectoryContentsAsList(dir, aDirectory.getFilter())); - int c; - while ((c = in.read()) != -1) - out.write(c); - in.close(); - out.close(); + if (aDirectory.getRecursive()) { + List dirs = new ArrayList(); + if (!dir.getCanonicalPath().equals(aDirectory.getRootDirectory().getCanonicalPath())) + responseData.put("updir", new File(aSubDirectory).getParent()); - edit(req, res); - } catch (Exception e) { - throw new ServletModuleException(e.toString()); + dirs.addAll(FileRoutines.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, listGenerator); + } + catch (Throwable e) { + throw new ServletModuleFailure(e); } } + + public void editFile(FileEditDirectory aDirectory, String aFileName, String aSubDirectory, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc { + try { + File f = new File(new File(aDirectory.getRootDirectory(), aSubDirectory), aFileName); + + if (!isDirectoryValid(aDirectory, f) || f.isDirectory() || !isFileValid(aDirectory, f)) { + listSubDirectory(aDirectory, "", aRequest, aResponse); + } + else { + Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}); + URLBuilder urlBuilder = new URLBuilder(); + + urlBuilder.setValue("module", "FileEdit"); + urlBuilder.setValue("do", "enter"); + urlBuilder.setValue("entry", aDirectory.getName()); + urlBuilder.setValue("directory", ""); + urlBuilder.setValue("subdirectory", aSubDirectory); + + BufferedReader in = new BufferedReader(new FileReader(f)); + StringWriter textout = new StringWriter(); + BufferedWriter out = new BufferedWriter(textout); + + // TODO read array + char[] c = new char[4096]; + int read; + while ((read=in.read(c)) != -1) { + out.write(c, 0, read); + } + in.close(); + out.close(); + + responseData.put("entry", aDirectory.getName()); + responseData.put("text", textout.toString()); + responseData.put("filename", aFileName); + responseData.put("subdirectory", aSubDirectory); + responseData.put("returnurl", urlBuilder.getQuery()); + + ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator); + } + } + catch (Throwable e) { + throw new ServletModuleFailure(e); + } + } + + private boolean isDirectoryValid(FileEditDirectory aDirectory, File aFile) { + try { + return aFile.getCanonicalPath().startsWith(aDirectory.getRootDirectory().getCanonicalPath()); + } + catch (Throwable t) { + return false; + } + } + + private boolean isFileValid(FileEditDirectory aDirectory, File aFile) { + try { + return aDirectory.getFilter().accept(aFile.getParentFile(), aFile.getName()); + } + catch (Throwable t) { + return false; + } + } + + private class FileEditDirectory { + private String name; + private FileRoutines.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 FileRoutines.RegExpFileFilter(aFilter); + recursive = aRecursive; + } + + public String getName() { + return name; + } + + public FileRoutines.RegExpFileFilter getFilter() { + return filter; + } + + public File getRootDirectory() { + return rootDirectory; + } + + public boolean getRecursive() { + return recursive; + } + } + }