📄 Update year in copyright line
[jSite.git] / src / main / java / de / todesbaum / jsite / application / ProjectInserter.java
index 30d4d7a..22ddaa0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * jSite - ProjectInserter.java - Copyright © 2006–2012 David Roden
+ * jSite - ProjectInserter.java - Copyright © 2006–2019 David Roden
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -20,27 +20,23 @@ package de.todesbaum.jsite.application;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Iterator;
+import java.util.Collection;
 import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-import java.util.concurrent.CountDownLatch;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import net.pterodactylus.util.io.StreamCopier.ProgressListener;
+
 import de.todesbaum.jsite.gui.FileScanner;
-import de.todesbaum.jsite.gui.FileScanner.ScannedFile;
+import de.todesbaum.jsite.gui.ScannedFile;
 import de.todesbaum.jsite.gui.FileScannerListener;
 import de.todesbaum.util.freenet.fcp2.Client;
 import de.todesbaum.util.freenet.fcp2.ClientPutComplexDir;
-import de.todesbaum.util.freenet.fcp2.ClientPutDir.ManifestPutter;
 import de.todesbaum.util.freenet.fcp2.Connection;
 import de.todesbaum.util.freenet.fcp2.DirectFileEntry;
 import de.todesbaum.util.freenet.fcp2.FileEntry;
@@ -63,22 +59,21 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        private static final int random = (int) (Math.random() * Integer.MAX_VALUE);
 
        /** Counter for FCP connection identifier. */
-       private static int counter = 0;
+       private static final AtomicInteger counter = new AtomicInteger();
 
-       /** The list of insert listeners. */
-       private List<InsertListener> insertListeners = new ArrayList<InsertListener>();
+       private final ProjectInsertListeners projectInsertListeners = new ProjectInsertListeners();
 
        /** The freenet interface. */
-       protected Freenet7Interface freenetInterface;
+       private Freenet7Interface freenetInterface;
 
        /** The project to insert. */
-       protected Project project;
+       private Project project;
 
        /** The file scanner. */
        private FileScanner fileScanner;
 
        /** Object used for synchronization. */
-       protected final Object lockObject = new Object();
+       private final Object lockObject = new Object();
 
        /** The temp directory. */
        private String tempDirectory;
@@ -98,9 +93,6 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /** The insert priority. */
        private PriorityClass priority;
 
-       /** The manifest putter. */
-       private ManifestPutter manifestPutter;
-
        /**
         * Adds a listener to the list of registered listeners.
         *
@@ -108,91 +100,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
         *            The listener to add
         */
        public void addInsertListener(InsertListener insertListener) {
-               insertListeners.add(insertListener);
-       }
-
-       /**
-        * Removes a listener from the list of registered listeners.
-        *
-        * @param insertListener
-        *            The listener to remove
-        */
-       public void removeInsertListener(InsertListener insertListener) {
-               insertListeners.remove(insertListener);
-       }
-
-       /**
-        * Notifies all listeners that the project insert has started.
-        *
-        * @see InsertListener#projectInsertStarted(Project)
-        */
-       protected void fireProjectInsertStarted() {
-               for (InsertListener insertListener : insertListeners) {
-                       insertListener.projectInsertStarted(project);
-               }
-       }
-
-       /**
-        * Notifies all listeners that the insert has generated a URI.
-        *
-        * @see InsertListener#projectURIGenerated(Project, String)
-        * @param uri
-        *            The generated URI
-        */
-       protected void fireProjectURIGenerated(String uri) {
-               for (InsertListener insertListener : insertListeners) {
-                       insertListener.projectURIGenerated(project, uri);
-               }
-       }
-
-       /**
-        * Notifies all listeners that the insert has made some progress.
-        *
-        * @see InsertListener#projectUploadFinished(Project)
-        */
-       protected void fireProjectUploadFinished() {
-               for (InsertListener insertListener : insertListeners) {
-                       insertListener.projectUploadFinished(project);
-               }
-       }
-
-       /**
-        * Notifies all listeners that the insert has made some progress.
-        *
-        * @see InsertListener#projectInsertProgress(Project, int, int, int, int,
-        *      boolean)
-        * @param succeeded
-        *            The number of succeeded blocks
-        * @param failed
-        *            The number of failed blocks
-        * @param fatal
-        *            The number of fatally failed blocks
-        * @param total
-        *            The total number of blocks
-        * @param finalized
-        *            <code>true</code> if the total number of blocks has already
-        *            been finalized, <code>false</code> otherwise
-        */
-       protected void fireProjectInsertProgress(int succeeded, int failed, int fatal, int total, boolean finalized) {
-               for (InsertListener insertListener : insertListeners) {
-                       insertListener.projectInsertProgress(project, succeeded, failed, fatal, total, finalized);
-               }
-       }
-
-       /**
-        * Notifies all listeners the project insert has finished.
-        *
-        * @see InsertListener#projectInsertFinished(Project, boolean, Throwable)
-        * @param success
-        *            <code>true</code> if the project was inserted successfully,
-        *            <code>false</code> if it failed
-        * @param cause
-        *            The cause of the failure, if any
-        */
-       protected void fireProjectInsertFinished(boolean success, Throwable cause) {
-               for (InsertListener insertListener : insertListeners) {
-                       insertListener.projectInsertFinished(project, success, cause);
-               }
+               projectInsertListeners.addInsertListener(insertListener);
        }
 
        /**
@@ -248,16 +156,6 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        }
 
        /**
-        * Sets the manifest putter to use for inserts.
-        *
-        * @param manifestPutter
-        *            The manifest putter to use
-        */
-       public void setManifestPutter(ManifestPutter manifestPutter) {
-               this.manifestPutter = manifestPutter;
-       }
-
-       /**
         * Starts the insert.
         *
         * @param progressListener
@@ -266,9 +164,8 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        public void start(ProgressListener progressListener) {
                cancelled = false;
                this.progressListener = progressListener;
-               fileScanner = new FileScanner(project);
-               fileScanner.addFileScannerListener(this);
-               new Thread(fileScanner).start();
+               fileScanner = new FileScanner(project, this);
+               fileScanner.startInBackground();
        }
 
        /**
@@ -284,41 +181,14 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        }
 
        /**
-        * Creates an input stream that delivers the given file, replacing edition
-        * tokens in the file’s content, if necessary.
-        *
-        * @param filename
-        *            The name of the file
-        * @param fileOption
-        *            The file options
-        * @param edition
-        *            The current edition
-        * @param length
-        *            An array containing a single long which is used to
-        *            <em>return</em> the final length of the file, after all
-        *            replacements
-        * @return The input stream for the file
-        * @throws IOException
-        *             if an I/O error occurs
-        */
-       private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException {
-               File file = new File(project.getLocalPath(), filename);
-               length[0] = file.length();
-               return new FileInputStream(file);
-       }
-
-       /**
         * Creates a file entry suitable for handing in to
         * {@link ClientPutComplexDir#addFileEntry(FileEntry)}.
         *
         * @param file
-        *            The name and hash of the file to insert
-        * @param edition
-        *            The current edition
+        *              The name and hash of the file to insert
         * @return A file entry for the given file
         */
