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.Arrays;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.Random;
40 import mir.config.MirPropertiesConfiguration;
41 import mir.entity.Entity;
42 import mir.log.LoggerWrapper;
43 import mir.session.Request;
44 import mir.session.Response;
45 import mir.session.Session;
46 import mir.session.SessionExc;
47 import mir.session.SessionFailure;
48 import mir.session.SessionHandler;
49 import mir.session.UploadedFile;
50 import mir.storage.StorageObject;
51 import mir.util.ExceptionFunctions;
52 import mircoders.entity.EntityComment;
53 import mircoders.global.MirGlobal;
54 import mircoders.media.MediaUploadProcessor;
55 import mircoders.module.ModuleComment;
56 import mircoders.storage.DatabaseComment;
57 import mircoders.storage.DatabaseCommentToMedia;
58 import mircoders.storage.DatabaseContent;
62 * <p>Title: Experimental session handler for comment postings </p>
63 * <p>Description: </p>
64 * <p>Copyright: Copyright (c) 2003</p>
66 * @author not attributable
70 public abstract class MirBasicPostingSessionHandler implements SessionHandler {
71 protected LoggerWrapper logger;
72 protected MirPropertiesConfiguration configuration;
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 public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
87 if (aSession.getAttribute("initialRequest")==null) {
88 initialRequest(aRequest, aSession, aResponse);
89 aSession.setAttribute("initialRequest", "no");
92 subsequentRequest(aRequest, aSession, aResponse);
96 protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
97 initializeSession(aRequest, aSession);
98 initializeResponseData(aRequest, aSession, aResponse);
99 makeInitialResponse(aRequest, aSession, aResponse);
102 public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
106 List validationErrors = new Vector();
108 if (!shouldProcessRequest(aRequest, aSession, validationErrors)) {
109 initializeResponseData(aRequest, aSession, aResponse);
110 makeResponse(aRequest, aSession, aResponse, validationErrors);
113 preProcessRequest(aRequest, aSession);
114 Iterator i = aRequest.getUploadedFiles().iterator();
115 while (i.hasNext()) {
116 processUploadedFile(aRequest, aSession, (UploadedFile) i.next());
118 postProcessRequest(aRequest, aSession);
119 initializeResponseData(aRequest, aSession, aResponse);
120 makeFinalResponse(aRequest, aSession, aResponse);
121 aSession.terminate();
124 catch (Throwable t) {
125 initializeResponseData(aRequest, aSession, aResponse);
126 makeErrorResponse(aRequest, aSession, aResponse, t);
127 aSession.terminate();
130 catch (Throwable t) {
131 aSession.terminate();
133 throw new SessionFailure(t);
137 protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
138 if (MirGlobal.abuse().getOpenPostingPassword()) {
139 String password = (String) aSession.getAttribute("password");
140 if (password==null) {
141 password = generateOnetimePassword();
142 aSession.setAttribute("password", password);
146 aSession.deleteAttribute("password");
149 logger.debug("referrer = " + aRequest.getHeader("Referer"));
151 aSession.setAttribute("referer", aRequest.getHeader("Referer"));
152 aSession.setAttribute("nrmediaitems",
153 new Integer(configuration.getInt("ServletModule.OpenIndy.DefaultMediaUploadItems")));
156 protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
157 int nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue();
158 List mediaItems = new Vector();
161 while (i<nrMediaItems) {
163 mediaItems.add(new Integer(i));
166 aResponse.setResponseValue("nrmediaitems", new Integer(nrMediaItems));
167 aResponse.setResponseValue("mediaitems", mediaItems);
168 aResponse.setResponseValue("password", aSession.getAttribute("password"));
169 aResponse.setResponseValue("referer", aSession.getAttribute("referer"));
170 aResponse.setResponseValue("errors", null);
173 protected abstract void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure;
174 protected abstract void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure;
175 protected abstract void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure;
177 protected void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
179 public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
181 protected void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
184 protected boolean shouldProcessRequest(Request aRequest, Session aSession, List aValidationErrors) throws SessionExc, SessionFailure {
185 int nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue();
187 nrMediaItems = Math.min(configuration.getInt("ServletModule.OpenIndy.MaxMediaUploadItems"), Integer.parseInt(aRequest.getParameter("nrmediaitems")));
189 catch (Throwable t) {
192 aSession.setAttribute("nrmediaitems", new Integer(nrMediaItems));
194 if (aRequest.getParameter("post")==null)
197 validate(aValidationErrors, aRequest, aSession);
198 return (aValidationErrors == null || aValidationErrors.size() == 0);
202 protected void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
205 protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure {
206 aResponse.setResponseValue("errorstring", anError.getMessage());
207 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.ErrorTemplate"));
211 * Class that represents a validation error
214 * <p>Description: </p>
215 * <p>Copyright: Copyright (c) 2003</p>
217 * @author not attributable
221 public class ValidationError {
222 private String field;
223 private String message;
224 private List parameters;
226 public ValidationError(String aField, String aMessage) {
227 this (aField, aMessage, new String[] {});
230 public ValidationError(String aField, String aMessage, Object aParameter) {
231 this (aField, aMessage, new Object[] {aParameter});
234 public ValidationError(String aField, String aMessage, Object[] aParameters) {
237 parameters = Arrays.asList(aParameters);
240 public String getMessage() {
244 public String getField() {
248 public List getParameters() {
254 * Convenience validation method to test wether a field has been filled in
258 * @param anErrorMessageResource
259 * @param aValidationResults
263 protected boolean testFieldEntered(Request aRequest, String aFieldName, String anErrorMessageResource, List aValidationResults) {
264 Object value = aRequest.getParameter(aFieldName);
265 if (value==null || !(value instanceof String) || ((String) value).trim().length()==0) {
266 aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
274 * Convenience validation method to test wether a field is numeric
278 * @param anErrorMessageResource
279 * @param aValidationResults
283 protected boolean testFieldIsNumeric(Request aRequest, String aFieldName, String anErrorMessageResource, List aValidationResults) {
284 Object value = aRequest.getParameter(aFieldName);
287 Integer.parseInt((String) value);
290 catch (Throwable t) {
291 aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
299 * Convenience validation method to test wether a field is numeric
303 * @param anErrorMessageResource
304 * @param aValidationResults
308 protected boolean testFieldLength(Request aRequest, String aFieldName, int aMaxLength, String anErrorMessageResource, List aValidationResults) {
309 String value = aRequest.getParameter(aFieldName);
312 if (value.length()>aMaxLength) {
313 aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
322 * Method to generate a one-time password
324 * @return a password, to be used once
327 protected String generateOnetimePassword() {
328 Random r = new Random();
329 int random = r.nextInt();
331 long l = System.currentTimeMillis();
333 l = (l*l*l*l)/random;
337 String returnString = ""+l;
339 return returnString.substring(5);
349 * @throws SessionFailure
352 protected static final Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionExc, SessionFailure {
353 Map result = new HashMap();
355 Iterator i = aStorage.getFields().iterator();
357 while (i.hasNext()) {
358 String fieldName = (String) i.next();
359 Object value = aRequest.getParameter(fieldName);
361 result.put(fieldName, value);