2 * jSite - ProjectInserter.java - Copyright © 2006–2012 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.application;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.List;
31 import java.util.Map.Entry;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
36 import de.todesbaum.jsite.gui.FileScanner;
37 import de.todesbaum.jsite.gui.FileScanner.ScannedFile;
38 import de.todesbaum.jsite.gui.FileScannerListener;
39 import de.todesbaum.util.freenet.fcp2.Client;
40 import de.todesbaum.util.freenet.fcp2.ClientPutComplexDir;
41 import de.todesbaum.util.freenet.fcp2.ClientPutDir.ManifestPutter;
42 import de.todesbaum.util.freenet.fcp2.Connection;
43 import de.todesbaum.util.freenet.fcp2.DirectFileEntry;
44 import de.todesbaum.util.freenet.fcp2.FileEntry;
45 import de.todesbaum.util.freenet.fcp2.Message;
46 import de.todesbaum.util.freenet.fcp2.RedirectFileEntry;
47 import de.todesbaum.util.freenet.fcp2.Verbosity;
48 import de.todesbaum.util.io.StreamCopier.ProgressListener;
51 * Manages project inserts.
53 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
55 public class ProjectInserter implements FileScannerListener, Runnable {
58 private static final Logger logger = Logger.getLogger(ProjectInserter.class.getName());
60 /** Random number for FCP instances. */
61 private static final int random = (int) (Math.random() * Integer.MAX_VALUE);
63 /** Counter for FCP connection identifier. */
64 private static int counter = 0;
66 /** The list of insert listeners. */
67 private List<InsertListener> insertListeners = new ArrayList<InsertListener>();
69 /** The freenet interface. */
70 protected Freenet7Interface freenetInterface;
72 /** The project to insert. */
73 protected Project project;
75 /** The file scanner. */
76 private FileScanner fileScanner;
78 /** Object used for synchronization. */
79 protected final Object lockObject = new Object();
81 /** The temp directory. */
82 private String tempDirectory;
84 /** The current connection. */
85 private Connection connection;
87 /** Whether the insert is cancelled. */
88 private volatile boolean cancelled = false;
90 /** Progress listener for payload transfers. */
91 private ProgressListener progressListener;
93 /** Whether to use “early encode.” */
94 private boolean useEarlyEncode;
97 * Adds a listener to the list of registered listeners.
99 * @param insertListener
100 * The listener to add
102 public void addInsertListener(InsertListener insertListener) {
103 insertListeners.add(insertListener);
107 * Removes a listener from the list of registered listeners.
109 * @param insertListener
110 * The listener to remove
112 public void removeInsertListener(InsertListener insertListener) {
113 insertListeners.remove(insertListener);
117 * Notifies all listeners that the project insert has started.
119 * @see InsertListener#projectInsertStarted(Project)
121 protected void fireProjectInsertStarted() {
122 for (InsertListener insertListener : insertListeners) {
123 insertListener.projectInsertStarted(project);
128 * Notifies all listeners that the insert has generated a URI.
130 * @see InsertListener#projectURIGenerated(Project, String)
134 protected void fireProjectURIGenerated(String uri) {
135 for (InsertListener insertListener : insertListeners) {
136 insertListener.projectURIGenerated(project, uri);
141 * Notifies all listeners that the insert has made some progress.
143 * @see InsertListener#projectUploadFinished(Project)
145 protected void fireProjectUploadFinished() {
146 for (InsertListener insertListener : insertListeners) {
147 insertListener.projectUploadFinished(project);
152 * Notifies all listeners that the insert has made some progress.
154 * @see InsertListener#projectInsertProgress(Project, int, int, int, int,
157 * The number of succeeded blocks
159 * The number of failed blocks
161 * The number of fatally failed blocks
163 * The total number of blocks
165 * <code>true</code> if the total number of blocks has already
166 * been finalized, <code>false</code> otherwise
168 protected void fireProjectInsertProgress(int succeeded, int failed, int fatal, int total, boolean finalized) {
169 for (InsertListener insertListener : insertListeners) {
170 insertListener.projectInsertProgress(project, succeeded, failed, fatal, total, finalized);
175 * Notifies all listeners the project insert has finished.
177 * @see InsertListener#projectInsertFinished(Project, boolean, Throwable)
179 * <code>true</code> if the project was inserted successfully,
180 * <code>false</code> if it failed
182 * The cause of the failure, if any
184 protected void fireProjectInsertFinished(boolean success, Throwable cause) {
185 for (InsertListener insertListener : insertListeners) {
186 insertListener.projectInsertFinished(project, success, cause);
191 * Sets the project to insert.
194 * The project to insert
196 public void setProject(Project project) {
197 this.project = project;
201 * Sets the freenet interface to use.
203 * @param freenetInterface
204 * The freenet interface to use
206 public void setFreenetInterface(Freenet7Interface freenetInterface) {
207 this.freenetInterface = freenetInterface;
211 * Sets the temp directory to use.
213 * @param tempDirectory
214 * The temp directory to use, or {@code null} to use the system
217 public void setTempDirectory(String tempDirectory) {
218 this.tempDirectory = tempDirectory;
222 * Sets whether to use the “early encode“ flag for the insert.
224 * @param useEarlyEncode
225 * {@code true} to set the “early encode” flag for the insert,
226 * {@code false} otherwise
228 public void setUseEarlyEncode(boolean useEarlyEncode) {
229 this.useEarlyEncode = useEarlyEncode;
235 * @param progressListener
236 * Listener to notify on progress events
238 public void start(ProgressListener progressListener) {
240 this.progressListener = progressListener;
241 fileScanner = new FileScanner(project);
242 fileScanner.addFileScannerListener(this);
243 new Thread(fileScanner).start();
247 * Stops the current insert.
251 synchronized (lockObject) {
252 if (connection != null) {
253 connection.disconnect();
259 * Creates an input stream that delivers the given file, replacing edition
260 * tokens in the file’s content, if necessary.
263 * The name of the file
267 * The current edition
269 * An array containing a single long which is used to
270 * <em>return</em> the final length of the file, after all
272 * @return The input stream for the file
273 * @throws IOException
274 * if an I/O error occurs
276 private InputStream createFileInputStream(String filename, FileOption fileOption, int edition, long[] length) throws IOException {
277 File file = new File(project.getLocalPath(), filename);
278 length[0] = file.length();
279 return new FileInputStream(file);
283 * Creates a file entry suitable for handing in to
284 * {@link ClientPutComplexDir#addFileEntry(FileEntry)}.
287 * The name and hash of the file to insert
289 * The current edition
290 * @return A file entry for the given file
292 private FileEntry createFileEntry(ScannedFile file, int edition) {
293 FileEntry fileEntry = null;
294 String filename = file.getFilename();
295 FileOption fileOption = project.getFileOption(filename);
296 if (fileOption.isInsert()) {
297 fileOption.setCurrentHash(file.getHash());
298 /* check if file was modified. */
299 if (!fileOption.isForceInsert() && file.getHash().equals(fileOption.getLastInsertHash())) {
300 /* only insert a redirect. */
301 logger.log(Level.FINE, String.format("Inserting redirect to edition %d for %s.", fileOption.getLastInsertEdition(), filename));
302 return new RedirectFileEntry(filename, fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + filename);
305 long[] fileLength = new long[1];
306 InputStream fileEntryInputStream = createFileInputStream(filename, fileOption, edition, fileLength);
307 fileEntry = new DirectFileEntry(filename, fileOption.getMimeType(), fileEntryInputStream, fileLength[0]);
308 } catch (IOException ioe1) {
309 /* ignore, null is returned. */
312 if (fileOption.isInsertRedirect()) {
313 fileEntry = new RedirectFileEntry(filename, fileOption.getMimeType(), fileOption.getCustomKey());
320 * Validates the given project. The project will be checked for any invalid
321 * conditions, such as invalid insert or request keys, missing path names,
322 * missing default file, and so on.
325 * The project to check
326 * @return The encountered warnings and errors
328 public static CheckReport validateProject(Project project) {
329 CheckReport checkReport = new CheckReport();
330 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
331 checkReport.addIssue("error.no-local-path", true);
333 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
334 checkReport.addIssue("error.no-path", true);
336 if ((project.getIndexFile() == null) || (project.getIndexFile().length() == 0)) {
337 checkReport.addIssue("warning.empty-index", false);
339 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
340 if (!indexFile.exists()) {
341 checkReport.addIssue("error.index-missing", true);
344 String indexFile = project.getIndexFile();
345 boolean hasIndexFile = (indexFile != null) && (indexFile.length() > 0);
346 List<String> allowedIndexContentTypes = Arrays.asList("text/html", "application/xhtml+xml");
347 if (hasIndexFile && !allowedIndexContentTypes.contains(project.getFileOption(indexFile).getMimeType())) {
348 checkReport.addIssue("warning.index-not-html", false);
350 Map<String, FileOption> fileOptions = project.getFileOptions();
351 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
352 boolean insert = fileOptionEntries.isEmpty();
353 for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
354 String fileName = fileOptionEntry.getKey();
355 FileOption fileOption = fileOptionEntry.getValue();
356 insert |= fileOption.isInsert() || fileOption.isInsertRedirect();
357 if (fileName.equals(project.getIndexFile()) && !fileOption.isInsert() && !fileOption.isInsertRedirect()) {
358 checkReport.addIssue("error.index-not-inserted", true);
360 if (!fileOption.isInsert() && fileOption.isInsertRedirect() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
361 checkReport.addIssue("error.no-custom-key", true, fileName);
365 checkReport.addIssue("error.no-files-to-insert", true);
367 Set<String> fileNames = new HashSet<String>();
368 for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
369 FileOption fileOption = fileOptionEntry.getValue();
370 if (!fileOption.isInsert() && !fileOption.isInsertRedirect()) {
371 logger.log(Level.FINEST, "Ignoring {0}.", fileOptionEntry.getKey());
374 String fileName = fileOptionEntry.getKey();
375 if (fileOption.hasChangedName()) {
376 fileName = fileOption.getChangedName();
378 logger.log(Level.FINEST, "Adding “{0}” for {1}.", new Object[] { fileName, fileOptionEntry.getKey() });
379 if (!fileNames.add(fileName)) {
380 checkReport.addIssue("error.duplicate-file", true, fileName);
390 fireProjectInsertStarted();
391 List<ScannedFile> files = fileScanner.getFiles();
393 /* create connection to node */
394 synchronized (lockObject) {
395 connection = freenetInterface.getConnection("project-insert-" + random + counter++);
397 connection.setTempDirectory(tempDirectory);
398 boolean connected = false;
399 Throwable cause = null;
401 connected = connection.connect();
402 } catch (IOException e1) {
406 if (!connected || cancelled) {
407 fireProjectInsertFinished(false, cancelled ? new AbortedException() : cause);
411 Client client = new Client(connection);
414 int edition = project.getEdition();
415 String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
416 ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI, tempDirectory);
417 if ((project.getIndexFile() != null) && (project.getIndexFile().length() > 0)) {
418 putDir.setDefaultName(project.getIndexFile());
420 putDir.setVerbosity(Verbosity.ALL);
421 putDir.setMaxRetries(-1);
422 putDir.setEarlyEncode(useEarlyEncode);
423 putDir.setManifestPutter(ManifestPutter.DEFAULT);
424 for (ScannedFile file : files) {
425 FileEntry fileEntry = createFileEntry(file, edition);
426 if (fileEntry != null) {
428 putDir.addFileEntry(fileEntry);
429 } catch (IOException ioe1) {
430 fireProjectInsertFinished(false, ioe1);
438 client.execute(putDir, progressListener);
439 fireProjectUploadFinished();
440 } catch (IOException ioe1) {
441 fireProjectInsertFinished(false, ioe1);
445 /* parse progress and success messages */
446 String finalURI = null;
447 boolean success = false;
448 boolean finished = false;
449 boolean disconnected = false;
450 while (!finished && !cancelled) {
451 Message message = client.readMessage();
452 finished = (message == null) || (disconnected = client.isDisconnected());
453 logger.log(Level.FINE, "Received message: " + message);
455 @SuppressWarnings("null")
456 String messageName = message.getName();
457 if ("URIGenerated".equals(messageName)) {
458 finalURI = message.get("URI");
459 fireProjectURIGenerated(finalURI);
461 if ("SimpleProgress".equals(messageName)) {
462 int total = Integer.parseInt(message.get("Total"));
463 int succeeded = Integer.parseInt(message.get("Succeeded"));
464 int fatal = Integer.parseInt(message.get("FatallyFailed"));
465 int failed = Integer.parseInt(message.get("Failed"));
466 boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal"));
467 fireProjectInsertProgress(succeeded, failed, fatal, total, finalized);
469 success |= "PutSuccessful".equals(messageName);
470 finished = (success && (finalURI != null)) || "PutFailed".equals(messageName) || messageName.endsWith("Error");
474 /* post-insert work */
476 @SuppressWarnings("null")
477 String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
478 int newEdition = Integer.parseInt(editionPart);
479 project.setEdition(newEdition);
480 project.setLastInsertionTime(System.currentTimeMillis());
481 project.onSuccessfulInsert();
483 fireProjectInsertFinished(success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null));
487 // INTERFACE FileScannerListener
493 public void fileScannerFinished(FileScanner fileScanner) {
494 if (!fileScanner.isError()) {
495 new Thread(this).start();
497 fireProjectInsertFinished(false, null);
499 fileScanner.removeFileScannerListener(this);
503 * Container class that collects all warnings and errors that occured during
504 * {@link ProjectInserter#validateProject(Project) project validation}.
506 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
508 public static class CheckReport implements Iterable<Issue> {
510 /** The issures that occured. */
511 private final List<Issue> issues = new ArrayList<Issue>();
519 public void addIssue(Issue issue) {
524 * Creates an {@link Issue} from the given error key and fatality flag
525 * and {@link #addIssue(Issue) adds} it.
530 * {@code true} if the error is fatal, {@code false} if only
531 * a warning should be generated
533 * Any additional parameters
535 public void addIssue(String errorKey, boolean fatal, String... parameters) {
536 addIssue(new Issue(errorKey, fatal, parameters));
542 public Iterator<Issue> iterator() {
543 return issues.iterator();
547 * Returns whether this check report does not contain any errors.
549 * @return {@code true} if this check report does not contain any
550 * errors, {@code false} if this check report does contain
553 public boolean isEmpty() {
554 return issues.isEmpty();
558 * Returns the number of issues in this check report.
560 * @return The number of issues
563 return issues.size();
569 * Container class for a single issue. An issue contains an error key
570 * that describes the error, and a fatality flag that determines whether
571 * the insert has to be aborted (if the flag is {@code true}) or if it
572 * can still be performed and only a warning should be generated (if the
573 * flag is {@code false}).
575 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
578 public static class Issue {
580 /** The error key. */
581 private final String errorKey;
583 /** The fatality flag. */
584 private final boolean fatal;
586 /** Additional parameters. */
587 private String[] parameters;
590 * Creates a new issue.
597 * Any additional parameters
599 protected Issue(String errorKey, boolean fatal, String... parameters) {
600 this.errorKey = errorKey;
602 this.parameters = parameters;
606 * Returns the key of the encountered error.
608 * @return The error key
610 public String getErrorKey() {
615 * Returns whether the issue is fatal and the insert has to be
616 * aborted. Otherwise only a warning should be shown.
618 * @return {@code true} if the insert needs to be aborted, {@code
621 public boolean isFatal() {
626 * Returns any additional parameters.
628 * @return The additional parameters
630 public String[] getParameters() {