- all servletmodules and all modules now use log4j for logging
[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.*;
35 import java.net.*;
36
37 import javax.servlet.http.*;
38 import javax.servlet.*;
39
40 import freemarker.template.*;
41
42 import mir.servlet.*;
43 import mir.misc.*;
44 import mir.misc.FileExtFilter;
45 import mir.log.*;
46
47 /*
48  *  ServletModuleFileEdit -
49  *  Allows one to do a basic edit of a file in a directory specified
50  *  in the config file.
51  *
52  * @author $Author: zapata $
53  * @version $Revision: 1.3 $ $Date: 2002/11/29 13:43:42 $
54  *
55  */
56
57 public class ServletModuleFileEdit extends ServletModule
58 {
59
60 // Singelton / Kontruktor
61
62   private static ServletModuleFileEdit instance =
63       new ServletModuleFileEdit();
64   public static ServletModule getInstance() { return instance; }
65
66   private String _dirName;
67   private String _extName;
68
69   private ServletModuleFileEdit() {
70
71     logger = new LoggerWrapper("ServletModule.FileEdit");
72
73     _dirName = MirConfig.getProp("ServletModule.FileEdit.FileDirectory");
74     _extName = MirConfig.getProp("ServletModule.FileEdit.ExtFilter");
75
76     templateListString =
77         MirConfig.getProp("ServletModule.FileEdit.ListTemplate");
78     templateObjektString =
79         MirConfig.getProp("ServletModule.FileEdit.ObjektTemplate");
80     templateConfirmString =
81         MirConfig.getProp("ServletModule.FileEdit.ConfirmTemplate");
82   }
83
84   public void list(HttpServletRequest req, HttpServletResponse res)
85       throws ServletModuleException
86   {
87 // fetch and deliver
88     try {
89       SimpleHash mergeData = new SimpleHash();
90       String offset = req.getParameter("offset");
91       if (offset==null || offset.equals("")) offset="0";
92       mergeData.put("offset",offset);
93       File dir = new File(_dirName);
94       System.out.println("DIRNAME: "+_dirName);
95       FileExtFilter extFilter = new FileExtFilter(_extName);
96       String[] dirEntries = dir.list(extFilter);
97       SimpleList theList = new SimpleList();
98       for ( int i = 0; i < dirEntries.length; ++i ) {
99         System.out.println(" FILE: "+dirEntries[i]);
100         theList.add(dirEntries[i]);
101       }
102       mergeData.put("filelist",theList);
103
104       HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
105     }
106     catch (IOException e) {throw new ServletModuleException(e.toString());}
107     catch (Exception e) {throw new ServletModuleException(e.toString());}
108   }
109
110   public void edit(HttpServletRequest req, HttpServletResponse res)
111       throws ServletModuleException
112   {
113     String filename = req.getParameter("filename");
114     if (filename == null) throw new ServletModuleException("No filename specified");
115     try {
116
117       File f = new File(_dirName, filename);
118       FileReader in = new FileReader(f);
119       StringWriter out = new StringWriter();
120
121       int c;
122       while ((c = in.read()) != -1)
123         out.write(c);
124       in.close();
125       out.close();
126       SimpleHash withValues = new SimpleHash();
127       withValues.put("text", out.toString());
128       withValues.put("filename", filename);
129
130
131       deliver(req, res, withValues, null, templateObjektString);
132     } catch (Exception e) {
133       throw new ServletModuleException(e.toString());
134     }
135   }
136
137   public void update(HttpServletRequest req, HttpServletResponse res)
138       throws ServletModuleException
139   {
140     String filename = req.getParameter("filename");
141     if (filename == null) throw new ServletModuleException("No filename specified");
142     try {
143
144       File f = new File(_dirName, filename);
145       StringReader in = new StringReader(req.getParameter("text"));
146       FileWriter out = new FileWriter(f);
147
148       int c;
149       while ((c = in.read()) != -1)
150         out.write(c);
151       in.close();
152       out.close();
153
154       edit(req, res);
155     } catch (Exception e) {
156       throw new ServletModuleException(e.toString());
157     }
158   }
159 }