merged 1.1 branch into head
[mir.git] / source / mircoders / localizer / basic / MirBasicPostingSessionHandler.java
index 1371c79..31822f5 100755 (executable)
@@ -32,11 +32,12 @@ package mircoders.localizer.basic;
 import mir.config.MirPropertiesConfiguration;
 import mir.log.LoggerWrapper;
 import mir.session.*;
-import mir.storage.StorageObject;
-import mir.util.ExceptionFunctions;
-import mir.util.FileFunctions;
+import mir.storage.Database;
+import mir.util.ExceptionRoutines;
+import mir.util.FileRoutines;
+import mir.util.IORoutines;
 import mircoders.global.MirGlobal;
-import mircoders.module.ModuleMediaType;
+import mircoders.media.UnsupportedMediaTypeExc;
 
 import java.io.*;
 import java.util.*;
@@ -47,7 +48,7 @@ import java.util.*;
  */
 public abstract class MirBasicPostingSessionHandler implements SessionHandler {
   protected static LoggerWrapper logger = new LoggerWrapper("Localizer.OpenPosting");
-  protected MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();;
+  protected MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
 
   /** Previously uploaded files */
   protected List attachments;
@@ -93,7 +94,7 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
         subsequentRequest(aRequest, aSession, aResponse);
       }
     }
-  };
+  }
 
   protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
     initializeSession(aRequest, aSession);
@@ -101,10 +102,21 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
     makeInitialResponse(aRequest, aSession, aResponse);
   }
 
-  protected void processAttachments(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
+  protected void processAttachments(Request aRequest, Session aSession, Response aResponse) {
     Iterator i = attachments.iterator();
     while (i.hasNext()) {
-      processAttachment(aRequest, aSession, (Attachment) i.next());
+      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);
+      }
     }
   }
 
@@ -124,7 +136,6 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
           preProcessRequest(aRequest, aSession);
 
           processAttachments(aRequest, aSession, aResponse);
-
           postProcessRequest(aRequest, aSession);
           initializeResponseData(aRequest, aSession, aResponse);
           makeFinalResponse(aRequest, aSession, aResponse);
@@ -196,7 +207,7 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
 
     if (configuration.getBoolean("Localizer.OpenSession.AllowFTPUploads", false)) {
       aResponse.setResponseValue("ftpfiles",
-          FileFunctions.getDirectoryContentsAsList(configuration.getFile("Localizer.OpenSession.FTPDirectory"),
+          FileRoutines.getDirectoryContentsAsList(configuration.getFile("Localizer.OpenSession.FTPDirectory"),
               new FilenameFilter() {
                 public boolean accept(File aDir, String aName) {
                   return !(new File(aDir, aName).isDirectory());
@@ -282,7 +293,18 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
     if (configuration.getBoolean("Localizer.OpenSession.AllowFTPUploads", false)) {
       File FTPDirectory = configuration.getFile("Localizer.OpenSession.FTPDirectory");
 
-      List ftpUploads = aRequest.getPrefixedParameterNames("ftpupload");
+      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();
@@ -297,7 +319,16 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
               preprocessNewAttachment(aRequest, aSession, new UploadedFile() {
                 public void writeToFile(File aFile) throws SessionFailure {
                   try {
-                    FileFunctions.move(sourceFile, aFile);
+                    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);
@@ -334,24 +365,24 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
 
   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 = ExceptionFunctions.traceCauseException(anError);
+    Throwable rootCause = ExceptionRoutines.traceCauseException(anError);
 
     if (rootCause instanceof DuplicatePostingExc)
       aResponse.setResponseGenerator(dupeResponseGenerator);
-    if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) {
-      aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType());
+    if (rootCause instanceof UnsupportedMediaTypeExc) {
+      aResponse.setResponseValue("mimetype", ((UnsupportedMediaTypeExc) rootCause).getMimeType());
       aResponse.setResponseGenerator(unsupportedMediaTypeResponseGenerator);
     }
     else {
@@ -360,7 +391,7 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
           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"));
@@ -370,11 +401,13 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
    *
    */
   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,
@@ -384,10 +417,8 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
   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);
-    }
+               validate(aValidationErrors, aRequest, aSession);
+               return (aValidationErrors == null || aValidationErrors.size() == 0);
   }
 
   /**
@@ -432,12 +463,12 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
 
   /**
    * Method to filter the attributes and their values of a request
-   * based on the fields of a storage object.
+   * based on the fields of a database object.
    */
-  protected static final Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionFailure {
+  protected static final Map getIntersectingValues(Request aRequest, Database aStorage) throws SessionFailure {
     Map result = new HashMap();
 
-    Iterator i = aStorage.getFields().iterator();
+    Iterator i = aStorage.getFieldNames().iterator();
 
     while (i.hasNext()) {
       String fieldName = (String) i.next();
@@ -480,6 +511,15 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler {
       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();
     }