Add javadoc.
[jSite.git] / src / de / todesbaum / jsite / application / ProjectInserter.java
index 569e2fe..e52587d 100644 (file)
@@ -27,9 +27,14 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.zip.ZipEntry;
@@ -48,6 +53,7 @@ import de.todesbaum.util.freenet.fcp2.Verbosity;
 import de.todesbaum.util.io.Closer;
 import de.todesbaum.util.io.ReplacingOutputStream;
 import de.todesbaum.util.io.StreamCopier;
+import de.todesbaum.util.io.StreamCopier.ProgressListener;
 
 /**
  * Manages project inserts.
@@ -83,6 +89,15 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /** The temp directory. */
        private String tempDirectory;
 
+       /** The current connection. */
+       private Connection connection;
+
+       /** Whether the insert is cancelled. */
+       private volatile boolean cancelled = false;
+
+       /** Progress listener for payload transfers. */
+       private ProgressListener progressListener;
+
        /**
         * Adds a listener to the list of registered listeners.
         *
@@ -210,14 +225,31 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Starts the insert.
+        *
+        * @param progressListener
+        *            Listener to notify on progress events
         */
-       public void start() {
+       public void start(ProgressListener progressListener) {
+               cancelled = false;
+               this.progressListener = progressListener;
                fileScanner = new FileScanner(project);
                fileScanner.addFileScannerListener(this);
                new Thread(fileScanner).start();
        }
 
        /**
+        * Stops the current insert.
+        */
+       public void stop() {
+               cancelled = true;
+               synchronized (lockObject) {
+                       if (connection != null) {
+                               connection.disconnect();
+                       }
+               }
+       }
+
+       /**
         * Creates an input stream that delivers the given file, replacing edition
         * tokens in the file’s content, if necessary.
         *
@@ -380,6 +412,76 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        }
 
        /**
+        * Validates the given project. The project will be checked for any invalid
+        * conditions, such as invalid insert or request keys, missing path names,
+        * missing default file, and so on.
+        *
+        * @param project
+        *            The project to check
+        * @return The encountered warnings and errors
+        */
+       public static CheckReport validateProject(Project project) {
+               CheckReport checkReport = new CheckReport();
+               if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
+                       checkReport.addIssue("error.no-local-path", true);
+               }
+               if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
+                       checkReport.addIssue("error.no-path", true);
+               }
+               if ((project.getIndexFile() == null) || (project.getIndexFile().length() == 0)) {
+                       checkReport.addIssue("warning.empty-index", false);
+               } else {
+                       File indexFile = new File(project.getLocalPath(), project.getIndexFile());
+                       if (!indexFile.exists()) {
+                               checkReport.addIssue("error.index-missing", true);
+                       }
+               }
+               String indexFile = project.getIndexFile();
+               boolean hasIndexFile = (indexFile != null) && (indexFile.length() > 0);
+               if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
+                       checkReport.addIssue("warning.container-index", false);
+               }
+               List<String> allowedIndexContentTypes = Arrays.asList("text/html", "application/xhtml+xml");
+               if (hasIndexFile && !allowedIndexContentTypes.contains(project.getFileOption(indexFile).getMimeType())) {
+                       checkReport.addIssue("warning.index-not-html", false);
+               }
+               Map<String, FileOption> fileOptions = project.getFileOptions();
+               Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
+               boolean insert = fileOptionEntries.isEmpty();
+               for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
+                       String fileName = fileOptionEntry.getKey();
+                       FileOption fileOption = fileOptionEntry.getValue();
+                       insert |= fileOption.isInsert() || fileOption.isInsertRedirect();
+                       if (fileName.equals(project.getIndexFile()) && !fileOption.isInsert() && !fileOption.isInsertRedirect()) {
+                               checkReport.addIssue("error.index-not-inserted", true);
+                       }
+                       if (!fileOption.isInsert() && fileOption.isInsertRedirect() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
+                               checkReport.addIssue("error.no-custom-key", true, fileName);
+                       }
+               }
+               if (!insert) {
+                       checkReport.addIssue("error.no-files-to-insert", true);
+               }
+               Set<String> fileNames = new HashSet<String>();
+               for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
+                       FileOption fileOption = fileOptionEntry.getValue();
+                       if (!fileOption.isInsert() && !fileOption.isInsertRedirect()) {
+                               logger.log(Level.FINEST, "Ignoring {0}.", fileOptionEntry.getKey());
+                               continue;
+                       }
+                       String fileName = fileOptionEntry.getKey();
+                       if (fileOption.hasChangedName()) {
+                               fileName = fileOption.getChangedName();
+                       }
+                       logger.log(Level.FINEST, "Adding “{0}” for {1}.", new Object[] { fileName, fileOptionEntry.getKey() });
+                       if (!fileNames.add(fileName)) {
+                               checkReport.addIssue("error.duplicate-file", true, fileName);
+                       }
+               }
+               return checkReport;
+       }
+
+       /**
         * {@inheritDoc}
         */
        public void run() {
@@ -387,7 +489,9 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                List<String> files = fileScanner.getFiles();
 
                /* create connection to node */
-               Connection connection = freenetInterface.getConnection("project-insert-" + random + counter++);
+               synchronized (lockObject) {
+                       connection = freenetInterface.getConnection("project-insert-" + random + counter++);
+               }
                connection.setTempDirectory(tempDirectory);
                boolean connected = false;
                Throwable cause = null;
@@ -397,8 +501,8 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                        cause = e1;
                }
 
