media handling fixes, gotten rid of StorageObject, set the default method for blots...
[mir.git] / source / mircoders / localizer / basic / MirBasicPostingSessionHandler.java
index 921eb09..75e7978 100755 (executable)
  * 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.  
+ * 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 java.util.Arrays;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
-import java.util.Vector;
 
 import mir.config.MirPropertiesConfiguration;
-import mir.entity.Entity;
 import mir.log.LoggerWrapper;
 import mir.session.Request;
 import mir.session.Response;
@@ -47,324 +51,490 @@ import mir.session.SessionExc;
 import mir.session.SessionFailure;
 import mir.session.SessionHandler;
 import mir.session.UploadedFile;
-import mir.storage.StorageObject;
+import mir.session.ValidationError;
+import mir.storage.Database;
 import mir.util.ExceptionFunctions;
-import mircoders.entity.EntityComment;
+import mir.util.FileFunctions;
 import mircoders.global.MirGlobal;
-import mircoders.media.MediaUploadProcessor;
-import mircoders.module.ModuleComment;
-import mircoders.storage.DatabaseComment;
-import mircoders.storage.DatabaseCommentToMedia;
-import mircoders.storage.DatabaseContent;
+import mircoders.media.UnsupportedMediaTypeExc;
 
 /**
- *
- * <p>Title: Experimental session handler for comment postings </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
+ * 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();;
 
-public class MirBasicPostingSessionHandler implements SessionHandler {
-  protected LoggerWrapper logger;
-  protected MirPropertiesConfiguration configuration;
-  protected ModuleComment commentModule;
-  protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance();
+  /** Previously uploaded files */
+  protected List attachments;
+  /** counter to generate unique field names for uploaded files */
+  protected int uploadedFileIndex = 0;
 
-  public MirBasicPostingSessionHandler() {
-    logger = new LoggerWrapper("Localizer.OpenPosting");
-    try {
-      configuration = MirPropertiesConfiguration.instance();
-    }
-    catch (Throwable t) {
-      logger.fatal("Cannot load configuration: " + t.toString());
+  private String normalResponseGenerator;
+  private String dupeResponseGenerator;
+  private String unsupportedMediaTypeResponseGenerator;
+  private String finalResponseGenerator;
 
-      throw new RuntimeException("Cannot load configuration: " + t.toString());
-    }
-    commentModule= new ModuleComment(DatabaseComment.getInstance());
+  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 (aSession.getAttribute("initialRequest")==null) {
-      initialRequest(aRequest, aSession, aResponse);
-      aSession.setAttribute("initialRequest", "no");
+    if (MirGlobal.abuse().getOpenPostingDisabled()) {
+      makeOpenPostingDisabledResponse(aRequest, aSession, aResponse);
+      aSession.terminate();
     }
     else {
-      subsequentRequest(aRequest, aSession, aResponse);
+      if (aSession.getAttribute("initialRequest") == null) {
+        initialRequest(aRequest, aSession, aResponse);
+        aSession.setAttribute("initialRequest", "no");
+      }
+      else {
+        subsequentRequest(aRequest, aSession, aResponse);
+      }
     }
   };
 
-  protected Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionExc, SessionFailure {
-    Map result = new HashMap();
-
-    Iterator i = aStorage.getFields().iterator();
+  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()) {
-      String fieldName = (String) i.next();
-      Object value = aRequest.getParameter(fieldName);
-      if (value != null)
-        result.put(fieldName, value);
+      Attachment attachment = (Attachment) i.next();
+      try{
+        processAttachment(aRequest, aSession, attachment);
+      }
+      catch (Throwable t) {
+        try {
+          processAttachmentError(aRequest, aSession, attachment, t);
+        }
+        catch (Throwable u) {
+        }
+      }
     }
-
-    return result;
   }
 
