2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
30 package mircoders.localizer.basic;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
36 import java.util.Random;
37 import java.util.Vector;
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;
56 * <p>Title: Experimental session handler for comment postings </p>
57 * <p>Description: </p>
58 * <p>Copyright: Copyright (c) 2003</p>
60 * @author not attributable
64 public abstract class MirBasicPostingSessionHandler implements SessionHandler {
65 protected LoggerWrapper logger;
66 protected MirPropertiesConfiguration configuration;
68 private String normalResponseGenerator;
69 private String dupeResponseGenerator;
70 private String unsupportedMediaTypeResponseGenerator;
71 private String finalResponseGenerator;
74 public MirBasicPostingSessionHandler() {
75 logger = new LoggerWrapper("Localizer.OpenPosting");
77 configuration = MirPropertiesConfiguration.instance();
80 logger.fatal("Cannot load configuration: " + t.toString());
82 throw new RuntimeException("Cannot load configuration: " + t.toString());
86 protected void setNormalResponseGenerator(String aGenerator) {
87 normalResponseGenerator = aGenerator;
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;
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();
104 if (aSession.getAttribute("initialRequest") == null) {
105 initialRequest(aRequest, aSession, aResponse);
106 aSession.setAttribute("initialRequest", "no");
109 subsequentRequest(aRequest, aSession, aResponse);
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);
120 public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
123 List validationErrors = new Vector();
125 if (!shouldProcessRequest(aRequest, aSession, validationErrors)) {
126 initializeResponseData(aRequest, aSession, aResponse);
127 makeResponse(aRequest, aSession, aResponse, validationErrors);
130 preProcessRequest(aRequest, aSession);
131 Iterator i = aRequest.getUploadedFiles().iterator();
132 while (i.hasNext()) {
133 processUploadedFile(aRequest, aSession, (UploadedFile) i.next());
135 postProcessRequest(aRequest, aSession);
136 initializeResponseData(aRequest, aSession, aResponse);
137 makeFinalResponse(aRequest, aSession, aResponse);
138 aSession.terminate();
141 catch (Throwable t) {
142 initializeResponseData(aRequest, aSession, aResponse);
143 makeErrorResponse(aRequest, aSession, aResponse, t);
144 aSession.terminate();
147 catch (Throwable t) {
148 aSession.terminate();
150 throw new SessionFailure(t);
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);
163 aSession.deleteAttribute("password");
166 logger.debug("referrer = " + aRequest.getHeader("Referer"));
168 aSession.setAttribute("referer", aRequest.getHeader("Referer"));
171 protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
172 int nrMediaItems = configuration.getInt("ServletModule.OpenIndy.DefaultMediaUploadItems", 5);
174 if (aSession.getAttribute("nrmediaitems")!=null) {
175 nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue();
178 nrMediaItems = Math.min(configuration.getInt("ServletModule.OpenIndy.MaxMediaUploadItems"), Integer.parseInt(aRequest.getParameter("nrmediaitems")));
180 catch (Throwable t) {
182 aSession.setAttribute("nrmediaitems", new Integer(nrMediaItems));
184 List mediaItems = new Vector();
187 while (i<nrMediaItems) {
189 mediaItems.add(new Integer(i));
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);
199 protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
200 aResponse.setResponseGenerator(normalResponseGenerator);
203 protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure {
204 aResponse.setResponseValue("errors", anErrors);
205 aResponse.setResponseGenerator(normalResponseGenerator);
208 protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
209 aResponse.setResponseGenerator(finalResponseGenerator);
212 protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure {
213 anError.printStackTrace();
214 Throwable rootCause = ExceptionFunctions.traceCauseException(anError);
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);
223 aResponse.setResponseValue("errorstring", anError.getMessage());
224 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.ErrorTemplate"));
228 protected void makeOpenPostingDisabledResponse(Request aRequest, Session aSession, Response aResponse) {
229 aResponse.setResponseGenerator(configuration.getString("ServletModule.OpenIndy.PostingDisabledTemplate"));
232 protected void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
234 public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
236 protected void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
239 protected boolean shouldProcessRequest(Request aRequest, Session aSession, List aValidationErrors) throws SessionExc, SessionFailure {
240 if (aRequest.getParameter("post")==null)
243 validate(aValidationErrors, aRequest, aSession);
244 return (aValidationErrors == null || aValidationErrors.size() == 0);
248 protected void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
249 String password = (String) aSession.getAttribute("password");
251 if (password!=null) {
252 String submittedPassword= aRequest.getParameter("password").trim();
254 if (!password.equals(submittedPassword)) {
255 aResults.add(new ValidationError("password", "passwordmismatch"));
262 * Method to generate a one-time password
264 * @return a password, to be used once
267 protected String generateOnetimePassword() {
268 Random r = new Random();
269 int random = r.nextInt();
271 long l = System.currentTimeMillis();
273 l = (l*l*l*l)/random;
277 String returnString = ""+l;
279 return returnString.substring(5);
289 * @throws SessionFailure
292 protected static final Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionExc, SessionFailure {
293 Map result = new HashMap();
295 Iterator i = aStorage.getFields().iterator();
297 while (i.hasNext()) {
298 String fieldName = (String) i.next();
299 Object value = aRequest.getParameter(fieldName);
301 result.put(fieldName, value);
307 protected static class DuplicatePostingExc extends SessionExc {
308 public DuplicatePostingExc(String aMessage) {