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