2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with any library licensed under the Apache Software License,
\r
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
\r
23 * (or with modified versions of the above that use the same license as the above),
\r
24 * and distribute linked combinations including the two. You must obey the
\r
25 * GNU General Public License in all respects for all of the code used other than
\r
26 * the above mentioned libraries. If you modify this file, you may extend this
\r
27 * exception to your version of the file, but you are not obligated to do so.
\r
28 * If you do not wish to do so, delete this exception statement from your version.
\r
30 package mircoders.localizer.basic;
\r
32 import java.util.HashMap;
\r
33 import java.util.Iterator;
\r
34 import java.util.List;
\r
35 import java.util.Map;
\r
36 import java.util.Random;
\r
37 import java.util.Vector;
\r
39 import mir.config.MirPropertiesConfiguration;
\r
40 import mir.log.LoggerWrapper;
\r
41 import mir.session.Request;
\r
42 import mir.session.Response;
\r
43 import mir.session.Session;
\r
44 import mir.session.SessionExc;
\r
45 import mir.session.SessionFailure;
\r
46 import mir.session.SessionHandler;
\r
47 import mir.session.UploadedFile;
\r
48 import mir.session.ValidationError;
\r
49 import mir.session.ValidationHelper;
\r
50 import mir.storage.StorageObject;
\r
51 import mir.util.ExceptionFunctions;
\r
52 import mircoders.global.MirGlobal;
\r
53 import mircoders.module.ModuleMediaType;
\r
57 * <p>Title: Experimental session handler for comment postings </p>
\r
58 * <p>Description: </p>
\r
59 * <p>Copyright: Copyright (c) 2003</p>
\r
61 * @author not attributable
\r
65 public abstract class MirBasicPostingSessionHandler implements SessionHandler {
\r
66 protected LoggerWrapper logger;
\r
67 protected MirPropertiesConfiguration configuration;
\r
69 private String normalResponseGenerator;
\r
70 private String dupeResponseGenerator;
\r
71 private String unsupportedMediaTypeResponseGenerator;
\r
72 private String finalResponseGenerator;
\r
75 public MirBasicPostingSessionHandler() {
\r
76 logger = new LoggerWrapper("Localizer.OpenPosting");
\r
78 configuration = MirPropertiesConfiguration.instance();
\r
80 catch (Throwable t) {
\r
81 logger.fatal("Cannot load configuration: " + t.toString());
\r
83 throw new RuntimeException("Cannot load configuration: " + t.toString());
\r
87 protected void setNormalResponseGenerator(String aGenerator) {
\r
88 normalResponseGenerator = aGenerator;
\r
91 protected void setResponseGenerators(String aNormalResponseGenerator, String aDupeResponseGenerator,
\r
92 String anUnsupportedMediaTypeResponseGenerator, String aFinalResponseGenerator) {
\r
93 setNormalResponseGenerator(aNormalResponseGenerator);
\r
94 dupeResponseGenerator = aDupeResponseGenerator;
\r
95 unsupportedMediaTypeResponseGenerator = anUnsupportedMediaTypeResponseGenerator;
\r
96 finalResponseGenerator = aFinalResponseGenerator;
\r
99 public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
100 if (MirGlobal.abuse().getOpenPostingDisabled()) {
\r
101 makeOpenPostingDisabledResponse(aRequest, aSession, aResponse);
\r
102 aSession.terminate();
\r
105 if (aSession.getAttribute("initialRequest") == null) {
\r
106 initialRequest(aRequest, aSession, aResponse);
\r
107 aSession.setAttribute("initialRequest", "no");
\r
110 subsequentRequest(aRequest, aSession, aResponse);
\r
115 protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
116 initializeSession(aRequest, aSession);
\r
117 initializeResponseData(aRequest, aSession, aResponse);
\r
118 makeInitialResponse(aRequest, aSession, aResponse);
\r
121 public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
124 List validationErrors = new Vector();
\r
126 if (!shouldProcessRequest(aRequest, aSession, validationErrors)) {
\r
127 initializeResponseData(aRequest, aSession, aResponse);
\r
128 makeResponse(aRequest, aSession, aResponse, validationErrors);
\r
131 preProcessRequest(aRequest, aSession);
\r
132 Iterator i = aRequest.getUploadedFiles().iterator();
\r
133 while (i.hasNext()) {
\r
134 processUploadedFile(aRequest, aSession, (UploadedFile) i.next());
\r
136 postProcessRequest(aRequest, aSession);
\r
137 initializeResponseData(aRequest, aSession, aResponse);
\r
138 makeFinalResponse(aRequest, aSession, aResponse);
\r
139 aSession.terminate();
\r
142 catch (Throwable t) {
\r
143 initializeResponseData(aRequest, aSession, aResponse);
\r
144 makeErrorResponse(aRequest, aSession, aResponse, t);
\r
145 aSession.terminate();
\r
148 catch (Throwable t) {
\r
149 aSession.terminate();
\r
151 throw new SessionFailure(t);
\r
155 protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
156 if (MirGlobal.abuse().getOpenPostingPassword()) {
\r
157 String password = (String) aSession.getAttribute("password");
\r
158 if (password==null) {
\r
159 password = generateOnetimePassword();
\r
160 aSession.setAttribute("password", password);
\r
164 aSession.deleteAttribute("password");
\r
167 logger.debug("referrer = " + aRequest.getHeader("Referer"));
\r
169 aSession.setAttribute("referer", aRequest.getHeader("Referer"));
\r
172 protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
173 int nrMediaItems = configuration.getInt("ServletModule.OpenIndy.DefaultMediaUploadItems", 5);
\r
175 if (aSession.getAttribute("nrmediaitems")!=null) {
\r
176 nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue();
\r
179 nrMediaItems = Math.min(configuration.getInt("ServletModule.OpenIndy.MaxMediaUploadItems"), Integer.parseInt(aRequest.getParameter("nrmediaitems")));
\r
181 catch (Throwable t) {
\r
183 aSession.setAttribute("nrmediaitems", new Integer(nrMediaItems));
\r
185 List mediaItems = new Vector();
\r
188 while (i<nrMediaItems) {
\r
190 mediaItems.add(new Integer(i));
\r
193 aResponse.setResponseValue("nrmediaitems", new Integer(nrMediaItems));
\r
194 aResponse.setResponseValue("mediaitems", mediaItems);
\r
195 aResponse.setResponseValue("password", aSession.getAttribute("password"));
\r
196 aResponse.setResponseValue("referer", aSession.getAttribute("referer"));
\r
197 aResponse.setResponseValue("errors", null);
\r
200 protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
201 aResponse.setResponseGenerator(normalResponseGenerator);
\r
204 protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure {
\r
205 aResponse.setResponseValue("errors", anErrors);
\r
206 aResponse.setResponseGenerator(normalResponseGenerator);
\r
209 protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
\r
210 aResponse.setResponseGenerator(finalResponseGenerator);
\r
213 protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure {
\r
214 anError.printStackTrace();
\r
215 Throwable rootCause = ExceptionFunctions.traceCauseException(anError);
\r
217 if (rootCause instanceof DuplicatePostingExc)
\r
218 aResponse.setResponseGenerator(dupeResponseGenerator);
\r
219 if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) {
\r
220 aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType());
\r
221 aResponse.setResponseGenerator(unsupportedMediaTypeResponseGenerator);
\r
224 aResponse.setResponseValue("errorstring", anError.getMessage());
\r
225 aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.ErrorTemplate"));
\r
229 protected void makeOpenPostingDisabledResponse(Request aRequest, Session aSession, Response aResponse) {
\r
230 aResponse.setResponseGenerator(configuration.getString("ServletModule.OpenIndy.PostingDisabledTemplate"));
\r
233 protected void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
235 public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
\r
237 protected void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
240 protected boolean shouldProcessRequest(Request aRequest, Session aSession, List aValidationErrors) throws SessionExc, SessionFailure {
\r
241 if (aRequest.getParameter("post")==null)
\r
244 validate(aValidationErrors, aRequest, aSession);
\r
245 return (aValidationErrors == null || aValidationErrors.size() == 0);
\r
249 protected void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
\r
250 String password = (String) aSession.getAttribute("password");
\r
252 if (password!=null) {
\r
253 String submittedPassword= aRequest.getParameter("password").trim();
\r
255 if (!password.equals(submittedPassword)) {
\r
256 aResults.add(new ValidationError("password", "passwordmismatch"));
\r
263 * Method to generate a one-time password
\r
265 * @return a password, to be used once
\r
268 protected String generateOnetimePassword() {
\r
269 Random r = new Random();
\r
270 int random = r.nextInt();
\r
272 long l = System.currentTimeMillis();
\r
274 l = (l*l*l*l)/random;
\r
278 String returnString = ""+l;
\r
280 return returnString.substring(5);
\r
289 * @throws SessionExc
\r
290 * @throws SessionFailure
\r
293 protected static final Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionExc, SessionFailure {
\r
294 Map result = new HashMap();
\r
296 Iterator i = aStorage.getFields().iterator();
\r
298 while (i.hasNext()) {
\r
299 String fieldName = (String) i.next();
\r
300 Object value = aRequest.getParameter(fieldName);
\r
302 result.put(fieldName, value);
\r
308 protected static class DuplicatePostingExc extends SessionExc {
\r
309 public DuplicatePostingExc(String aMessage) {
\r