-/*\r
- * Copyright (C) 2001, 2002 The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with any library licensed under the Apache Software License,\r
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
- * (or with modified versions of the above that use the same license as the above),\r
- * and distribute linked combinations including the two. You must obey the\r
- * GNU General Public License in all respects for all of the code used other than\r
- * the above mentioned libraries. If you modify this file, you may extend this\r
- * exception to your version of the file, but you are not obligated to do so.\r
- * If you do not wish to do so, delete this exception statement from your version.\r
- */\r
-package mircoders.localizer.basic;\r
-\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Random;\r
-import java.util.Vector;\r
-\r
-import mir.config.MirPropertiesConfiguration;\r
-import mir.log.LoggerWrapper;\r
-import mir.session.Request;\r
-import mir.session.Response;\r
-import mir.session.Session;\r
-import mir.session.SessionExc;\r
-import mir.session.SessionFailure;\r
-import mir.session.SessionHandler;\r
-import mir.session.UploadedFile;\r
-import mir.session.ValidationError;\r
-import mir.session.ValidationHelper;\r
-import mir.storage.StorageObject;\r
-import mir.util.ExceptionFunctions;\r
-import mircoders.global.MirGlobal;\r
-import mircoders.module.ModuleMediaType;\r
-\r
-/**\r
- *\r
- * <p>Title: Experimental session handler for comment postings </p>\r
- * <p>Description: </p>\r
- * <p>Copyright: Copyright (c) 2003</p>\r
- * <p>Company: </p>\r
- * @author not attributable\r
- * @version 1.0\r
- */\r
-\r
-public abstract class MirBasicPostingSessionHandler implements SessionHandler {\r
- protected LoggerWrapper logger;\r
- protected MirPropertiesConfiguration configuration;\r
-\r
- private String normalResponseGenerator;\r
- private String dupeResponseGenerator;\r
- private String unsupportedMediaTypeResponseGenerator;\r
- private String finalResponseGenerator;\r
-\r
-\r
- public MirBasicPostingSessionHandler() {\r
- logger = new LoggerWrapper("Localizer.OpenPosting");\r
- try {\r
- configuration = MirPropertiesConfiguration.instance();\r
- }\r
- catch (Throwable t) {\r
- logger.fatal("Cannot load configuration: " + t.toString());\r
-\r
- throw new RuntimeException("Cannot load configuration: " + t.toString());\r
- }\r
- }\r
-\r
- protected void setNormalResponseGenerator(String aGenerator) {\r
- normalResponseGenerator = aGenerator;\r
- }\r
-\r
- protected void setResponseGenerators(String aNormalResponseGenerator, String aDupeResponseGenerator,\r
- String anUnsupportedMediaTypeResponseGenerator, String aFinalResponseGenerator) {\r
- setNormalResponseGenerator(aNormalResponseGenerator);\r
- dupeResponseGenerator = aDupeResponseGenerator;\r
- unsupportedMediaTypeResponseGenerator = anUnsupportedMediaTypeResponseGenerator;\r
- finalResponseGenerator = aFinalResponseGenerator;\r
- }\r
-\r
- public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {\r
- if (MirGlobal.abuse().getOpenPostingDisabled()) {\r
- makeOpenPostingDisabledResponse(aRequest, aSession, aResponse);\r
- aSession.terminate();\r
- }\r
- else {\r
- if (aSession.getAttribute("initialRequest") == null) {\r
- initialRequest(aRequest, aSession, aResponse);\r
- aSession.setAttribute("initialRequest", "no");\r
- }\r
- else {\r
- subsequentRequest(aRequest, aSession, aResponse);\r
- }\r
- }\r
- };\r
-\r
- protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {\r
- initializeSession(aRequest, aSession);\r
- initializeResponseData(aRequest, aSession, aResponse);\r
- makeInitialResponse(aRequest, aSession, aResponse);\r
- }\r
-\r
- public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {\r
- try {\r
- try {\r
- List validationErrors = new Vector();\r
-\r
- if (!shouldProcessRequest(aRequest, aSession, validationErrors)) {\r
- initializeResponseData(aRequest, aSession, aResponse);\r
- makeResponse(aRequest, aSession, aResponse, validationErrors);\r
- }\r
- else {\r
- preProcessRequest(aRequest, aSession);\r
- Iterator i = aRequest.getUploadedFiles().iterator();\r
- while (i.hasNext()) {\r
- processUploadedFile(aRequest, aSession, (UploadedFile) i.next());\r
- }\r
- postProcessRequest(aRequest, aSession);\r
- initializeResponseData(aRequest, aSession, aResponse);\r
- makeFinalResponse(aRequest, aSession, aResponse);\r
- aSession.terminate();\r
- }\r
- }\r
- catch (Throwable t) {\r
- initializeResponseData(aRequest, aSession, aResponse);\r
- makeErrorResponse(aRequest, aSession, aResponse, t);\r
- aSession.terminate();\r
- }\r
- }\r
- catch (Throwable t) {\r
- aSession.terminate();\r
-\r
- throw new SessionFailure(t);\r
- }\r
- }\r
-\r
- protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {\r
- if (MirGlobal.abuse().getOpenPostingPassword()) {\r
- String password = (String) aSession.getAttribute("password");\r
- if (password==null) {\r
- password = generateOnetimePassword();\r
- aSession.setAttribute("password", password);\r
- }\r
- }\r
- else {\r
- aSession.deleteAttribute("password");\r
- }\r
-\r
- int nrMediaItems = configuration.getInt("ServletModule.OpenIndy.DefaultMediaUploadItems", 5);\r
-\r
- if (aSession.getAttribute("nrmediaitems")!=null) {\r
- nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue();\r
- }\r
- try {\r
- nrMediaItems = Math.min(configuration.getInt("ServletModule.OpenIndy.MaxMediaUploadItems"), Integer.parseInt(aRequest.getParameter("nrmediaitems")));\r
- }\r
- catch (Throwable t) {\r
- }\r
- aSession.setAttribute("nrmediaitems", new Integer(nrMediaItems));\r
-\r
- logger.debug("referrer = " + aRequest.getHeader("Referer"));\r
-\r
- aSession.setAttribute("referer", aRequest.getHeader("Referer"));\r
- }\r
-\r
- protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {\r
- int nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue();\r
- List mediaItems = new Vector();\r
- int i=0;\r
-\r
- while (i<nrMediaItems) {\r
- i++;\r
- mediaItems.add(new Integer(i));\r
- }\r
-\r
- aResponse.setResponseValue("nrmediaitems", new Integer(nrMediaItems));\r
- aResponse.setResponseValue("mediaitems", mediaItems);\r
- aResponse.setResponseValue("password", aSession.getAttribute("password"));\r
- aResponse.setResponseValue("referer", aSession.getAttribute("referer"));\r
- aResponse.setResponseValue("errors", null);\r
- }\r
-\r
- protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {\r
- aResponse.setResponseGenerator(normalResponseGenerator);\r
- };\r
-\r
- protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure {\r
- aResponse.setResponseValue("errors", anErrors);\r
- aResponse.setResponseGenerator(normalResponseGenerator);\r
- };\r
-\r
- protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {\r
- aResponse.setResponseGenerator(finalResponseGenerator);\r
- };\r
-\r
- protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure {\r
- anError.printStackTrace();\r
- Throwable rootCause = ExceptionFunctions.traceCauseException(anError);\r
-\r
- if (rootCause instanceof DuplicatePostingExc)\r
- aResponse.setResponseGenerator(dupeResponseGenerator);\r
- if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) {\r
- aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType());\r
- aResponse.setResponseGenerator(unsupportedMediaTypeResponseGenerator);\r
- }\r
- else {\r
- aResponse.setResponseValue("errorstring", anError.getMessage());\r
- aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.ErrorTemplate"));\r
- }\r
- };\r
-\r
- protected void makeOpenPostingDisabledResponse(Request aRequest, Session aSession, Response aResponse) {\r
- aResponse.setResponseGenerator(configuration.getString("ServletModule.OpenIndy.PostingDisabledTemplate"));\r
- }\r
-\r
- protected void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {\r
- };\r
- public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {\r
- };\r
- protected void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {\r
- };\r
-\r
- protected boolean shouldProcessRequest(Request aRequest, Session aSession, List aValidationErrors) throws SessionExc, SessionFailure {\r
- if (aRequest.getParameter("post")==null)\r
- return false;\r
- else {\r
- validate(aValidationErrors, aRequest, aSession);\r
- return (aValidationErrors == null || aValidationErrors.size() == 0);\r
- }\r
- }\r
-\r
- protected void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {\r
- String password = (String) aSession.getAttribute("password");\r
-\r
- if (password!=null) {\r
- String submittedPassword= aRequest.getParameter("password").trim();\r
-\r
- if (!password.equals(submittedPassword)) {\r
- aResults.add(new ValidationError("password", "passwordmismatch"));\r
- }\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Method to generate a one-time password\r
- *\r
- * @return a password, to be used once\r
- */\r
-\r
- protected String generateOnetimePassword() {\r
- Random r = new Random();\r
- int random = r.nextInt();\r
-\r
- long l = System.currentTimeMillis();\r
-\r
- l = (l*l*l*l)/random;\r
- if (l<0)\r
- l = l * -1;\r
-\r
- String returnString = ""+l;\r
-\r
- return returnString.substring(5);\r
- }\r
-\r
-\r
- /**\r
- *\r
- * @param aRequest\r
- * @param aStorage\r
- * @return\r
- * @throws SessionExc\r
- * @throws SessionFailure\r
- */\r
-\r
- protected static final Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionExc, SessionFailure {\r
- Map result = new HashMap();\r
-\r
- Iterator i = aStorage.getFields().iterator();\r
-\r
- while (i.hasNext()) {\r
- String fieldName = (String) i.next();\r
- Object value = aRequest.getParameter(fieldName);\r
- if (value != null)\r
- result.put(fieldName, value);\r
- }\r
-\r
- return result;\r
- }\r
-\r
- protected static class DuplicatePostingExc extends SessionExc {\r
- public DuplicatePostingExc(String aMessage) {\r
- super(aMessage);\r
- }\r
- }\r
-\r
-}\r
+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two. You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries. If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+package mircoders.localizer.basic;
+
+import mir.config.MirPropertiesConfiguration;
+import mir.log.LoggerWrapper;
+import mir.session.*;
+import mir.storage.Database;
+import mir.util.ExceptionRoutines;
+import mir.util.FileRoutines;
+import mir.util.IORoutines;
+import mircoders.global.MirGlobal;
+import mircoders.media.UnsupportedMediaTypeExc;
+
+import java.io.*;
+import java.util.*;
+
+/**
+ * Extensible handler for open postings.
+ * Behaviour can be altered by overriding methods.
+ */
+public abstract class MirBasicPostingSessionHandler implements SessionHandler {
+ protected static LoggerWrapper logger = new LoggerWrapper("Localizer.OpenPosting");
+ protected MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
+
+ /** Previously uploaded files */
+ protected List attachments;
+ /** counter to generate unique field names for uploaded files */
+ protected int uploadedFileIndex = 0;
+
+ private String normalResponseGenerator;
+ private String dupeResponseGenerator;
+ private String unsupportedMediaTypeResponseGenerator;
+ private String finalResponseGenerator;
+
+ private boolean persistentUploadedFiles;
+
+ public MirBasicPostingSessionHandler(boolean aPersistentUploadedFiles) {
+ attachments = new ArrayList();
+ persistentUploadedFiles = aPersistentUploadedFiles;
+ }
+
+ protected void setNormalResponseGenerator(String aGenerator) {
+ normalResponseGenerator = aGenerator;
+ }
+
+ protected void setResponseGenerators(String aNormalResponseGenerator,
+ String aDupeResponseGenerator, String anUnsupportedMediaTypeResponseGenerator,
+ String aFinalResponseGenerator) {
+ setNormalResponseGenerator(aNormalResponseGenerator);
+ dupeResponseGenerator = aDupeResponseGenerator;
+ unsupportedMediaTypeResponseGenerator = anUnsupportedMediaTypeResponseGenerator;
+ finalResponseGenerator = aFinalResponseGenerator;
+ }
+
+ public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+ if (MirGlobal.abuse().getOpenPostingDisabled()) {
+ makeOpenPostingDisabledResponse(aRequest, aSession, aResponse);
+ aSession.terminate();
+ }
+ else {
+ if (aSession.getAttribute("initialRequest") == null) {
+ initialRequest(aRequest, aSession, aResponse);
+ aSession.setAttribute("initialRequest", "no");
+ }
+ else {
+ subsequentRequest(aRequest, aSession, aResponse);
+ }
+ }
+ }
+
+ protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+ initializeSession(aRequest, aSession);
+ initializeResponseData(aRequest, aSession, aResponse);
+ makeInitialResponse(aRequest, aSession, aResponse);
+ }
+
+ protected void processAttachments(Request aRequest, Session aSession, Response aResponse) {
+ Iterator i = attachments.iterator();
+ while (i.hasNext()) {
+ Attachment attachment = (Attachment) i.next();
+ try{
+ processAttachment(aRequest, aSession, attachment);
+ }
+ catch (Throwable t) {
+ try {
+ processAttachmentError(aRequest, aSession, attachment, t);
+ }
+ catch (Throwable u) {
+ }
+ logger.error("Error while processing attachment", t);
+ }
+ }
+ }
+
+ public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+ try {
+ try {
+ List validationErrors = new ArrayList();
+
+ preprocessPreviousAttachments(aRequest, aSession);
+ preProcessNewAttachments(aRequest, aSession);
+
+ if (!shouldProcessRequest(aRequest, aSession, validationErrors)) {
+ initializeResponseData(aRequest, aSession, aResponse);
+ makeResponse(aRequest, aSession, aResponse, validationErrors);
+ }
+ else {
+ preProcessRequest(aRequest, aSession);
+
+ processAttachments(aRequest, aSession, aResponse);
+ postProcessRequest(aRequest, aSession);
+ initializeResponseData(aRequest, aSession, aResponse);
+ makeFinalResponse(aRequest, aSession, aResponse);
+ aSession.terminate();
+ }
+ }
+ catch (Throwable t) {
+ initializeResponseData(aRequest, aSession, aResponse);
+ makeErrorResponse(aRequest, aSession, aResponse, t);
+ aSession.terminate();
+ }
+ }
+ catch (Throwable t) {
+ aSession.terminate();
+
+ throw new SessionFailure(t);
+ }
+ }
+
+ /**
+ * Initializes a session.
+ * This may happen in the case of a new session being initiated, but also
+ * when an older session gets re-initiated after a session timeout.
+ */
+ protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
+ if (MirGlobal.abuse().getOpenPostingPassword()) {
+ String password = (String) aSession.getAttribute("password");
+ if (password==null) {
+ password = generateOnetimePassword();
+ aSession.setAttribute("password", password);
+ }
+ }
+ else {
+ aSession.deleteAttribute("password");
+ }
+
+ aSession.setAttribute("referer", aRequest.getHeader("Referer"));
+ }
+
+ /**
+ * Called every time a response is being prepared.
+ * This may be at the initial response, or on a subsequent one.
+ */
+ protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+ int nrMediaItems = configuration.getInt("ServletModule.OpenIndy.DefaultMediaUploadItems", 5);
+
+ if (aSession.getAttribute("nrmediaitems")!=null) {
+ nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue();
+ }
+
+ try {
+ nrMediaItems = Math.min(configuration.getInt("ServletModule.OpenIndy.MaxMediaUploadItems"), Integer.parseInt(aRequest.getParameter("nrmediaitems")));
+ }
+ catch (Throwable t) {
+ }
+
+ aSession.setAttribute("nrmediaitems", new Integer(nrMediaItems));
+
+ List mediaItems = new ArrayList();
+ for (int i=1; i<=nrMediaItems; i++) {
+ mediaItems.add(new Integer(i));
+ }
+
+ aResponse.setResponseValue("nrmediaitems", new Integer(nrMediaItems));
+ aResponse.setResponseValue("mediaitems", mediaItems);
+ aResponse.setResponseValue("password", aSession.getAttribute("password"));
+ aResponse.setResponseValue("referer", aSession.getAttribute("referer"));
+ aResponse.setResponseValue("errors", null);
+
+ if (configuration.getBoolean("Localizer.OpenSession.AllowFTPUploads", false)) {
+ aResponse.setResponseValue("ftpfiles",
+ FileRoutines.getDirectoryContentsAsList(configuration.getFile("Localizer.OpenSession.FTPDirectory"),
+ new FilenameFilter() {
+ public boolean accept(File aDir, String aName) {
+ return !(new File(aDir, aName).isDirectory());
+ }
+ }));
+ }
+ else {
+ aResponse.setResponseValue("ftpfiles", null);
+ }
+
+ initializeAttachmentResponseData(aRequest, aSession, aResponse);
+ }
+
+ /**
+ * Process possible changes to previously uploaded files
+ */
+ protected void initializeAttachmentResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+ List result = new ArrayList();
+ if (persistentUploadedFiles) {
+ Iterator i = attachments.iterator();
+ while (i.hasNext()) {
+ Attachment attachment = (Attachment) i.next();
+ Map attachmentData = new HashMap();
+ attachmentData.putAll(attachment.getAllAttributes());
+ attachmentData.put("fieldname", attachment.getFieldName());
+ attachmentData.put("filename", attachment.getFileName());
+ result.add(attachmentData);
+ }
+ }
+
+ aResponse.setResponseValue("attachments", result);
+ }
+
+ /**
+ * Process possible changes to previously uploaded files
+ */
+ protected void preprocessPreviousAttachments(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
+ synchronized (attachments) {
+ List previouslyUploadedFiles = new ArrayList(attachments);
+ attachments.clear();
+
+ if (persistentUploadedFiles) {
+ Iterator i = previouslyUploadedFiles.iterator();
+ while (i.hasNext()) {
+ Attachment uploadedFile = (Attachment) i.next();
+ if (!(aRequest.getParameter(uploadedFile.getFieldName()+"_cancel")!=null)) {
+ addAttachment(aRequest, aSession, uploadedFile, uploadedFile.getFieldName());
+ }
+ }
+ }
+ }
+ }
+
+ protected void addAttachment(Request aRequest, Session aSession, Attachment anAttachment, String aFieldName) throws SessionExc, SessionFailure {
+ List parameters = aRequest.getPrefixedParameterNames(aFieldName+"_");
+ Iterator j = parameters.iterator();
+ while (j.hasNext()) {
+ String parameter = ((String) j.next());
+ anAttachment.setAttribute(
+ parameter.substring(aFieldName.length()+1),
+ aRequest.getParameter(parameter));
+ }
+ attachments.add(anAttachment);
+ }
+
+ public void preprocessNewAttachment(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
+ Attachment uploadedFile =
+ new Attachment(aFile, aFile.getFileName(), "attachment"+ ++uploadedFileIndex, aFile.getContentType());
+
+ addAttachment(aRequest, aSession, uploadedFile, aFile.getFieldName());
+ }
+
+
+ /**
+ * Process newly uploaded files
+ */
+ protected void preProcessNewAttachments(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
+ Iterator i = aRequest.getUploadedFiles().iterator();
+ while (i.hasNext()) {
+ preprocessNewAttachment(aRequest, aSession, (UploadedFile) i.next());
+ }
+
+ if (configuration.getBoolean("Localizer.OpenSession.AllowFTPUploads", false)) {
+ File FTPDirectory = configuration.getFile("Localizer.OpenSession.FTPDirectory");
+
+ List ftpUploads = new ArrayList(aRequest.getPrefixedParameterNames("ftpupload"));
+ Collections.sort(ftpUploads, new Comparator() {
+ public int compare(Object o1, Object o2) {
+ if (o1 instanceof String && o2 instanceof String) {
+ return ((String) o1).compareTo((String) o2);
+ }
+ else {
+ return 0;
+ }
+ }
+ });
+
+ i = ftpUploads.iterator();
+ while (i.hasNext()) {
+ final String fieldName = (String) i.next();
+
+ if (fieldName.indexOf("_")<0) {
+ final String fileName = aRequest.getParameter(fieldName);
+
+ if (fileName!=null && fileName.trim().length()>0) {
+ final File sourceFile = new File(FTPDirectory, fileName);
+
+ if (sourceFile.getParentFile().equals(FTPDirectory)) {
+ preprocessNewAttachment(aRequest, aSession, new UploadedFile() {
+ public void writeToFile(File aFile) throws SessionFailure {
+ try {
+ FileRoutines.move(sourceFile, aFile);
+ }
+ catch (IOException e) {
+ throw new SessionFailure(e);
+ }
+ }
+
+ public void writeToStream(OutputStream aStream) throws SessionFailure {
+ try {
+ IORoutines.copyStream(getInputStream(), aStream);
+ }
+ catch (IOException e) {
+ throw new SessionFailure(e);
+ }
+ }
+
+ public InputStream getInputStream() throws SessionFailure {
+ try {
+ return new FileInputStream(sourceFile);
+ }
+ catch (IOException e) {
+ throw new SessionFailure(e);
+ }
+ }
+
+ public String getFileName() {
+ return fileName;
+ }
+
+ public String getFieldName() {
+ return fieldName;
+ }
+
+ public String getContentType() {
+ return null;
+ }
+ });
+ }
+ }
+ }
+ }
+ }
+ }
+
+ protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+ aResponse.setResponseGenerator(normalResponseGenerator);
+ }
+
+ protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure {
+ aResponse.setResponseValue("errors", anErrors);
+ aResponse.setResponseGenerator(normalResponseGenerator);
+ }
+
+ protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+ aResponse.setResponseGenerator(finalResponseGenerator);
+ }
+
+ protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure {
+ Throwable rootCause = ExceptionRoutines.traceCauseException(anError);
+
+ if (rootCause instanceof DuplicatePostingExc)
+ aResponse.setResponseGenerator(dupeResponseGenerator);
+ if (rootCause instanceof UnsupportedMediaTypeExc) {
+ aResponse.setResponseValue("mimetype", ((UnsupportedMediaTypeExc) rootCause).getMimeType());
+ aResponse.setResponseGenerator(unsupportedMediaTypeResponseGenerator);
+ }
+ else {
+ List errors = new ArrayList();
+ errors.add(new ValidationError("", "general.unexpectederror",
+ new Object[] {anError.getMessage()}));
+ makeResponse(aRequest, aSession, aResponse, errors);
+ }
+ }
+
+ protected void makeOpenPostingDisabledResponse(Request aRequest, Session aSession, Response aResponse) {
+ aResponse.setResponseGenerator(configuration.getString("ServletModule.OpenIndy.PostingDisabledTemplate"));
+ }
+
+ /**
+ *
+ */
+ protected void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
+ }
+ public void processAttachment(Request aRequest, Session aSession, Attachment aFile) throws SessionExc, SessionFailure {
+ }
+ public void processAttachmentError(Request aRequest, Session aSession, Attachment aFile, Throwable anError) {
+ }
+ protected void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
+ }
+
+ /**
+ * Determine whether the request shoudl be processed: that is, the validate,
+ * preprocess, postprocess methods should be called to perform validations,
+ * store data, etc
+ */
+ protected boolean shouldProcessRequest(Request aRequest, Session aSession, List aValidationErrors) throws SessionExc, SessionFailure {
+ if (aRequest.getParameter("post")==null)
+ return false;
+ validate(aValidationErrors, aRequest, aSession);
+ return (aValidationErrors == null || aValidationErrors.size() == 0);
+ }
+
+ /**
+ * Method used to validate user input.
+ * Multiple {@link ValidationError}s may be added to the
+ * <code>aResults</code> parameter.
+ * The request is considered validated if, after calling this method,
+ * <code>aResults</code> is empty.
+ */
+ protected void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure {
+ String password = (String) aSession.getAttribute("password");
+
+ if (password!=null) {
+ String submittedPassword= aRequest.getParameter("password").trim();
+
+ if (!password.equals(submittedPassword)) {
+ aResults.add(new ValidationError("password", "passwordmismatch"));
+ }
+ }
+ }
+
+
+ /**
+ * Method to generate a one-time password
+ *
+ * @return a password, to be used once
+ */
+ protected String generateOnetimePassword() {
+ Random r = new Random();
+ int random = r.nextInt();
+
+ long l = System.currentTimeMillis();
+
+ l = (l*l*l*l)/random;
+ if (l<0)
+ l = l * -1;
+
+ String returnString = ""+l;
+
+ return returnString.substring(5);
+ }
+
+ /**
+ * Method to filter the attributes and their values of a request
+ * based on the fields of a database object.
+ */
+ protected static final Map getIntersectingValues(Request aRequest, Database aStorage) throws SessionFailure {
+ Map result = new HashMap();
+
+ Iterator i = aStorage.getFieldNames().iterator();
+
+ while (i.hasNext()) {
+ String fieldName = (String) i.next();
+ Object value = aRequest.getParameter(fieldName);
+ if (value != null)
+ result.put(fieldName, value);
+ }
+
+ return result;
+ }
+
+ /**
+ * Exception to be thrown when an article or comment was already posted
+ */
+ protected static class DuplicatePostingExc extends SessionExc {
+ public DuplicatePostingExc(String aMessage) {
+ super(aMessage);
+ }
+ }
+
+ /**
+ * A file that has been attached to a session
+ */
+ protected static class Attachment implements UploadedFile {
+ private UploadedFile uploadedFile;
+ private String filename;
+ private String fieldName;
+ private String contentType;
+ private Map attributes;
+
+ public Attachment(UploadedFile anUploadedFile, String aFilename, String aFieldName, String aContentType) {
+ attributes = new HashMap();
+ filename = aFilename;
+ fieldName = aFieldName;
+ contentType = aContentType;
+ uploadedFile = anUploadedFile;
+ }
+
+ public void writeToFile(File aFile) throws SessionExc, SessionFailure {
+ uploadedFile.writeToFile(aFile);
+ }
+
+ public void writeToStream(OutputStream aStream) throws SessionExc, SessionFailure {
+ try {
+ IORoutines.copyStream(uploadedFile.getInputStream(), aStream);
+ }
+ catch (IOException e) {
+ throw new SessionFailure(e);
+ }
+ }
+
+ public InputStream getInputStream() throws SessionExc, SessionFailure {
+ return uploadedFile.getInputStream();
+ }
+
+ public String getFileName() {
+ return filename;
+ }
+
+ public String getFieldName() {
+ return fieldName;
+ }
+
+ public String getContentType() {
+ return contentType;
+ }
+
+ public String getAttribute(String anAttribute) {
+ return (String) attributes.get(anAttribute);
+ }
+
+ public void setAttribute(String anAttribute, String aValue) {
+ attributes.put(anAttribute, aValue);
+ }
+
+ public Map getAllAttributes() {
+ return Collections.unmodifiableMap(attributes);
+ }
+ }
+}
+