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