+ } catch (Throwable t1) {
+ logger.log(Level.SEVERE, "SoneInserter threw an Exception!", t1);
+ }
+ }
+ }
+
+ @Subscribe
+ public void insertionDelayChanged(InsertionDelayChangedEvent insertionDelayChangedEvent) {
+ setInsertionDelay(insertionDelayChangedEvent.getInsertionDelay());
+ }
+
+ /**
+ * Container for information that are required to insert a Sone. This
+ * container merely exists to copy all relevant data without holding a lock
+ * on the {@link Sone} object for too long.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+ @VisibleForTesting
+ class InsertInformation implements Closeable {
+
+ /** All properties of the Sone, copied for thread safety. */
+ private final Map<String, Object> soneProperties = new HashMap<String, Object>();
+ private final String fingerprint;
+ private final ManifestCreator manifestCreator;
+
+ /**
+ * Creates a new insert information container.
+ *
+ * @param sone
+ * The sone to insert
+ */
+ public InsertInformation(Sone sone) {
+ this.fingerprint = sone.getFingerprint();
+ Map<String, Object> soneProperties = new HashMap<String, Object>();
+ soneProperties.put("id", sone.getId());
+ soneProperties.put("name", sone.getName());
+ soneProperties.put("time", currentTimeMillis());
+ soneProperties.put("requestUri", sone.getRequestUri());
+ soneProperties.put("profile", sone.getProfile());
+ soneProperties.put("posts", Ordering.from(Post.TIME_COMPARATOR).sortedCopy(sone.getPosts()));
+ soneProperties.put("replies", Ordering.from(Reply.TIME_COMPARATOR).reverse().sortedCopy(sone.getReplies()));
+ soneProperties.put("likedPostIds", new HashSet<String>(sone.getLikedPostIds()));
+ soneProperties.put("likedReplyIds", new HashSet<String>(sone.getLikedReplyIds()));
+ soneProperties.put("albums", FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).filter(NOT_EMPTY).toList());
+ manifestCreator = new ManifestCreator(core, soneProperties);
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ @VisibleForTesting
+ String getFingerprint() {
+ return fingerprint;
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Generates all manifest entries required to insert this Sone.
+ *
+ * @return The manifest entries for the Sone insert
+ */
+ public HashMap<String, Object> generateManifestEntries() {
+ HashMap<String, Object> manifestEntries = new HashMap<String, Object>();
+
+ /* first, create an index.html. */
+ manifestEntries.put("index.html", manifestCreator.createManifestElement(
+ "index.html", "text/html; charset=utf-8",
+ "/templates/insert/index.html"));
+
+ /* now, store the sone. */
+ manifestEntries.put("sone.xml", manifestCreator.createManifestElement(
+ "sone.xml", "text/xml; charset=utf-8",
+ "/templates/insert/sone.xml"));
+
+ return manifestEntries;
+ }
+
+ @Override
+ public void close() {
+ manifestCreator.close();
+ }
+
+ }
+
+ /**
+ * Creates manifest elements for an insert by rendering a template.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+ @VisibleForTesting
+ static class ManifestCreator implements Closeable {
+
+ private final Core core;
+ private final Map<String, Object> soneProperties;
+ private final Set<Bucket> buckets = new HashSet<Bucket>();
+
+ ManifestCreator(Core core, Map<String, Object> soneProperties) {
+ this.core = core;
+ this.soneProperties = soneProperties;
+ }
+
+ public ManifestElement createManifestElement(String name, String contentType, String templateName) {
+ InputStreamReader templateInputStreamReader = null;
+ InputStream templateInputStream = null;
+ Template template;
+ try {
+ templateInputStream = getClass().getResourceAsStream(templateName);
+ templateInputStreamReader = new InputStreamReader(templateInputStream, utf8Charset);
+ template = TemplateParser.parse(templateInputStreamReader);
+ } catch (TemplateException te1) {
+ logger.log(Level.SEVERE, String.format("Could not parse template “%s”!", templateName), te1);
+ return null;
+ } finally {
+ Closer.close(templateInputStreamReader);
+ Closer.close(templateInputStream);
+ }
+
+ TemplateContext templateContext = templateContextFactory.createTemplateContext();
+ templateContext.set("core", core);
+ templateContext.set("currentSone", soneProperties);
+ templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition());
+ templateContext.set("version", SonePlugin.VERSION);
+ StringWriter writer = new StringWriter();
+ try {
+ template.render(templateContext, writer);
+ RandomAccessBucket bucket = new ArrayBucket(writer.toString().getBytes(Charsets.UTF_8));
+ buckets.add(bucket);
+ return new ManifestElement(name, bucket, contentType, bucket.size());
+ } catch (TemplateException te1) {
+ logger.log(Level.SEVERE, String.format("Could not render template “%s”!", templateName), te1);
+ return null;
+ } finally {
+ Closer.close(writer);