whoops
[mir.git] / source / mircoders / localizer / basic / MirBasicPostingSessionHandler.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  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.localizer.basic;
31
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Random;
37 import java.util.Vector;
38
39 import mir.config.MirPropertiesConfiguration;
40 import mir.log.LoggerWrapper;
41 import mir.session.Request;
42 import mir.session.Response;
43 import mir.session.Session;
44 import mir.session.SessionExc;
45 import mir.session.SessionFailure;
46 import mir.session.SessionHandler;
47 import mir.session.UploadedFile;
48 import mir.session.ValidationError;
49 import mir.storage.StorageObject;
50 import mir.util.ExceptionFunctions;
51 import mircoders.global.MirGlobal;
52 import mircoders.module.ModuleMediaType;
53
54 /**
55  *
56  * <p>Title: Experimental session handler for comment postings </p>
57  * <p>Description: </p>
58  * <p>Copyright: Copyright (c) 2003</p>
59  * <p>Company: </p>
60  * @author not attributable
61  * @version 1.0
62  */
63
64 public abstract class MirBasicPostingSessionHandler implements SessionHandler {
65   protected LoggerWrapper logger;
66   protected MirPropertiesConfiguration configuration;
67
68   private String normalResponseGenerator;
69   private String dupeResponseGenerator;
70   private String unsupportedMediaTypeResponseGenerator;
71   private String finalResponseGenerator;
72
73
74   public MirBasicPostingSessionHandler() {
75     logger = new LoggerWrapper("Localizer.OpenPosting");
76     try {
77       configuration = MirPropertiesConfiguration.instance();
78     }
79     catch (Throwable t) {
80       logger.fatal("Cannot load configuration: " + t.toString());
81
82       throw new RuntimeException("Cannot load configuration: " + t.toString());
83     }
84   }
85
86   protected void setNormalResponseGenerator(String aGenerator) {
87     normalResponseGenerator = aGenerator;
88   }
89
90   protected void setResponseGenerators(String aNormalResponseGenerator, String aDupeResponseGenerator,
91         String anUnsupportedMediaTypeResponseGenerator, String aFinalResponseGenerator) {
92     setNormalResponseGenerator(aNormalResponseGenerator);
93     dupeResponseGenerator = aDupeResponseGenerator;
94     unsupportedMediaTypeResponseGenerator = anUnsupportedMediaTypeResponseGenerator;
95     finalResponseGenerator = aFinalResponseGenerator;
96   }
97
98   public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
99     if (MirGlobal.abuse().getOpenPostingDisabled()) {
100       makeOpenPostingDisabledResponse(aRequest, aSession, aResponse);
101       aSession.terminate();
102     }
103     else {
104       if (aSession.getAttribute("initialRequest") == null) {
105         initialRequest(aRequest, aSession, aResponse);
106         aSession.setAttribute("initialRequest", "no");
107       }
108       else {
109         subsequentRequest(aRequest, aSession, aResponse);
110       }
111     }
112   };
113
114   protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
115     initializeSession(aRequest, aSession);
116     initializeResponseData(aRequest, aSession, aResponse);
117     makeInitialResponse(aRequest, aSession, aResponse);
118   }
119
120   public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
121     try {
122       try {
123         List validationErrors = new Vector();
124
125         if (!shouldProcessRequest(aRequest, aSession, validationErrors)) {
126           initializeResponseData(aRequest, aSession, aResponse);
127           makeResponse(aRequest, aSession, aResponse, validationErrors);
128         }
129         else {
130           preProcessRequest(aRequest, aSession);
131           Iterator i = aRequest.getUploadedFiles().iterator();
132           while (i.hasNext()) {
133             processUploadedFile(aRequest, aSession, (UploadedFile) i.next());
134           }
135           postProcessRequest(aRequest, aSession);
136           initializeResponseData(aRequest, aSession, aResponse);
137           makeFinalResponse(aRequest, aSession, aResponse);
138           aSession.terminate();
139         }
140       }
141       catch (Throwable t) {
142         initializeResponseData(aRequest, aSession, aResponse);
143         makeErrorResponse(aRequest, aSession, aResponse, t);
144         aSession.terminate();
145       }
146     }
147     catch (Throwable t) {
148       aSession.terminate();
149
150       throw new SessionFailure(t);
151     }
152   }
153
154   protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
155     if (MirGlobal.abuse().getOpenPostingPassword()) {
156       String password = (String) aSession.getAttribute("password");
157       if (password==null) {
158         password = generateOnetimePassword();
159         aSession.setAttribute("password", password);
160       }
161     }
162     else {
163       aSession.deleteAttribute("password");
164     }
165
166     logger.debug("referrer = " + aRequest.getHeader("Referer"));
167
168     aSession.setAttribute("referer", aRequest.getHeader("Referer"));
169   }
170
171   protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
172     int nrMediaItems = configuration.getInt("ServletModule.OpenIndy.DefaultMediaUploadItems", 5);
173
174     if (aSession.getAttribute("nrmediaitems")!=null) {
175       nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue();
176     }
177     try {
178       nrMediaItems = Math.min(configuration.getInt("ServletModule.OpenIndy.MaxMediaUploadItems"), Integer.parseInt(aRequest.getParameter("nrmediaitems")));
179     }
180     catch (Throwable t) {
181     }
182     aSession.setAttribute("nrmediaitems", new Integer(nrMediaItems));
183
184     List mediaItems = new Vector();
185     int i=0;
186
187     while (i<nrMediaItems) {
188       i++;
189       mediaItems.add(new Integer(i));
190     }
191
192     aResponse.setResponseValue("nrmediaitems", new Integer(nrMediaItems));
193     aResponse.setResponseValue("mediaitems", mediaItems);
194     aResponse.setResponseValue("password", aSession.getAttribute("password"));
195     aResponse.setResponseValue("referer", aSession.getAttribute("referer"));
196     aResponse.setResponseValue("errors", null);
197   }
198
199   protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
200     aResponse.setResponseGenerator(normalResponseGenerator);
201   };
202
203   protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure {
204     aResponse.setResponseValue("errors", anErrors);
205     aResponse.setResponseGenerator(normalResponseGenerator);
206   };
207
208   protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
209     aResponse.setResponseGenerator(finalResponseGenerator);
210   };
211
212   protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure {
213     anError.printStackTrace();
214     Throwable rootCause = ExceptionFunctions.traceCauseException(anError);
215
216     if (rootCause instanceof DuplicatePostingExc)
217       aResponse.setResponseGenerator(dupeResponseGenerator);
218     if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) {
219       aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType());
220       aResponse.setResponseGenerator(unsupportedMediaTypeResponseGenerator);
221     }
222     else {
223       aResponse.setResponseValue("errorstring", anError.getMessage());
224       aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.ErrorTemplate"));
225     }
226   };
227
228   protected void makeOpenPostingDisabledResponse(Request aRequest, Session aSession, Response aResponse) {
229     aResponse.setResponseGenerator(configuration.getString("ServletModule.OpenIndy.PostingDisabledTemplate"));
230   }
231
232   protected void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
233   };
234   public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
235   };
236   protected void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
237   };
238
239   protected boolean shouldProcessRequest(Request aRequest, Session aSession, List aValidationErrors) throws SessionExc, SessionFailure {
240     if (aRequest.getParameter("post")==null)
241       return false;
242     else {
243       validate(aValidationErrors, aRequest, aSession);
244       return (aValidationErrors == null || aValidationErrors.size() == 0);
245     }
246   }
247
248   protected void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
249     String password = (String) aSession.getAttribute("password");
250
251     if (password!=null) {
252       String submittedPassword= aRequest.getParameter("password").trim();
253
254       if (!password.equals(submittedPassword)) {
255         aResults.add(new ValidationError("password", "passwordmismatch"));
256       }
257     }
258   }
259
260
261   /**
262    * Method to generate a one-time password
263    *
264    * @return a password, to be used once
265    */
266
267   protected String generateOnetimePassword() {
268     Random r = new Random();
269     int random = r.nextInt();
270
271     long l = System.currentTimeMillis();
272
273     l = (l*l*l*l)/random;
274     if (l<0)
275       l = l * -1;
276
277     String returnString = ""+l;
278
279     return returnString.substring(5);
280   }
281
282
283   /**
284    *
285    * @param aRequest
286    * @param aStorage
287    * @return
288    * @throws SessionExc
289    * @throws SessionFailure
290    */
291
292   protected static final Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionExc, SessionFailure {
293     Map result = new HashMap();
294
295     Iterator i = aStorage.getFields().iterator();
296
297     while (i.hasNext()) {
298       String fieldName = (String) i.next();
299       Object value = aRequest.getParameter(fieldName);
300       if (value != null)
301         result.put(fieldName, value);
302     }
303
304     return result;
305   }
306
307   protected static class DuplicatePostingExc extends SessionExc {
308     public DuplicatePostingExc(String aMessage) {
309       super(aMessage);
310     }
311   }
312
313 }