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.setResponseGenerator(configuration.getString("Localizer.OpenSession.ErrorTemplate"));
210 * Class that represents a validation error
213 * <p>Description: </p>
214 * <p>Copyright: Copyright (c) 2003</p>
216 * @author not attributable
220 public class ValidationError {
221 private String field;
222 private String message;
223 private List parameters;
225 public ValidationError(String aField, String aMessage) {
226 this (aField, aMessage, new String[] {});
229 public ValidationError(String aField, String aMessage, Object aParameter) {
230 this (aField, aMessage, new Object[] {aParameter});
233 public ValidationError(String aField, String aMessage, Object[] aParameters) {
236 parameters = Arrays.asList(aParameters);
239 public String getMessage() {
243 public String getField() {
247 public List getParameters() {
253 * Convenience validation method to test wether a field has been filled in
257 * @param anErrorMessageResource
258 * @param aValidationResults
262 protected boolean testFieldEntered(Request aRequest, String aFieldName, String anErrorMessageResource, List aValidationResults) {
263 Object value = aRequest.getParameter(aFieldName);
264 if (value==null || !(value instanceof String) || ((String) value).trim().length()==0) {
265 aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
273 * Convenience validation method to test wether a field is numeric
277 * @param anErrorMessageResource
278 * @param aValidationResults
282 protected boolean testFieldIsNumeric(Request aRequest, String aFieldName, String anErrorMessageResource, List aValidationResults) {
283 Object value = aRequest.getParameter(aFieldName);
286 Integer.parseInt((String) value);
289 catch (Throwable t) {
290 aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
298 * Convenience validation method to test wether a field is numeric
302 * @param anErrorMessageResource
303 * @param aValidationResults
307 protected boolean testFieldLength(Request aRequest, String aFieldName, int aMaxLength, String anErrorMessageResource, List aValidationResults) {
308 String value = aRequest.getParameter(aFieldName);
311 if (value.length()>aMaxLength) {
312 aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
320 * Method to generate a one-time password
322 * @return a password, to be used once
324 protected String generateOnetimePassword() {
325 Random r = new Random();
326 int random = r.nextInt();
328 long l = System.currentTimeMillis();
330 l = (l*l*l*l)/random;
334 String returnString = ""+l;
336 return returnString.substring(5);
346 * @throws SessionFailure
349 protected static final Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionExc, SessionFailure {
350 Map result = new HashMap();
352 Iterator i = aStorage.getFields().iterator();
354 while (i.hasNext()) {
355 String fieldName = (String) i.next();
356 Object value = aRequest.getParameter(fieldName);
358 result.put(fieldName, value);