Don’t set insert URI of a Sone, let it be generated from the identity.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneInserter.java
1 /*
2  * Sone - SoneInserter.java - Copyright © 2010–2013 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 3 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, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import static java.lang.String.format;
21 import static java.lang.System.currentTimeMillis;
22 import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
23
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.StringWriter;
27 import java.nio.charset.Charset;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.concurrent.atomic.AtomicInteger;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
35 import net.pterodactylus.sone.core.Options.Option;
36 import net.pterodactylus.sone.core.Options.OptionWatcher;
37 import net.pterodactylus.sone.core.SoneModificationDetector.LockableFingerprintProvider;
38 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
39 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
40 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
41 import net.pterodactylus.sone.data.Album;
42 import net.pterodactylus.sone.data.Post;
43 import net.pterodactylus.sone.data.Reply;
44 import net.pterodactylus.sone.data.Sone;
45 import net.pterodactylus.sone.data.Sone.SoneStatus;
46 import net.pterodactylus.sone.freenet.StringBucket;
47 import net.pterodactylus.sone.main.SonePlugin;
48 import net.pterodactylus.util.io.Closer;
49 import net.pterodactylus.util.logging.Logging;
50 import net.pterodactylus.util.service.AbstractService;
51 import net.pterodactylus.util.template.HtmlFilter;
52 import net.pterodactylus.util.template.ReflectionAccessor;
53 import net.pterodactylus.util.template.Template;
54 import net.pterodactylus.util.template.TemplateContext;
55 import net.pterodactylus.util.template.TemplateContextFactory;
56 import net.pterodactylus.util.template.TemplateException;
57 import net.pterodactylus.util.template.TemplateParser;
58 import net.pterodactylus.util.template.XmlFilter;
59
60 import com.google.common.annotations.VisibleForTesting;
61 import com.google.common.base.Optional;
62 import com.google.common.collect.FluentIterable;
63 import com.google.common.collect.Ordering;
64 import com.google.common.eventbus.EventBus;
65
66 import freenet.client.async.ManifestElement;
67 import freenet.keys.FreenetURI;
68
69 /**
70  * A Sone inserter is responsible for inserting a Sone if it has changed.
71  *
72  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
73  */
74 public class SoneInserter extends AbstractService {
75
76         /** The logger. */
77         private static final Logger logger = Logging.getLogger(SoneInserter.class);
78
79         /** The insertion delay (in seconds). */
80         private static final AtomicInteger insertionDelay = new AtomicInteger(60);
81
82         /** The template factory used to create the templates. */
83         private static final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
84
85         static {
86                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
87                 templateContextFactory.addFilter("xml", new XmlFilter());
88                 templateContextFactory.addFilter("html", new HtmlFilter());
89         }
90
91         /** The UTF-8 charset. */
92         private static final Charset utf8Charset = Charset.forName("UTF-8");
93
94         /** The core. */
95         private final Core core;
96
97         /** The event bus. */
98         private final EventBus eventBus;
99
100         /** The Freenet interface. */
101         private final FreenetInterface freenetInterface;
102
103         private final SoneModificationDetector soneModificationDetector;
104         private final long delay;
105         private final String soneId;
106
107         /**
108          * Creates a new Sone inserter.
109          *
110          * @param core
111          *            The core
112          * @param eventBus
113          *            The event bus
114          * @param freenetInterface
115          *            The freenet interface
116          * @param soneId
117          *            The ID of the Sone to insert
118          */
119         public SoneInserter(final Core core, EventBus eventBus, FreenetInterface freenetInterface, final String soneId) {
120                 this(core, eventBus, freenetInterface, soneId, new SoneModificationDetector(new LockableFingerprintProvider() {
121                         @Override
122                         public boolean isLocked() {
123                                 final Optional<Sone> sone = core.getSone(soneId);
124                                 if (!sone.isPresent()) {
125                                         return false;
126                                 }
127                                 return core.isLocked(sone.get());
128                         }
129
130                         @Override
131                         public String getFingerprint() {
132                                 final Optional<Sone> sone = core.getSone(soneId);
133                                 if (!sone.isPresent()) {
134                                         return null;
135                                 }
136                                 return sone.get().getFingerprint();
137                         }
138                 }, insertionDelay), 1000);
139         }
140
141         @VisibleForTesting
142         SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, String soneId, SoneModificationDetector soneModificationDetector, long delay) {
143                 super("Sone Inserter for “" + soneId + "”", false);
144                 this.core = core;
145                 this.eventBus = eventBus;
146                 this.freenetInterface = freenetInterface;
147                 this.soneId = soneId;
148                 this.soneModificationDetector = soneModificationDetector;
149                 this.delay = delay;
150         }
151
152         //
153         // ACCESSORS
154         //
155
156         @VisibleForTesting
157         static AtomicInteger getInsertionDelay() {
158                 return insertionDelay;
159         }
160
161         /**
162          * Changes the insertion delay, i.e. the time the Sone inserter waits after it
163          * has noticed a Sone modification before it starts the insert.
164          *
165          * @param insertionDelay
166          *            The insertion delay (in seconds)
167          */
168         public static void setInsertionDelay(int insertionDelay) {
169                 SoneInserter.insertionDelay.set(insertionDelay);
170         }
171
172         /**
173          * Returns the fingerprint of the last insert.
174          *
175          * @return The fingerprint of the last insert
176          */
177         public String getLastInsertFingerprint() {
178                 return soneModificationDetector.getOriginalFingerprint();
179         }
180
181         /**
182          * Sets the fingerprint of the last insert.
183          *
184          * @param lastInsertFingerprint
185          *            The fingerprint of the last insert
186          */
187         public void setLastInsertFingerprint(String lastInsertFingerprint) {
188                 soneModificationDetector.setFingerprint(lastInsertFingerprint);
189         }
190
191         /**
192          * Returns whether the Sone inserter has detected a modification of the
193          * Sone.
194          *
195          * @return {@code true} if the Sone has been modified, {@code false}
196          *         otherwise
197          */
198         public boolean isModified() {
199                 return soneModificationDetector.isModified();
200         }
201
202         //
203         // SERVICE METHODS
204         //
205
206         /**
207          * {@inheritDoc}
208          */
209         @Override
210         protected void serviceRun() {
211                 while (!shouldStop()) {
212                         try {
213                                 /* check every second. */
214                                 sleep(delay);
215
216                                 if (soneModificationDetector.isEligibleForInsert()) {
217                                         Optional<Sone> soneOptional = core.getSone(soneId);
218                                         if (!soneOptional.isPresent()) {
219                                                 logger.log(Level.WARNING, format("Sone %s has disappeared, exiting inserter.", soneId));
220                                                 return;
221                                         }
222                                         Sone sone = soneOptional.get();
223                                         InsertInformation insertInformation = new InsertInformation(sone);
224                                         logger.log(Level.INFO, String.format("Inserting Sone “%s”…", sone.getName()));
225
226                                         boolean success = false;
227                                         try {
228                                                 sone.setStatus(SoneStatus.inserting);
229                                                 long insertTime = currentTimeMillis();
230                                                 eventBus.post(new SoneInsertingEvent(sone));
231                                                 FreenetURI finalUri = freenetInterface.insertDirectory(sone.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
232                                                 eventBus.post(new SoneInsertedEvent(sone, currentTimeMillis() - insertTime));
233                                                 /* at this point we might already be stopped. */
234                                                 if (shouldStop()) {
235                                                         /* if so, bail out, don’t change anything. */
236                                                         break;
237                                                 }
238                                                 sone.setTime(insertTime);
239                                                 sone.setLatestEdition(finalUri.getEdition());
240                                                 core.touchConfiguration();
241                                                 success = true;
242                                                 logger.log(Level.INFO, String.format("Inserted Sone “%s” at %s.", sone.getName(), finalUri));
243                                         } catch (SoneException se1) {
244                                                 eventBus.post(new SoneInsertAbortedEvent(sone, se1));
245                                                 logger.log(Level.WARNING, String.format("Could not insert Sone “%s”!", sone.getName()), se1);
246                                         } finally {
247                                                 sone.setStatus(SoneStatus.idle);
248                                         }
249
250                                         /*
251                                          * reset modification counter if Sone has not been modified
252                                          * while it was inserted.
253                                          */
254                                         if (success) {
255                                                 synchronized (sone) {
256                                                         if (insertInformation.getFingerprint().equals(sone.getFingerprint())) {
257                                                                 logger.log(Level.FINE, String.format("Sone “%s” was not modified further, resetting counter…", sone));
258                                                                 soneModificationDetector.setFingerprint(insertInformation.getFingerprint());
259                                                                 core.touchConfiguration();
260                                                         }
261                                                 }
262                                         }
263                                 }
264                         } catch (Throwable t1) {
265                                 logger.log(Level.SEVERE, "SoneInserter threw an Exception!", t1);
266                         }
267                 }
268         }
269
270         static class SetInsertionDelay implements OptionWatcher<Integer> {
271
272                 @Override
273                 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
274                         setInsertionDelay(newValue);
275                 }
276
277         }
278
279         /**
280          * Container for information that are required to insert a Sone. This
281          * container merely exists to copy all relevant data without holding a lock
282          * on the {@link Sone} object for too long.
283          *
284          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
285          */
286         @VisibleForTesting
287         class InsertInformation {
288
289                 private final String fingerprint;
290
291                 /** All properties of the Sone, copied for thread safety. */
292                 private final Map<String, Object> soneProperties = new HashMap<String, Object>();
293
294                 /**
295                  * Creates a new insert information container.
296                  *
297                  * @param sone
298                  *            The sone to insert
299                  */
300                 public InsertInformation(Sone sone) {
301                         this.fingerprint = sone.getFingerprint();
302                         soneProperties.put("id", sone.getId());
303                         soneProperties.put("name", sone.getName());
304                         soneProperties.put("time", currentTimeMillis());
305                         soneProperties.put("requestUri", sone.getRequestUri());
306                         soneProperties.put("profile", sone.getProfile());
307                         soneProperties.put("posts", Ordering.from(Post.TIME_COMPARATOR).sortedCopy(sone.getPosts()));
308                         soneProperties.put("replies", Ordering.from(Reply.TIME_COMPARATOR).reverse().sortedCopy(sone.getReplies()));
309                         soneProperties.put("likedPostIds", new HashSet<String>(sone.getLikedPostIds()));
310                         soneProperties.put("likedReplyIds", new HashSet<String>(sone.getLikedReplyIds()));
311                         soneProperties.put("albums", FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).filter(NOT_EMPTY).toList());
312                 }
313
314                 //
315                 // ACCESSORS
316                 //
317
318                 @VisibleForTesting
319                 String getFingerprint() {
320                         return fingerprint;
321                 }
322
323                 //
324                 // ACTIONS
325                 //
326
327                 /**
328                  * Generates all manifest entries required to insert this Sone.
329                  *
330                  * @return The manifest entries for the Sone insert
331                  */
332                 public HashMap<String, Object> generateManifestEntries() {
333                         HashMap<String, Object> manifestEntries = new HashMap<String, Object>();
334
335                         /* first, create an index.html. */
336                         manifestEntries.put("index.html", createManifestElement("index.html", "text/html; charset=utf-8", "/templates/insert/index.html"));
337
338                         /* now, store the sone. */
339                         manifestEntries.put("sone.xml", createManifestElement("sone.xml", "text/xml; charset=utf-8", "/templates/insert/sone.xml"));
340
341                         return manifestEntries;
342                 }
343
344                 //
345                 // PRIVATE METHODS
346                 //
347
348                 /**
349                  * Creates a new manifest element.
350                  *
351                  * @param name
352                  *            The name of the file
353                  * @param contentType
354                  *            The content type of the file
355                  * @param templateName
356                  *            The name of the template to render
357                  * @return The manifest element
358                  */
359                 @SuppressWarnings("synthetic-access")
360                 private ManifestElement createManifestElement(String name, String contentType, String templateName) {
361                         InputStreamReader templateInputStreamReader = null;
362                         InputStream templateInputStream = null;
363                         Template template;
364                         try {
365                                 templateInputStream = getClass().getResourceAsStream(templateName);
366                                 templateInputStreamReader = new InputStreamReader(templateInputStream, utf8Charset);
367                                 template = TemplateParser.parse(templateInputStreamReader);
368                         } catch (TemplateException te1) {
369                                 logger.log(Level.SEVERE, String.format("Could not parse template “%s”!", templateName), te1);
370                                 return null;
371                         } finally {
372                                 Closer.close(templateInputStreamReader);
373                                 Closer.close(templateInputStream);
374                         }
375
376                         TemplateContext templateContext = templateContextFactory.createTemplateContext();
377                         templateContext.set("core", core);
378                         templateContext.set("currentSone", soneProperties);
379                         templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition());
380                         templateContext.set("version", SonePlugin.VERSION);
381                         StringWriter writer = new StringWriter();
382                         StringBucket bucket = null;
383                         try {
384                                 template.render(templateContext, writer);
385                                 bucket = new StringBucket(writer.toString(), utf8Charset);
386                                 return new ManifestElement(name, bucket, contentType, bucket.size());
387                         } catch (TemplateException te1) {
388                                 logger.log(Level.SEVERE, String.format("Could not render template “%s”!", templateName), te1);
389                                 return null;
390                         } finally {
391                                 Closer.close(writer);
392                                 if (bucket != null) {
393                                         bucket.free();
394                                 }
395                         }
396                 }
397
398         }
399
400 }