📄 Update year in copyright line
[jSite.git] / src / main / java / de / todesbaum / jsite / application / ProjectInserter.java
1 /*
2  * jSite - ProjectInserter.java - Copyright © 2006–2019 David Roden
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package de.todesbaum.jsite.application;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.concurrent.atomic.AtomicInteger;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32
33 import net.pterodactylus.util.io.StreamCopier.ProgressListener;
34
35 import de.todesbaum.jsite.gui.FileScanner;
36 import de.todesbaum.jsite.gui.ScannedFile;
37 import de.todesbaum.jsite.gui.FileScannerListener;
38 import de.todesbaum.util.freenet.fcp2.Client;
39 import de.todesbaum.util.freenet.fcp2.ClientPutComplexDir;
40 import de.todesbaum.util.freenet.fcp2.Connection;
41 import de.todesbaum.util.freenet.fcp2.DirectFileEntry;
42 import de.todesbaum.util.freenet.fcp2.FileEntry;
43 import de.todesbaum.util.freenet.fcp2.Message;
44 import de.todesbaum.util.freenet.fcp2.PriorityClass;
45 import de.todesbaum.util.freenet.fcp2.RedirectFileEntry;
46 import de.todesbaum.util.freenet.fcp2.Verbosity;
47
48 /**
49  * Manages project inserts.
50  *
51  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
52  */
53 public class ProjectInserter implements FileScannerListener, Runnable {
54
55         /** The logger. */
56         private static final Logger logger = Logger.getLogger(ProjectInserter.class.getName());
57
58         /** Random number for FCP instances. */
59         private static final int random = (int) (Math.random() * Integer.MAX_VALUE);
60
61         /** Counter for FCP connection identifier. */
62         private static final AtomicInteger counter = new AtomicInteger();
63
64         private final ProjectInsertListeners projectInsertListeners = new ProjectInsertListeners();
65
66         /** The freenet interface. */
67         private Freenet7Interface freenetInterface;
68
69         /** The project to insert. */
70         private Project project;
71
72         /** The file scanner. */
73         private FileScanner fileScanner;
74
75         /** Object used for synchronization. */
76         private final Object lockObject = new Object();
77
78         /** The temp directory. */
79         private String tempDirectory;
80
81         /** The current connection. */
82         private Connection connection;
83
84         /** Whether the insert is cancelled. */
85         private volatile boolean cancelled = false;
86
87         /** Progress listener for payload transfers. */
88         private ProgressListener progressListener;
89
90         /** Whether to use “early encode.” */
91         private boolean useEarlyEncode;
92
93         /** The insert priority. */
94         private PriorityClass priority;
95
96         /**
97          * Adds a listener to the list of registered listeners.
98          *
99          * @param insertListener
100          *            The listener to add
101          */
102         public void addInsertListener(InsertListener insertListener) {
103                 projectInsertListeners.addInsertListener(insertListener);
104         }
105
106         /**
107          * Sets the project to insert.
108          *
109          * @param project
110          *            The project to insert
111          */
112         public void setProject(Project project) {
113                 this.project = project;
114         }
115
116         /**
117          * Sets the freenet interface to use.
118          *
119          * @param freenetInterface
120          *            The freenet interface to use
121          */
122         public void setFreenetInterface(Freenet7Interface freenetInterface) {
123                 this.freenetInterface = freenetInterface;
124         }
125
126         /**
127          * Sets the temp directory to use.
128          *
129          * @param tempDirectory
130          *            The temp directory to use, or {@code null} to use the system
131          *            default
132          */
133         public void setTempDirectory(String tempDirectory) {
134                 this.tempDirectory = tempDirectory;
135         }
136
137         /**
138          * Sets whether to use the “early encode“ flag for the insert.
139          *
140          * @param useEarlyEncode
141          *            {@code true} to set the “early encode” flag for the insert,
142          *            {@code false} otherwise
143          */
144         public void setUseEarlyEncode(boolean useEarlyEncode) {
145                 this.useEarlyEncode = useEarlyEncode;
146         }
147
148         /**
149          * Sets the insert priority.
150          *
151          * @param priority
152          *            The insert priority
153          */
154         public void setPriority(PriorityClass priority) {
155                 this.priority = priority;
156         }
157
158         /**
159          * Starts the insert.
160          *
161          * @param progressListener
162          *            Listener to notify on progress events
163          */
164         public void start(ProgressListener progressListener) {
165                 cancelled = false;
166                 this.progressListener = progressListener;
167                 fileScanner = new FileScanner(project, this);
168                 fileScanner.startInBackground();
169         }
170
171         /**
172          * Stops the current insert.
173          */
174         public void stop() {
175                 cancelled = true;
176                 synchronized (lockObject) {
177                         if (connection != null) {
178                                 connection.disconnect();
179                         }
180                 }
181         }
182
183         /**
184          * Creates a file entry suitable for handing in to
185          * {@link ClientPutComplexDir#addFileEntry(FileEntry)}.
186          *
187          * @param file
188          *              The name and hash of the file to insert
189          * @return A file entry for the given file
190          */
191         private Optional<FileEntry> createFileEntry(ScannedFile file) {
192                 String filename = file.getFilename();
193                 FileOption fileOption = project.getFileOption(filename);
194                 if (fileOption.isInsert()) {
195                         fileOption.setCurrentHash(file.getHash());
196                         /* check if file was modified. */
197                         if (!project.isAlwaysForceInsert() && !fileOption.isForceInsert() && file.getHash().equals(fileOption.getLastInsertHash())) {
198                                 /* only insert a redirect. */
199                                 logger.log(Level.FINE, String.format("Inserting redirect to edition %d for %s.", fileOption.getLastInsertEdition(), filename));
200                                 return Optional.of(new RedirectFileEntry(fileOption.getChangedName().orElse(filename), fileOption.getMimeType(), "SSK@" + project.getRequestURI() + "/" + project.getPath() + "-" + fileOption.getLastInsertEdition() + "/" + fileOption.getLastInsertFilename()));
201                         }
202                         try {
203                                 return Optional.of(createFileEntry(filename, fileOption.getChangedName(), fileOption.getMimeType()));
204                         } catch (IOException ioe1) {
205                                 /* ignore, null is returned. */
206                         }
207                 } else {
208                         if (fileOption.isInsertRedirect()) {
209                                 return Optional.of(new RedirectFileEntry(fileOption.getChangedName().orElse(filename), fileOption.getMimeType(), fileOption.getCustomKey()));
210                         }
211                 }
212                 return Optional.empty();
213         }
214
215         private FileEntry createFileEntry(String filename, Optional<String> changedName, String mimeType) throws FileNotFoundException {
216                 File physicalFile = new File(project.getLocalPath(), filename);
217                 InputStream fileEntryInputStream = new FileInputStream(physicalFile);
218                 return new DirectFileEntry(changedName.orElse(filename), mimeType, fileEntryInputStream, physicalFile.length());
219         }
220
221         /**
222          * {@inheritDoc}
223          */
224         @Override
225         public void run() {
226                 projectInsertListeners.fireProjectInsertStarted(project);
227                 List<ScannedFile> files = fileScanner.getFiles();
228
229                 /* create connection to node */
230                 synchronized (lockObject) {
231                         connection = freenetInterface.getConnection("project-insert-" + random + counter.getAndIncrement());
232                 }
233                 connection.setTempDirectory(tempDirectory);
234                 boolean connected = false;
235                 Throwable cause = null;
236                 try {
237                         connected = connection.connect();
238                 } catch (IOException e1) {
239                         cause = e1;
240                 }
241
242                 if (!connected || cancelled) {
243                         projectInsertListeners.fireProjectInsertFinished(project, false, cancelled ? new AbortedException() : cause);
244                         return;
245                 }
246
247                 Client client = new Client(connection);
248
249                 /* collect files */
250                 int edition = project.getEdition();
251                 String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
252                 ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter.getAndIncrement(), dirURI, tempDirectory);
253                 if ((project.getIndexFile() != null) && (project.getIndexFile().length() > 0)) {
254                         FileOption indexFileOption = project.getFileOption(project.getIndexFile());
255                         Optional<String> changedName = indexFileOption.getChangedName();
256                         if (changedName.isPresent()) {
257                                 putDir.setDefaultName(changedName.get());
258                         } else {
259                                 putDir.setDefaultName(project.getIndexFile());
260                         }
261                 }
262                 putDir.setVerbosity(Verbosity.ALL);
263                 putDir.setMaxRetries(-1);
264                 putDir.setEarlyEncode(useEarlyEncode);
265                 putDir.setPriorityClass(priority);
266                 for (ScannedFile file : files) {
267                         Optional<FileEntry> fileEntry = createFileEntry(file);
268                         if (fileEntry.isPresent()) {
269                                 try {
270                                         putDir.addFileEntry(fileEntry.get());
271                                 } catch (IOException ioe1) {
272                                         projectInsertListeners.fireProjectInsertFinished(project, false, ioe1);
273                                         return;
274                                 }
275                         }
276                 }
277
278                 /* start request */
279                 try {
280                         client.execute(putDir, progressListener);
281                         projectInsertListeners.fireProjectUploadFinished(project);
282                 } catch (IOException ioe1) {
283                         projectInsertListeners.fireProjectInsertFinished(project, false, ioe1);
284                         return;
285                 }
286
287                 /* parse progress and success messages */
288                 String finalURI = null;
289                 boolean success = false;
290                 boolean finished = false;
291                 boolean disconnected = false;
292                 while (!finished && !cancelled) {
293                         Message message = client.readMessage();
294                         finished = (message == null) || (disconnected = client.isDisconnected());
295                         logger.log(Level.FINE, "Received message: " + message);
296                         if (!finished) {
297                                 @SuppressWarnings("null")
298                                 String messageName = message.getName();
299                                 if ("URIGenerated".equals(messageName)) {
300                                         finalURI = message.get("URI");
301                                         projectInsertListeners.fireProjectURIGenerated(project, finalURI);
302                                 }
303                                 if ("SimpleProgress".equals(messageName)) {
304                                         int total = Integer.parseInt(message.get("Total"));
305                                         int succeeded = Integer.parseInt(message.get("Succeeded"));
306                                         int fatal = Integer.parseInt(message.get("FatallyFailed"));
307                                         int failed = Integer.parseInt(message.get("Failed"));
308                                         boolean finalized = Boolean.parseBoolean(message.get("FinalizedTotal"));
309                                         projectInsertListeners.fireProjectInsertProgress(project, succeeded, failed, fatal, total, finalized);
310                                 }
311                                 success |= "PutSuccessful".equals(messageName);
312                                 finished = (success && (finalURI != null)) || "PutFailed".equals(messageName) || messageName.endsWith("Error");
313                         }
314                 }
315
316                 /* post-insert work */
317                 if (success) {
318                         @SuppressWarnings("null")
319                         String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
320                         int newEdition = Integer.parseInt(editionPart);
321                         project.setEdition(newEdition);
322                         project.setLastInsertionTime(System.currentTimeMillis());
323                         project.onSuccessfulInsert();
324                 }
325                 projectInsertListeners.fireProjectInsertFinished(project, success, cancelled ? new AbortedException() : (disconnected ? new IOException("Connection terminated") : null));
326         }
327
328         //
329         // INTERFACE FileScannerListener
330         //
331
332         /**
333          * {@inheritDoc}
334          */
335         @Override
336         public void fileScannerFinished(boolean error, Collection<ScannedFile> files) {
337                 if (!error) {
338                         new Thread(this).start();
339                 } else {
340                         projectInsertListeners.fireProjectInsertFinished(project, false, null);
341                 }
342         }
343
344 }