-       private FileEntry createFileEntry(ScannedFile file, int edition) {
-               FileEntry fileEntry = null;
+       private Optional<FileEntry> createFileEntry(ScannedFile file) {
                String filename = file.getFilename();
                FileOption fileOption = project.getFileOption(filename);
                if (fileOption.isInsert()) {
@@ -327,117 +197,25 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                        if (!project.isAlwaysForceInsert() && !fileOption.isForceInsert() && file.getHash().equals(fileOption.getLastInsertHash())) {
                                /* only insert a redirect. */
                                logger.log(Level.FINE, String.format("Inserting redirect to edition %d for %s.", fileOption.getLastInsertEdition(), filename));
-                               return new RedirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename());
+                               return Optional.of(new RedirectFileEntry(fileOption.getChangedName().orElse(filename), fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename()));
                        }
                        try {
-                               long[] fileLength = new long[1];
-                               InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength);
-                               fileEntry = new DirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), fileEntryInputStream, fileLength[0]);
+                               return Optional.of(createFileEntry(filename, fileOption.getChangedName(), fileOption.getMimeType()));
                        } catch (IOException ioe1) {
                                /* ignore, null is returned. */
                        }
                } else {
                        if (fileOption.isInsertRedirect()) {
-                               fileEntry = new RedirectFileEntry(fileOption.hasChangedName() ? fileOption.getChangedName() : filename, fileOption.getMimeType(), fileOption.getCustomKey());
+                               return Optional.of(new RedirectFileEntry(fileOption.getChangedName().orElse(filename), fileOption.getMimeType(), fileOption.getCustomKey()));
                        }
                }
-               return fileEntry;
+               return Optional.empty();
        }
 
-       /**
-        * 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);
-               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);
-                       }
-               }
-               long totalSize = 0;
-               FileScanner fileScanner = new FileScanner(project);
-               final CountDownLatch completionLatch = new CountDownLatch(1);
-               fileScanner.addFileScannerListener(new FileScannerListener() {
-
-                       @Override
-                       public void fileScannerFinished(FileScanner fileScanner) {
-                               completionLatch.countDown();
-                       }
-               });
-               new Thread(fileScanner).start();
-               while (completionLatch.getCount() > 0) {
-                       try {
-                               completionLatch.await();
-                       } catch (InterruptedException ie1) {
-                               /* TODO: logging */
-                       }
-               }
-               for (ScannedFile scannedFile : fileScanner.getFiles()) {
-                       String fileName = scannedFile.getFilename();
-                       FileOption fileOption = project.getFileOption(fileName);
-                       if ((fileOption != null) && !fileOption.isInsert()) {
-                               continue;
-                       }
-                       totalSize += new File(project.getLocalPath(), fileName).length();
-               }
-               if (totalSize > 2 * 1024 * 1024) {
-                       checkReport.addIssue("warning.site-larger-than-2-mib", false);
-               }
-               return checkReport;
+       private FileEntry createFileEntry(String filename, Optional<String> changedName, String mimeType) throws FileNotFoundException {
+               File physicalFile = new File(project.getLocalPath(), filename);
+               InputStream fileEntryInputStream = new FileInputStream(physicalFile);
+               return new DirectFileEntry(changedName.orElse(filename), mimeType, fileEntryInputStream, physicalFile.length());
        }
 
        /**
@@ -445,12 +223,12 @@ public class ProjectInserter implements FileScannerListener, Runnable {
         */
        @Override
        public void run() {
-               fireProjectInsertStarted();
+               projectInsertListeners.fireProjectInsertStarted(project);
                List<ScannedFile> files = fileScanner.getFiles();
 
                /* create connection to node */
                synchronized (lockObject) {
-                       connection = freenetInterface.getConnection("project-insert-" + random + counter++);
+                       connection = freenetInterface.getConnection("project-insert-" + random + counter.getAndIncrement());
                }
                connection.setTempDirectory(tempDirectory);
                boolean connected = false;
@@ -462,7 +240,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                }
 
                if (!connected || cancelled) {
-                       fireProjectInsertFinished(false, cancelled ? new AbortedException() : cause);
+                       projectInsertListeners.fireProjectInsertFinished(project, false, cancelled ? new AbortedException() : cause);
                        return;
                }
 
