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;
59 import mircoders.module.*;
63 * <p>Title: Experimental session handler for comment postings </p>
64 * <p>Description: </p>
65 * <p>Copyright: Copyright (c) 2003</p>
67 * @author not attributable
71 public abstract class MirBasicPostingSessionHandler implements SessionHandler {
72 protected LoggerWrapper logger;
73 protected MirPropertiesConfiguration configuration;
75 private String normalResponseGenerator;
76 private String dupeResponseGenerator;
77 private String unsupportedMediaTypeResponseGenerator;
78 private String finalResponseGenerator;
81 public MirBasicPostingSessionHandler() {
82 logger = new LoggerWrapper("Localizer.OpenPosting");
84 configuration = MirPropertiesConfiguration.instance();
87 logger.fatal("Cannot load configuration: " + t.toString());
89 throw new RuntimeException("Cannot load configuration: " + t.toString());
93 protected void setNormalResponseGenerator(String aGenerator) {
94 normalResponseGenerator = aGenerator;
97 protected void setResponseGenerators(String aNormalResponseGenerator, String aDupeResponseGenerator,
98 String anUnsupportedMediaTypeResponseGenerator, String aFinalResponseGenerator) {
99 setNormalResponseGenerator(aNormalResponseGenerator);
100 dupeResponseGenerator = aDupeResponseGenerator;
101 unsupportedMediaTypeResponseGenerator = anUnsupportedMediaTypeResponseGenerator;
102 finalResponseGenerator = aFinalResponseGenerator;
105 public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
106 if (aSession.getAttribute("initialRequest")==null) {
107 initialRequest(aRequest, aSession, aResponse);
108 aSession.setAttribute("initialRequest", "no");
111 subsequentRequest(aRequest, aSession, aResponse);
115 protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
116 initializeSession(aRequest, aSession);
117 initializeResponseData(aRequest, aSession, aResponse);
118 makeInitialResponse(aRequest, aSession, aResponse);
121 public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
125 List validationErrors = new Vector();
127 if (!shouldProcessRequest(aRequest, aSession, validationErrors)) {
128 initializeResponseData(aRequest, aSession, aResponse);
129 makeResponse(aRequest, aSession, aResponse, validationErrors);
132 preProcessRequest(aRequest, aSession);
133 Iterator i = aRequest.getUploadedFiles().iterator();
134 while (i.hasNext()) {
135 processUploadedFile(aRequest, aSession, (UploadedFile) i.next());
137 postProcessRequest(aRequest, aSession);
138 initializeResponseData(aRequest, aSession, aResponse);
139 makeFinalResponse(aRequest, aSession, aResponse);
140 aSession.terminate();
143 catch (Throwable t) {
144 initializeResponseData(aRequest, aSession, aResponse);
145 makeErrorResponse(aRequest, aSession, aResponse, t);
146 aSession.terminate();
149 catch (Throwable t) {
150 aSession.terminate();
152 throw new SessionFailure(t);
156 protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
157 if (MirGlobal.abuse().getOpenPostingPassword()) {
158 String password = (String) aSession.getAttribute("password");
159 if (password==null) {
160 password = generateOnetimePassword();
161 aSession.setAttribute("password", password);
165 aSession.deleteAttribute("password");
168 logger.debug("referrer = " + aRequest.getHeader("Referer"));
170 aSession.setAttribute("referer", aRequest.getHeader("Referer"));
171 aSession.setAttribute("nrmediaitems",
172 new Integer(configuration.getInt("ServletModule.OpenIndy.DefaultMediaUploadItems")));
175 protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
176 int nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue();
177 List mediaItems = new Vector();
180 while (i<nrMediaItems) {
182 mediaItems.add(new Integer(i));
185 aResponse.setResponseValue("nrmediaitems", new Integer(nrMediaItems));
186 aResponse.setResponseValue("mediaitems", mediaItems);
187 aResponse.setResponseValue("password", aSession.getAttribute("password"));
188 aResponse.setResponseValue("referer", aSession.getAttribute("referer"));
189 aResponse.setResponseValue("errors", null);
192 protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
193 aResponse.setResponseGenerator(normalResponseGenerator);
196 protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure {
197 aResponse.setResponseValue("errors", anErrors);
198 aResponse.setResponseGenerator(normalResponseGenerator);
201 protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
202 aResponse.setResponseGenerator(finalResponseGenerator);
205 protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure {
206 anError.printStackTrace();
207 Throwable rootCause = ExceptionFunctions.traceCauseException(anError);
209 if (rootCause instanceof DuplicatePostingExc)
210 aResponse.setResponseGenerator(dupeResponseGenerator);
211 if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) {
212 aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType());
213 aResponse.setResponseGenerator(unsupportedMediaTypeResponseGenerator);
216 aResponse.setResponseValue("errorstring", anError.getMessage());
217 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.ErrorTemplate"));
221 protected void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
223 public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
225 protected void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
228 protected boolean shouldProcessRequest(Request aRequest, Session aSession, List aValidationErrors) throws SessionExc, SessionFailure {
229 int nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue();
231 nrMediaItems = Math.min(configuration.getInt("ServletModule.OpenIndy.MaxMediaUploadItems"), Integer.parseInt(aRequest.getParameter("nrmediaitems")));
233 catch (Throwable t) {
236 aSession.setAttribute("nrmediaitems", new Integer(nrMediaItems));
238 if (aRequest.getParameter("post")==null)
241 validate(aValidationErrors, aRequest, aSession);
242 return (aValidationErrors == null || aValidationErrors.size() == 0);
246 protected void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
250 * Class that represents a validation error
253 * <p>Description: </p>
254 * <p>Copyright: Copyright (c) 2003</p>
256 * @author not attributable
260 public class ValidationError {
261 private String field;
262 private String message;
263 private List parameters;
265 public ValidationError(String aField, String aMessage) {
266 this (aField, aMessage, new String[] {});
269 public ValidationError(String aField, String aMessage, Object aParameter) {
270 this (aField, aMessage, new Object[] {aParameter});
273 public ValidationError(String aField, String aMessage, Object[] aParameters) {
276 parameters = Arrays.asList(aParameters);
279 public String getMessage() {
283 public String getField() {
287 public List getParameters() {
293 * Convenience validation method to test wether a field has been filled in
297 * @param anErrorMessageResource
298 * @param aValidationResults
302 protected boolean testFieldEntered(Request aRequest, String aFieldName, String anErrorMessageResource, List aValidationResults) {
303 Object value = aRequest.getParameter(aFieldName);
304 if (value==null || !(value instanceof String) || ((String) value).trim().length()==0) {
305 aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
313 * Convenience validation method to test wether a field is numeric
317 * @param anErrorMessageResource
318 * @param aValidationResults
322 protected boolean testFieldIsNumeric(Request aRequest, String aFieldName, String anErrorMessageResource, List aValidationResults) {
323 Object value = aRequest.getParameter(aFieldName);
326 Integer.parseInt((String) value);
329 catch (Throwable t) {
330 aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
338 * Convenience validation method to test wether a field is numeric
342 * @param anErrorMessageResource
343 * @param aValidationResults
347 protected boolean testFieldLength(Request aRequest, String aFieldName, int aMaxLength, String anErrorMessageResource, List aValidationResults) {
348 String value = aRequest.getParameter(aFieldName);
351 if (value.length()>aMaxLength) {
352 aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
361 * Method to generate a one-time password
363 * @return a password, to be used once
366 protected String generateOnetimePassword() {
367 Random r = new Random();
368 int random = r.nextInt();
370 long l = System.currentTimeMillis();
372 l = (l*l*l*l)/random;
376 String returnString = ""+l;
378 return returnString.substring(5);
388 * @throws SessionFailure
391 protected static final Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionExc, SessionFailure {
392 Map result = new HashMap();
394 Iterator i = aStorage.getFields().iterator();
396 while (i.hasNext()) {
397 String fieldName = (String) i.next();
398 Object value = aRequest.getParameter(fieldName);
400 result.put(fieldName, value);
406 protected static class DuplicatePostingExc extends SessionExc {
407 public DuplicatePostingExc(String aMessage) {