+  public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+    try {
+      try {
+        List validationErrors = new ArrayList();
 
-  protected String generateOnetimePassword() {
-    Random r = new Random();
-    int random = r.nextInt();
-
-    long l = System.currentTimeMillis();
+        preprocessPreviousAttachments(aRequest, aSession);
+        preProcessNewAttachments(aRequest, aSession);
 
-    l = (l*l*l*l)/random;
-    if (l<0)
-      l = l * -1;
+        if (!shouldProcessRequest(aRequest, aSession, validationErrors)) {
+          initializeResponseData(aRequest, aSession, aResponse);
+          makeResponse(aRequest, aSession, aResponse, validationErrors);
+        }
+        else {
+          preProcessRequest(aRequest, aSession);
 
-    String returnString = ""+l;
+          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();
 
-    return returnString.substring(5);
+      throw new SessionFailure(t);
+    }
   }
 
-  protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+  /**
+   * 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);
       }
-      aResponse.setResponseValue("password", password);
     }
     else {
-      aResponse.setResponseValue("password", null);
       aSession.deleteAttribute("password");
     }
 
-    aResponse.setResponseValue("errors", null);
-  };
+    aSession.setAttribute("referer", aRequest.getHeader("Referer"));
+  }
 
-  protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure{
-    Iterator i = DatabaseComment.getInstance().getFields().iterator();
-    while (i.hasNext()) {
-      aResponse.setResponseValue( (String) i.next(), null);
+  /**
+   * 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();
     }
 
-    String articleId = aRequest.getParameter("to_media");
+    try {
+      nrMediaItems = Math.min(configuration.getInt("ServletModule.OpenIndy.MaxMediaUploadItems"), Integer.parseInt(aRequest.getParameter("nrmediaitems")));
+    }
+    catch (Throwable t) {
+    }
 
-    if (articleId == null)
-      throw new SessionExc("MirBasicPostingSessionHandler.initialRequest: article id not set!");
+    aSession.setAttribute("nrmediaitems", new Integer(nrMediaItems));
 
-    aSession.setAttribute("to_media", articleId);
+    List mediaItems = new ArrayList();
+    for (int i=1; i<=nrMediaItems; i++) {
+      mediaItems.add(new Integer(i));
+    }
 
-    initializeResponseData(aRequest, aSession, aResponse);
+    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);
 
-    try {
-      aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
+    if (configuration.getBoolean("Localizer.OpenSession.AllowFTPUploads", false)) {
+      aResponse.setResponseValue("ftpfiles",
+          FileFunctions.getDirectoryContentsAsList(configuration.getFile("Localizer.OpenSession.FTPDirectory"),
+              new FilenameFilter() {
+                public boolean accept(File aDir, String aName) {
+                  return !(new File(aDir, aName).isDirectory());
+                }
+          }));
     }
-    catch (Throwable e) {
-      throw new SessionFailure("Can't get configuration: " + e.getMessage(), e);
+    else {
+      aResponse.setResponseValue("ftpfiles", null);
     }
 
+    initializeAttachmentResponseData(aRequest, aSession, aResponse);
   }
 
-  protected boolean testFieldEntered(Request aRequest, String aFieldName, String anErrorMessageResource, List aValidationResults) {
-    Object value = aRequest.getParameter(aFieldName);
-    if (value==null || !(value instanceof String) || ((String) value).trim().length()==0) {
-      logger.debug("  missing field " + aFieldName + " value = " + value);
-      aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
-      return false;
+  /**
+   * 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);
+      }
     }
-    else
-      return true;
+
+    aResponse.setResponseValue("attachments", result);
   }
 
-  protected boolean testFieldIsNumeric(Request aRequest, String aFieldName, String anErrorMessageResource, List aValidationResults) {
-    Object value = aRequest.getParameter(aFieldName);
-    if (value!=null) {
-      try {
-        Integer.parseInt((String) value);
-        return true;
-      }
-      catch (Throwable t) {
-        logger.debug("  field not numeric: " + aFieldName + " value = " + value);
-        aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource));
-        return false;
+  /**
+   * 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());
+          }
+        }
       }
     }
-    return true;
   }
 
-  public void validate(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
-
+  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 List validate(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
-    List result = new Vector();
-
-    testFieldEntered(aRequest, "title", "validationerror.missing", result);
-    testFieldEntered(aRequest, "description", "validationerror.missing", result);
-    testFieldEntered(aRequest, "creator", "validationerror.missing", result);
+  public void preprocessNewAttachment(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure {
+      Attachment uploadedFile =
+          new Attachment(aFile, aFile.getFileName(), "attachment"+ ++uploadedFileIndex, aFile.getContentType());
 
-    return result;
+      addAttachment(aRequest, aSession, uploadedFile, aFile.getFieldName());
   }
 
-  public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
-    try {
-      Map commentFields = new HashMap();
-
-      Iterator i = DatabaseContent.getInstance().getFields().iterator();
-      while (i.hasNext()) {
-        String field = (String) i.next();
-        aResponse.setResponseValue(field, aRequest.getParameter(field));
-        if (aRequest.getParameter(field)!=null) {
-          commentFields.put(field, aRequest.getParameter(field));
-        }
-      }
-
-      initializeResponseData(aRequest, aSession, aResponse);
 
-      List validationErrors = validate(aRequest, aSession);
-
-      if (validationErrors != null && validationErrors.size()>0) {
-        returnValidationErrors(aRequest, aSession, aResponse, validationErrors);
-      }
-      else {
-//        finish(aRequest, aSession, aResponse);
-
-        EntityComment comment = (EntityComment) commentModule.createNew ();
-//        comment.setValues(getIntersectingValues(aRequest, ));
+  /**
+   * 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());
+    }
 
-        finishComment(aRequest, aSession, comment);
+    if (configuration.getBoolean("Localizer.OpenSession.AllowFTPUploads", false)) {
+      File FTPDirectory = configuration.getFile("Localizer.OpenSession.FTPDirectory");
 
-        String id = comment.insert();
-        if(id==null){
-          afterDuplicateCommentPosting(aRequest, aSession, aResponse, comment);
-          logger.info("Dupe comment rejected");
-          aSession.terminate();
-        }
-        else {
-          i = aRequest.getUploadedFiles().iterator();
-          while (i.hasNext()) {
-            UploadedFile file = (UploadedFile) i.next();
-            processMediaFile(aRequest, aSession, comment, file);
+      List ftpUploads = aRequest.getPrefixedParameterNames("ftpupload");
+      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 {
+                    FileFunctions.move(sourceFile, aFile);
+                  }
+                  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;
+                }
+              });
+            }
           }
-
-          afterCommentPosting(aRequest, aSession, aResponse, comment);
-          MirGlobal.abuse().checkComment(comment, aRequest, null);
-          MirGlobal.localizer().openPostings().afterCommentPosting(comment);
-          logger.info("Comment posted");
-          aSession.terminate();
         }
       }
     }
-    catch (Throwable t) {
-      ExceptionFunctions.traceCauseException(t).printStackTrace();
-
-      throw new SessionFailure("MirBasicPostingSessionHandler.subsequentRequest: " + t.getMessage(), t);
-    }
   }
 
-  public void initializeCommentPosting(Request aRequest, Session aSession, Response aResponse) throws SessionFailure, SessionExc {
-    String articleId = aRequest.getParameter("to_media");
-    if (articleId==null)
-      articleId = aRequest.getParameter("aid");
-
-    if (articleId==null)
-      throw new SessionExc("initializeCommentPosting: article id not set!");
+  protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+    aResponse.setResponseGenerator(normalResponseGenerator);
+  };
 
-    aSession.setAttribute("to_media", articleId);
-    processCommentPosting(aRequest, aSession, aResponse);
+  protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure {
+    aResponse.setResponseValue("errors", anErrors);
+    aResponse.setResponseGenerator(normalResponseGenerator);
   };
 
-  public void returnValidationErrors(Request aRequest, Session aSession, Response aResponse, List aValidationErrors) throws SessionFailure, SessionExc {
-    aResponse.setResponseValue("errors", aValidationErrors);
-    aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
+  protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+    aResponse.setResponseGenerator(finalResponseGenerator);
   };
 
-  public void processCommentPosting(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
-    if (MirGlobal.abuse().getOpenPostingPassword()) {
-      String password = generateOnetimePassword();
-      aSession.setAttribute("password", password);
-      aResponse.setResponseValue("password", password);
-      aResponse.setResponseValue("passwd", password);
+  protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure {
+    Throwable rootCause = ExceptionFunctions.traceCauseException(anError);
+
+    if (rootCause instanceof DuplicatePostingExc)
+      aResponse.setResponseGenerator(dupeResponseGenerator);
+    if (rootCause instanceof UnsupportedMediaTypeExc) {
+      aResponse.setResponseValue("mimetype", ((UnsupportedMediaTypeExc) rootCause).getMimeType());
+      aResponse.setResponseGenerator(unsupportedMediaTypeResponseGenerator);
     }
     else {
-      aResponse.setResponseValue("password", null);
+      List errors = new ArrayList();
+      errors.add(new ValidationError("", "general.unexpectederror",
+          new Object[] {anError.getMessage()}));
+      makeResponse(aRequest, aSession, aResponse, errors);
     }
+  };
 
-    aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate"));
+  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 {
   };
 
-  public void processMediaFile(Request aRequest, Session aSession, EntityComment aComment, UploadedFile aFile) throws SessionExc, SessionFailure {
-    try {
-      Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, new HashMap());
-      finishMedia(aRequest, aSession, aFile, mediaItem);
-      mediaItem.update();
-      commentToMedia.addMedia(aComment.getId(), mediaItem.getId());
-    }
-    catch (Throwable t) {
-      throw new SessionFailure(t);
+  /**
+   * 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;
+    else {
+      validate(aValidationErrors, aRequest, aSession);
+      return (aValidationErrors == null || aValidationErrors.size() == 0);
     }
   }
 
-  public void finishMedia(Request aRequest, Session aSession, UploadedFile aFile, Entity aMedia) throws SessionExc, SessionFailure {
+  /**
+   * 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"));
+      }
+    }
   }
 
-  public void finishComment(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure {
-    if (aSession.getAttribute("to_media") == null)
-      throw new SessionExc("missing to_media");
 
-    aComment.setValueForProperty("is_published", "1");
-    aComment.setValueForProperty("to_comment_status", "1");
-    aComment.setValueForProperty("is_html","0");
-    aComment.setValueForProperty("to_media", (String) aSession.getAttribute("to_media"));
-  };
+  /**
+   * 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();
 
-  public void addMedia(Request aRequest, Session aSession, EntityComment aComment)  throws SessionExc, SessionFailure {
+    l = (l*l*l*l)/random;
+    if (l<0)
+      l = l * -1;
+
+    String returnString = ""+l;
+
+    return returnString.substring(5);
   }
 
-  public void afterCommentPosting(Request aRequest, Session aSession, Response aResponse, EntityComment aComment) {
-    DatabaseContent.getInstance().setUnproduced("id=" + aComment.getValue("to_media"));
-    aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DoneTemplate"));
-  };
+  /**
+   * 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();
 
-  public void afterDuplicateCommentPosting(Request aRequest, Session aSession, Response aResponse, EntityComment aComment) {
-    aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DupeTemplate"));
-  };
+    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);
+    }
 
-  public class ValidationError {
-    private String field;
-    private String message;
-    private List parameters;
+    return result;
+  }
 
-    public ValidationError(String aField, String aMessage) {
-      this (aField, aMessage, new String[] {});
+  /**
+   * Exception to be thrown when an article or comment was already posted
+   */
+  protected static class DuplicatePostingExc extends SessionExc {
+    public DuplicatePostingExc(String aMessage) {
+      super(aMessage);
     }
+  }
 
-    public ValidationError(String aField, String aMessage, Object aParameter) {
-      this (aField, aMessage, new Object[] {aParameter});
+  /**
+   * 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 ValidationError(String aField, String aMessage, Object[] aParameters) {
-      field = aField;
-      message = aMessage;
-      parameters = Arrays.asList(aParameters);
+    public void writeToFile(File aFile) throws SessionExc, SessionFailure {
+      uploadedFile.writeToFile(aFile);
     }
 
-    public String getMessage() {
-      return message;
+    public InputStream getInputStream() throws SessionExc, SessionFailure {
+      return uploadedFile.getInputStream();
     }
 
-    public String getField() {
-      return field;
+    public String getFileName() {
+      return filename;
     }
 
-    public List getParameters() {
-      return parameters;
+    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);
+    }
+  }
 }
+