-               if (!connected) {
-                       fireProjectInsertFinished(false, cause);
+               if (!connected || cancelled) {
+                       fireProjectInsertFinished(false, cancelled ? new AbortedException() : cause);
                        return;
                }
 
@@ -418,6 +522,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                }
                putDir.setVerbosity(Verbosity.ALL);
                putDir.setMaxRetries(-1);
+               putDir.setEarlyEncode(false);
                for (String filename : files) {
                        FileEntry fileEntry = createFileEntry(filename, edition, containerFiles);
                        if (fileEntry != null) {
@@ -432,7 +537,8 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
                /* start request */
                try {
-                       client.execute(putDir);
+                       client.execute(putDir, progressListener);
+                       fireProjectUploadFinished();
                } catch (IOException ioe1) {
                        fireProjectInsertFinished(false, ioe1);
                        return;
@@ -440,17 +546,12 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
                /* parse progress and success messages */
                String finalURI = null;
-               boolean firstMessage = true;
                boolean success = false;
                boolean finished = false;
                boolean disconnected = false;
-               while (!finished) {
+               while (!finished && !cancelled) {
                        Message message = client.readMessage();
                        finished = (message == null) || (disconnected = client.isDisconnected());
-                       if (firstMessage) {
-                               fireProjectUploadFinished();
-                               firstMessage = false;
-                       }
                        logger.log(Level.FINE, "Received message: " + message);
                        if (!finished) {
                                @SuppressWarnings("null")
@@ -473,7 +574,6 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                }
 
                /* post-insert work */
-               fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
                if (success) {
                        @SuppressWarnings("null")
                        String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
@@ -481,6 +581,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                        project.setEdition(newEdition);
                        project.setLastInsertionTime(System.currentTimeMillis());
                }
+               fireProjectInsertFinished(success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null));
        }
 
        //
@@ -499,4 +600,138 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                fileScanner.removeFileScannerListener(this);
        }
 
+       /**
+        * Container class that collects all warnings and errors that occured during
+        * {@link ProjectInserter#validateProject(Project) project validation}.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       public static class CheckReport implements Iterable<Issue> {
+
+               /** The issures that occured. */
+               private final List<Issue> issues = new ArrayList<Issue>();
+
+               /**
+                * Adds an issue.
+                *
+                * @param issue
+                *            The issue to add
+                */
+               public void addIssue(Issue issue) {
+                       issues.add(issue);
+               }
+
+               /**
+                * Creates an {@link Issue} from the given error key and fatality flag
+                * and {@link #addIssue(Issue) adds} it.
+                *
+                * @param errorKey
+                *            The error key
+                * @param fatal
+                *            {@code true} if the error is fatal, {@code false} if only
+                *            a warning should be generated
+                * @param parameters
+                *            Any additional parameters
+                */
+               public void addIssue(String errorKey, boolean fatal, String... parameters) {
+                       addIssue(new Issue(errorKey, fatal, parameters));
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               public Iterator<Issue> iterator() {
+                       return issues.iterator();
+               }
+
+               /**
+                * Returns whether this check report does not contain any errors.
+                *
+                * @return {@code true} if this check report does not contain any
+                *         errors, {@code false} if this check report does contain
+                *         errors
+                */
+               public boolean isEmpty() {
+                       return issues.isEmpty();
+               }
+
+               /**
+                * Returns the number of issues in this check report.
+                *
+                * @return The number of issues
+                */
+               public int size() {
+                       return issues.size();
+               }
+
+       }
+
+       /**
+        * Container class for a single issue. An issue contains an error key
+        * that describes the error, and a fatality flag that determines whether
+        * the insert has to be aborted (if the flag is {@code true}) or if it
+        * can still be performed and only a warning should be generated (if the
+        * flag is {@code false}).
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
+        *         Roden</a>
+        */
+       public static class Issue {
+
+               /** The error key. */
+               private final String errorKey;
+
+               /** The fatality flag. */
+               private final boolean fatal;
+
+               /** Additional parameters. */
+               private String[] parameters;
+
+               /**
+                * Creates a new issue.
+                *
+                * @param errorKey
+                *            The error key
+                * @param fatal
+                *            The fatality flag
+                * @param parameters
+                *            Any additional parameters
+                */
+               protected Issue(String errorKey, boolean fatal, String... parameters) {
+                       this.errorKey = errorKey;
+                       this.fatal = fatal;
+                       this.parameters = parameters;
+               }
+
+               /**
+                * Returns the key of the encountered error.
+                *
+                * @return The error key
+                */
+               public String getErrorKey() {
+                       return errorKey;
+               }
+
+               /**
+                * Returns whether the issue is fatal and the insert has to be
+                * aborted. Otherwise only a warning should be shown.
+                *
+                * @return {@code true} if the insert needs to be aborted, {@code
+                *         false} otherwise
+                */
+               public boolean isFatal() {
+                       return fatal;
+               }
+
+               /**
+                * Returns any additional parameters.
+                *
+                * @return The additional parameters
+                */
+               public String[] getParameters() {
+                       return parameters;
+               }
+
+       }
+
 }