@@ -471,22 +249,27 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                /* collect files */
                int edition = project.getEdition();
                String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
-               ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI, tempDirectory);
+               ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter.getAndIncrement(), dirURI, tempDirectory);
                if ((project.getIndexFile() != null) && (project.getIndexFile().length() > 0)) {
-                       putDir.setDefaultName(project.getIndexFile());
+                       FileOption indexFileOption = project.getFileOption(project.getIndexFile());
+                       Optional<String> changedName = indexFileOption.getChangedName();
+                       if (changedName.isPresent()) {
+                               putDir.setDefaultName(changedName.get());
+                       } else {
+                               putDir.setDefaultName(project.getIndexFile());
+                       }
                }
                putDir.setVerbosity(Verbosity.ALL);
                putDir.setMaxRetries(-1);
                putDir.setEarlyEncode(useEarlyEncode);
                putDir.setPriorityClass(priority);
-               putDir.setManifestPutter(manifestPutter);
                for (ScannedFile file : files) {
-                       FileEntry fileEntry = createFileEntry(file, edition);
-                       if (fileEntry != null) {
+                       Optional<FileEntry> fileEntry = createFileEntry(file);
+                       if (fileEntry.isPresent()) {
                                try {
-                                       putDir.addFileEntry(fileEntry);
+                                       putDir.addFileEntry(fileEntry.get());
                                } catch (IOException ioe1) {
-                                       fireProjectInsertFinished(false, ioe1);
+                                       projectInsertListeners.fireProjectInsertFinished(project, false, ioe1);
                                        return;
                                }
                        }
@@ -495,9 +278,9 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                /* start request */
                try {
                        client.execute(putDir, progressListener);
-                       fireProjectUploadFinished();
+                       projectInsertListeners.fireProjectUploadFinished(project);
                } catch (IOException ioe1) {
-                       fireProjectInsertFinished(false, ioe1);
+                       projectInsertListeners.fireProjectInsertFinished(project, false, ioe1);
                        return;
                }
 
@@ -515,7 +298,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                                String messageName = message.getName();
                                if ("URIGenerated".equals(messageName)) {
                                        finalURI = message.get("URI");
-                                       fireProjectURIGenerated(finalURI);
+                                       projectInsertListeners.fireProjectURIGenerated(project, finalURI);
                                }
                                if ("SimpleProgress".equals(messageName)) {
                                        int total = Integer.parseInt(message.get("Total"));
@@ -523,7 +306,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                                        int fatal = Integer.parseInt(message.get("FatallyFailed"));
                                        int failed = Integer.parseInt(message.get("Failed"));
                                        boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal"));
-                                       fireProjectInsertProgress(succeeded, failed, fatal, total, finalized);
+                                       projectInsertListeners.fireProjectInsertProgress(project, succeeded, failed, fatal, total, finalized);
                                }
                                success |= "PutSuccessful".equals(messageName);
                                finished = (success && (finalURI != null)) || "PutFailed".equals(messageName) || messageName.endsWith("Error");
@@ -539,7 +322,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                        project.setLastInsertionTime(System.currentTimeMillis());
                        project.onSuccessfulInsert();
                }
-               fireProjectInsertFinished(success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null));
+               projectInsertListeners.fireProjectInsertFinished(project, success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null));
        }
 
        //
@@ -550,148 +333,12 @@ public class ProjectInserter implements FileScannerListener, Runnable {
         * {@inheritDoc}
         */
        @Override
-       public void fileScannerFinished(FileScanner fileScanner) {
-               if (!fileScanner.isError()) {
+       public void fileScannerFinished(boolean error, Collection<ScannedFile> files) {
+               if (!error) {
                        new Thread(this).start();
                } else {
-                       fireProjectInsertFinished(false, null);
+                       projectInsertListeners.fireProjectInsertFinished(project, false, null);
                }
-               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}
-                */
-               @Override
-               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;
-               }
-
        }
 
 }