Store LatestEdition property only after saving the Sone to disk.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
1 /*
2  * Sone - WebInterface.java - Copyright © 2010 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.web;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.Reader;
24 import java.io.StringReader;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.UUID;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37
38 import net.pterodactylus.sone.core.Core;
39 import net.pterodactylus.sone.core.CoreListener;
40 import net.pterodactylus.sone.data.Album;
41 import net.pterodactylus.sone.data.Image;
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.freenet.L10nFilter;
46 import net.pterodactylus.sone.freenet.wot.Identity;
47 import net.pterodactylus.sone.freenet.wot.Trust;
48 import net.pterodactylus.sone.main.SonePlugin;
49 import net.pterodactylus.sone.notify.ListNotification;
50 import net.pterodactylus.sone.template.AlbumAccessor;
51 import net.pterodactylus.sone.template.CollectionAccessor;
52 import net.pterodactylus.sone.template.CssClassNameFilter;
53 import net.pterodactylus.sone.template.HttpRequestAccessor;
54 import net.pterodactylus.sone.template.IdentityAccessor;
55 import net.pterodactylus.sone.template.ImageLinkFilter;
56 import net.pterodactylus.sone.template.JavascriptFilter;
57 import net.pterodactylus.sone.template.ParserFilter;
58 import net.pterodactylus.sone.template.PostAccessor;
59 import net.pterodactylus.sone.template.ReplyAccessor;
60 import net.pterodactylus.sone.template.ReplyGroupFilter;
61 import net.pterodactylus.sone.template.RequestChangeFilter;
62 import net.pterodactylus.sone.template.SoneAccessor;
63 import net.pterodactylus.sone.template.SubstringFilter;
64 import net.pterodactylus.sone.template.TrustAccessor;
65 import net.pterodactylus.sone.template.UniqueElementFilter;
66 import net.pterodactylus.sone.template.UnknownDateFilter;
67 import net.pterodactylus.sone.text.Part;
68 import net.pterodactylus.sone.text.SonePart;
69 import net.pterodactylus.sone.text.SoneTextParser;
70 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
71 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
72 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
73 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
74 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
75 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
76 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
77 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
78 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
79 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
80 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
81 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
82 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
83 import net.pterodactylus.sone.web.ajax.GetNotificationAjaxPage;
84 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
85 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
86 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
87 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
88 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
89 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
90 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
91 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
92 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
93 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
94 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
95 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
96 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
97 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
98 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
99 import net.pterodactylus.sone.web.page.FreenetRequest;
100 import net.pterodactylus.sone.web.page.PageToadlet;
101 import net.pterodactylus.sone.web.page.PageToadletFactory;
102 import net.pterodactylus.util.cache.Cache;
103 import net.pterodactylus.util.cache.CacheException;
104 import net.pterodactylus.util.cache.CacheItem;
105 import net.pterodactylus.util.cache.DefaultCacheItem;
106 import net.pterodactylus.util.cache.MemoryCache;
107 import net.pterodactylus.util.cache.ValueRetriever;
108 import net.pterodactylus.util.collection.SetBuilder;
109 import net.pterodactylus.util.filter.Filters;
110 import net.pterodactylus.util.logging.Logging;
111 import net.pterodactylus.util.notify.Notification;
112 import net.pterodactylus.util.notify.NotificationManager;
113 import net.pterodactylus.util.notify.TemplateNotification;
114 import net.pterodactylus.util.template.CollectionSortFilter;
115 import net.pterodactylus.util.template.ContainsFilter;
116 import net.pterodactylus.util.template.DateFilter;
117 import net.pterodactylus.util.template.FormatFilter;
118 import net.pterodactylus.util.template.HtmlFilter;
119 import net.pterodactylus.util.template.MatchFilter;
120 import net.pterodactylus.util.template.ModFilter;
121 import net.pterodactylus.util.template.Provider;
122 import net.pterodactylus.util.template.ReflectionAccessor;
123 import net.pterodactylus.util.template.ReplaceFilter;
124 import net.pterodactylus.util.template.StoreFilter;
125 import net.pterodactylus.util.template.Template;
126 import net.pterodactylus.util.template.TemplateContext;
127 import net.pterodactylus.util.template.TemplateContextFactory;
128 import net.pterodactylus.util.template.TemplateException;
129 import net.pterodactylus.util.template.TemplateParser;
130 import net.pterodactylus.util.template.XmlFilter;
131 import net.pterodactylus.util.thread.Ticker;
132 import net.pterodactylus.util.version.Version;
133 import net.pterodactylus.util.web.RedirectPage;
134 import net.pterodactylus.util.web.StaticPage;
135 import net.pterodactylus.util.web.TemplatePage;
136 import freenet.clients.http.SessionManager;
137 import freenet.clients.http.ToadletContainer;
138 import freenet.clients.http.ToadletContext;
139 import freenet.clients.http.SessionManager.Session;
140 import freenet.l10n.BaseL10n;
141 import freenet.support.api.HTTPRequest;
142
143 /**
144  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
145  * references to l10n helpers.
146  *
147  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
148  */
149 public class WebInterface implements CoreListener {
150
151         /** The logger. */
152         private static final Logger logger = Logging.getLogger(WebInterface.class);
153
154         /** The notification manager. */
155         private final NotificationManager notificationManager = new NotificationManager();
156
157         /** The Sone plugin. */
158         private final SonePlugin sonePlugin;
159
160         /** The registered toadlets. */
161         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
162
163         /** The form password. */
164         private final String formPassword;
165
166         /** The template context factory. */
167         private final TemplateContextFactory templateContextFactory;
168
169         /** The Sone text parser. */
170         private final SoneTextParser soneTextParser;
171
172         /** The “new Sone” notification. */
173         private final ListNotification<Sone> newSoneNotification;
174
175         /** The “new post” notification. */
176         private final ListNotification<Post> newPostNotification;
177
178         /** The “new reply” notification. */
179         private final ListNotification<Reply> newReplyNotification;
180
181         /** The invisible “local post” notification. */
182         private final ListNotification<Post> localPostNotification;
183
184         /** The invisible “local reply” notification. */
185         private final ListNotification<Reply> localReplyNotification;
186
187         /** The “you have been mentioned” notification. */
188         private final ListNotification<Post> mentionNotification;
189
190         /** Notifications for sone inserts. */
191         private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
192
193         /** Sone locked notification ticker objects. */
194         private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
195
196         /** The “Sone locked” notification. */
197         private final ListNotification<Sone> lockedSonesNotification;
198
199         /** The “new version” notification. */
200         private final TemplateNotification newVersionNotification;
201
202         /** The “inserting images” notification. */
203         private final ListNotification<Image> insertingImagesNotification;
204
205         /** The “inserted images” notification. */
206         private final ListNotification<Image> insertedImagesNotification;
207
208         /** The “image insert failed” notification. */
209         private final ListNotification<Image> imageInsertFailedNotification;
210
211         /**
212          * Creates a new web interface.
213          *
214          * @param sonePlugin
215          *            The Sone plugin
216          */
217         @SuppressWarnings("synthetic-access")
218         public WebInterface(SonePlugin sonePlugin) {
219                 this.sonePlugin = sonePlugin;
220                 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
221                 soneTextParser = new SoneTextParser(getCore(), getCore());
222
223                 templateContextFactory = new TemplateContextFactory();
224                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
225                 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
226                 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
227                 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
228                 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
229                 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
230                 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
231                 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
232                 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
233                 templateContextFactory.addFilter("date", new DateFilter());
234                 templateContextFactory.addFilter("html", new HtmlFilter());
235                 templateContextFactory.addFilter("replace", new ReplaceFilter());
236                 templateContextFactory.addFilter("store", new StoreFilter());
237                 templateContextFactory.addFilter("l10n", new L10nFilter(getL10n()));
238                 templateContextFactory.addFilter("substring", new SubstringFilter());
239                 templateContextFactory.addFilter("xml", new XmlFilter());
240                 templateContextFactory.addFilter("change", new RequestChangeFilter());
241                 templateContextFactory.addFilter("match", new MatchFilter());
242                 templateContextFactory.addFilter("css", new CssClassNameFilter());
243                 templateContextFactory.addFilter("js", new JavascriptFilter());
244                 templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory, soneTextParser));
245                 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
246                 templateContextFactory.addFilter("format", new FormatFilter());
247                 templateContextFactory.addFilter("sort", new CollectionSortFilter());
248                 templateContextFactory.addFilter("image-link", new ImageLinkFilter(templateContextFactory));
249                 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
250                 templateContextFactory.addFilter("in", new ContainsFilter());
251                 templateContextFactory.addFilter("unique", new UniqueElementFilter());
252                 templateContextFactory.addFilter("mod", new ModFilter());
253                 templateContextFactory.addProvider(Provider.TEMPLATE_CONTEXT_PROVIDER);
254                 templateContextFactory.addProvider(new ClassPathTemplateProvider());
255                 templateContextFactory.addTemplateObject("webInterface", this);
256                 templateContextFactory.addTemplateObject("formPassword", formPassword);
257
258                 /* create notifications. */
259                 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
260                 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
261
262                 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
263                 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
264
265                 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
266                 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
267
268                 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
269                 newReplyNotification = new ListNotification<Reply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
270
271                 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
272                 localReplyNotification = new ListNotification<Reply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
273
274                 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
275                 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
276
277                 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
278                 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
279
280                 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
281                 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
282
283                 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
284                 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
285
286                 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
287                 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
288
289                 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
290                 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
291         }
292
293         //
294         // ACCESSORS
295         //
296
297         /**
298          * Returns the Sone core used by the Sone plugin.
299          *
300          * @return The Sone core
301          */
302         public Core getCore() {
303                 return sonePlugin.core();
304         }
305
306         /**
307          * Returns the template context factory of the web interface.
308          *
309          * @return The template context factory
310          */
311         public TemplateContextFactory getTemplateContextFactory() {
312                 return templateContextFactory;
313         }
314
315         /**
316          * Returns the current session, creating a new session if there is no
317          * current session.
318          *
319          * @param toadletContenxt
320          *            The toadlet context
321          * @return The current session, or {@code null} if there is no current
322          *         session
323          */
324         public Session getCurrentSession(ToadletContext toadletContenxt) {
325                 return getCurrentSession(toadletContenxt, true);
326         }
327
328         /**
329          * Returns the current session, creating a new session if there is no
330          * current session and {@code create} is {@code true}.
331          *
332          * @param toadletContenxt
333          *            The toadlet context
334          * @param create
335          *            {@code true} to create a new session if there is no current
336          *            session, {@code false} otherwise
337          * @return The current session, or {@code null} if there is no current
338          *         session
339          */
340         public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
341                 Session session = getSessionManager().useSession(toadletContenxt);
342                 if (create && (session == null)) {
343                         session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
344                 }
345                 return session;
346         }
347
348         /**
349          * Returns the currently logged in Sone.
350          *
351          * @param toadletContext
352          *            The toadlet context
353          * @return The currently logged in Sone, or {@code null} if no Sone is
354          *         currently logged in
355          */
356         public Sone getCurrentSone(ToadletContext toadletContext) {
357                 return getCurrentSone(toadletContext, true);
358         }
359
360         /**
361          * Returns the currently logged in Sone.
362          *
363          * @param toadletContext
364          *            The toadlet context
365          * @param create
366          *            {@code true} to create a new session if no session exists,
367          *            {@code false} to not create a new session
368          * @return The currently logged in Sone, or {@code null} if no Sone is
369          *         currently logged in
370          */
371         public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
372                 Set<Sone> localSones = getCore().getLocalSones();
373                 if (localSones.size() == 1) {
374                         return localSones.iterator().next();
375                 }
376                 return getCurrentSone(getCurrentSession(toadletContext, create));
377         }
378
379         /**
380          * Returns the currently logged in Sone.
381          *
382          * @param session
383          *            The session
384          * @return The currently logged in Sone, or {@code null} if no Sone is
385          *         currently logged in
386          */
387         public Sone getCurrentSone(Session session) {
388                 if (session == null) {
389                         return null;
390                 }
391                 String soneId = (String) session.getAttribute("Sone.CurrentSone");
392                 if (soneId == null) {
393                         return null;
394                 }
395                 return getCore().getLocalSone(soneId, false);
396         }
397
398         /**
399          * Sets the currently logged in Sone.
400          *
401          * @param toadletContext
402          *            The toadlet context
403          * @param sone
404          *            The Sone to set as currently logged in
405          */
406         public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
407                 Session session = getCurrentSession(toadletContext);
408                 if (sone == null) {
409                         session.removeAttribute("Sone.CurrentSone");
410                 } else {
411                         session.setAttribute("Sone.CurrentSone", sone.getId());
412                 }
413         }
414
415         /**
416          * Returns the notification manager.
417          *
418          * @return The notification manager
419          */
420         public NotificationManager getNotifications() {
421                 return notificationManager;
422         }
423
424         /**
425          * Returns the l10n helper of the node.
426          *
427          * @return The node’s l10n helper
428          */
429         public BaseL10n getL10n() {
430                 return sonePlugin.l10n().getBase();
431         }
432
433         /**
434          * Returns the session manager of the node.
435          *
436          * @return The node’s session manager
437          */
438         public SessionManager getSessionManager() {
439                 return sonePlugin.pluginRespirator().getSessionManager("Sone");
440         }
441
442         /**
443          * Returns the node’s form password.
444          *
445          * @return The form password
446          */
447         public String getFormPassword() {
448                 return formPassword;
449         }
450
451         /**
452          * Returns the posts that have been announced as new in the
453          * {@link #newPostNotification}.
454          *
455          * @return The new posts
456          */
457         public Set<Post> getNewPosts() {
458                 return new SetBuilder<Post>().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).get();
459         }
460
461         /**
462          * Returns the replies that have been announced as new in the
463          * {@link #newReplyNotification}.
464          *
465          * @return The new replies
466          */
467         public Set<Reply> getNewReplies() {
468                 return new SetBuilder<Reply>().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).get();
469         }
470
471         /**
472          * Sets whether the current start of the plugin is the first start. It is
473          * considered a first start if the configuration file does not exist.
474          *
475          * @param firstStart
476          *            {@code true} if no configuration file existed when Sone was
477          *            loaded, {@code false} otherwise
478          */
479         public void setFirstStart(boolean firstStart) {
480                 if (firstStart) {
481                         Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
482                         Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
483                         notificationManager.addNotification(firstStartNotification);
484                 }
485         }
486
487         /**
488          * Sets whether Sone was started with a fresh configuration file.
489          *
490          * @param newConfig
491          *            {@code true} if Sone was started with a fresh configuration,
492          *            {@code false} if the existing configuration could be read
493          */
494         public void setNewConfig(boolean newConfig) {
495                 if (newConfig && !hasFirstStartNotification()) {
496                         Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
497                         Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
498                         notificationManager.addNotification(configNotReadNotification);
499                 }
500         }
501
502         //
503         // PRIVATE ACCESSORS
504         //
505
506         /**
507          * Returns whether the first start notification is currently displayed.
508          *
509          * @return {@code true} if the first-start notification is currently
510          *         displayed, {@code false} otherwise
511          */
512         private boolean hasFirstStartNotification() {
513                 return notificationManager.getNotification("first-start-notification") != null;
514         }
515
516         //
517         // ACTIONS
518         //
519
520         /**
521          * Starts the web interface and registers all toadlets.
522          */
523         public void start() {
524                 registerToadlets();
525
526                 /* notification templates. */
527                 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
528
529                 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
530                 notificationManager.addNotification(startupNotification);
531
532                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
533
534                         @Override
535                         public void run() {
536                                 startupNotification.dismiss();
537                         }
538                 }, "Sone Startup Notification Remover");
539
540                 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
541                 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
542                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
543
544                         @Override
545                         @SuppressWarnings("synthetic-access")
546                         public void run() {
547                                 if (getCore().getIdentityManager().isConnected()) {
548                                         wotMissingNotification.dismiss();
549                                 } else {
550                                         notificationManager.addNotification(wotMissingNotification);
551                                 }
552                                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
553                         }
554
555                 }, "Sone WoT Connector Checker");
556         }
557
558         /**
559          * Stops the web interface and unregisters all toadlets.
560          */
561         public void stop() {
562                 unregisterToadlets();
563                 Ticker.getInstance().stop();
564         }
565
566         //
567         // PRIVATE METHODS
568         //
569
570         /**
571          * Register all toadlets.
572          */
573         private void registerToadlets() {
574                 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
575                 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
576                 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
577                 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
578                 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
579                 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
580                 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
581                 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
582                 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
583                 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
584                 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
585                 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
586                 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
587                 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
588                 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
589                 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
590                 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
591                 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
592                 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
593                 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
594                 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
595                 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
596                 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
597                 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
598                 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
599                 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
600                 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
601                 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
602                 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
603
604                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
605                 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
606                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
607                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
608                 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
609                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
610                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
611                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
612                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
613                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
614                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
615                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
616                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
617                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
618                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
619                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
620                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
621                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
622                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
623                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
624                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
625                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
626                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
627                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
628                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
629                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
630                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
631                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
632                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
633                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
634                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
635                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
636                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
637                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
638                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
639                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
640                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
641                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
642                 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
643                 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
644                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
645                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
646                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
647                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
648                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
649                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
650                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
651                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
652                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
653                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
654                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
655                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationAjaxPage(this)));
656                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
657                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
658                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
659                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
660                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
661                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
662                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
663                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
664                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
665                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
666                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
667                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
668                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
669                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
670                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this)));
671                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
672                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
673                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
674                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
675                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
676                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
677                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
678                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
679                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
680                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
681                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
682
683                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
684                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
685                 for (PageToadlet toadlet : pageToadlets) {
686                         String menuName = toadlet.getMenuName();
687                         if (menuName != null) {
688                                 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
689                         } else {
690                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
691                         }
692                 }
693         }
694
695         /**
696          * Unregisters all toadlets.
697          */
698         private void unregisterToadlets() {
699                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
700                 for (PageToadlet pageToadlet : pageToadlets) {
701                         toadletContainer.unregister(pageToadlet);
702                 }
703                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
704         }
705
706         /**
707          * Creates a {@link Reader} from the {@link InputStream} for the resource
708          * with the given name.
709          *
710          * @param resourceName
711          *            The name of the resource
712          * @return A {@link Reader} for the resource
713          */
714         private Reader createReader(String resourceName) {
715                 try {
716                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
717                 } catch (UnsupportedEncodingException uee1) {
718                         return null;
719                 }
720         }
721
722         /**
723          * Returns all {@link Core#isLocalSone(Sone) local Sone}s that are
724          * referenced by {@link SonePart}s in the given text (after parsing it using
725          * {@link SoneTextParser}).
726          *
727          * @param text
728          *            The text to parse
729          * @return All mentioned local Sones
730          */
731         private Set<Sone> getMentionedSones(String text) {
732                 /* we need no context to find mentioned Sones. */
733                 Set<Sone> mentionedSones = new HashSet<Sone>();
734                 try {
735                         for (Part part : soneTextParser.parse(null, new StringReader(text))) {
736                                 if (part instanceof SonePart) {
737                                         mentionedSones.add(((SonePart) part).getSone());
738                                 }
739                         }
740                 } catch (IOException ioe1) {
741                         logger.log(Level.WARNING, "Could not parse post text: " + text, ioe1);
742                 }
743                 return Filters.filteredSet(mentionedSones, Sone.LOCAL_SONE_FILTER);
744         }
745
746         /**
747          * Returns the Sone insert notification for the given Sone. If no
748          * notification for the given Sone exists, a new notification is created and
749          * cached.
750          *
751          * @param sone
752          *            The Sone to get the insert notification for
753          * @return The Sone insert notification
754          */
755         private TemplateNotification getSoneInsertNotification(Sone sone) {
756                 synchronized (soneInsertNotifications) {
757                         TemplateNotification templateNotification = soneInsertNotifications.get(sone);
758                         if (templateNotification == null) {
759                                 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
760                                 templateNotification.set("insertSone", sone);
761                                 soneInsertNotifications.put(sone, templateNotification);
762                         }
763                         return templateNotification;
764                 }
765         }
766
767         //
768         // CORELISTENER METHODS
769         //
770
771         /**
772          * {@inheritDoc}
773          */
774         @Override
775         public void newSoneFound(Sone sone) {
776                 newSoneNotification.add(sone);
777                 if (!hasFirstStartNotification()) {
778                         notificationManager.addNotification(newSoneNotification);
779                 }
780         }
781
782         /**
783          * {@inheritDoc}
784          */
785         @Override
786         public void newPostFound(Post post) {
787                 boolean isLocal = getCore().isLocalSone(post.getSone());
788                 if (isLocal) {
789                         localPostNotification.add(post);
790                 } else {
791                         newPostNotification.add(post);
792                 }
793                 if (!hasFirstStartNotification()) {
794                         notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
795                         if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
796                                 mentionNotification.add(post);
797                                 notificationManager.addNotification(mentionNotification);
798                         }
799                 } else {
800                         getCore().markPostKnown(post);
801                 }
802         }
803
804         /**
805          * {@inheritDoc}
806          */
807         @Override
808         public void newReplyFound(Reply reply) {
809                 boolean isLocal = getCore().isLocalSone(reply.getSone());
810                 if (isLocal) {
811                         localReplyNotification.add(reply);
812                 } else {
813                         newReplyNotification.add(reply);
814                 }
815                 if (!hasFirstStartNotification()) {
816                         notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
817                         if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
818                                 mentionNotification.add(reply.getPost());
819                                 notificationManager.addNotification(mentionNotification);
820                         }
821                 } else {
822                         getCore().markReplyKnown(reply);
823                 }
824         }
825
826         /**
827          * {@inheritDoc}
828          */
829         @Override
830         public void markSoneKnown(Sone sone) {
831                 newSoneNotification.remove(sone);
832         }
833
834         /**
835          * {@inheritDoc}
836          */
837         @Override
838         public void markPostKnown(Post post) {
839                 newPostNotification.remove(post);
840                 localPostNotification.remove(post);
841                 mentionNotification.remove(post);
842         }
843
844         /**
845          * {@inheritDoc}
846          */
847         @Override
848         public void markReplyKnown(Reply reply) {
849                 newReplyNotification.remove(reply);
850                 localReplyNotification.remove(reply);
851                 mentionNotification.remove(reply.getPost());
852         }
853
854         /**
855          * {@inheritDoc}
856          */
857         @Override
858         public void soneRemoved(Sone sone) {
859                 newSoneNotification.remove(sone);
860         }
861
862         /**
863          * {@inheritDoc}
864          */
865         @Override
866         public void postRemoved(Post post) {
867                 newPostNotification.remove(post);
868                 localPostNotification.remove(post);
869         }
870
871         /**
872          * {@inheritDoc}
873          */
874         @Override
875         public void replyRemoved(Reply reply) {
876                 newReplyNotification.remove(reply);
877                 localReplyNotification.remove(reply);
878         }
879
880         /**
881          * {@inheritDoc}
882          */
883         @Override
884         public void soneLocked(final Sone sone) {
885                 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
886
887                         @Override
888                         @SuppressWarnings("synthetic-access")
889                         public void run() {
890                                 lockedSonesNotification.add(sone);
891                                 lockedSonesTickerObjects.remove(sone);
892                                 notificationManager.addNotification(lockedSonesNotification);
893                         }
894                 }, "Sone Locked Notification");
895                 lockedSonesTickerObjects.put(sone, tickerObject);
896         }
897
898         /**
899          * {@inheritDoc}
900          */
901         @Override
902         public void soneUnlocked(Sone sone) {
903                 lockedSonesNotification.remove(sone);
904                 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
905         }
906
907         /**
908          * {@inheritDoc}
909          */
910         @Override
911         public void soneInserting(Sone sone) {
912                 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
913                 soneInsertNotification.set("soneStatus", "inserting");
914                 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
915                         notificationManager.addNotification(soneInsertNotification);
916                 }
917         }
918
919         /**
920          * {@inheritDoc}
921          */
922         @Override
923         public void soneInserted(Sone sone, long insertDuration) {
924                 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
925                 soneInsertNotification.set("soneStatus", "inserted");
926                 soneInsertNotification.set("insertDuration", insertDuration / 1000);
927                 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
928                         notificationManager.addNotification(soneInsertNotification);
929                 }
930         }
931
932         /**
933          * {@inheritDoc}
934          */
935         @Override
936         public void soneInsertAborted(Sone sone, Throwable cause) {
937                 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
938                 soneInsertNotification.set("soneStatus", "insert-aborted");
939                 soneInsertNotification.set("insert-error", cause);
940                 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
941                         notificationManager.addNotification(soneInsertNotification);
942                 }
943         }
944
945         /**
946          * {@inheritDoc}
947          */
948         @Override
949         public void updateFound(Version version, long releaseTime, long latestEdition) {
950                 newVersionNotification.getTemplateContext().set("latestVersion", version);
951                 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
952                 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
953                 notificationManager.addNotification(newVersionNotification);
954         }
955
956         /**
957          * {@inheritDoc}
958          */
959         @Override
960         public void imageInsertStarted(Image image) {
961                 insertingImagesNotification.add(image);
962                 notificationManager.addNotification(insertingImagesNotification);
963         }
964
965         /**
966          * {@inheritDoc}
967          */
968         @Override
969         public void imageInsertAborted(Image image) {
970                 insertingImagesNotification.remove(image);
971         }
972
973         /**
974          * {@inheritDoc}
975          */
976         @Override
977         public void imageInsertFinished(Image image) {
978                 insertingImagesNotification.remove(image);
979                 insertedImagesNotification.add(image);
980                 notificationManager.addNotification(insertedImagesNotification);
981         }
982
983         /**
984          * {@inheritDoc}
985          */
986         @Override
987         public void imageInsertFailed(Image image, Throwable cause) {
988                 insertingImagesNotification.remove(image);
989                 imageInsertFailedNotification.add(image);
990                 notificationManager.addNotification(imageInsertFailedNotification);
991         }
992
993         /**
994          * Template provider implementation that uses
995          * {@link WebInterface#createReader(String)} to load templates for
996          * inclusion.
997          *
998          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
999          */
1000         private class ClassPathTemplateProvider implements Provider {
1001
1002                 /** Cache for templates. */
1003                 private final Cache<String, Template> templateCache = new MemoryCache<String, Template>(new ValueRetriever<String, Template>() {
1004
1005                         @Override
1006                         @SuppressWarnings("synthetic-access")
1007                         public CacheItem<Template> retrieve(String key) throws CacheException {
1008                                 Template template = findTemplate(key);
1009                                 if (template != null) {
1010                                         return new DefaultCacheItem<Template>(template);
1011                                 }
1012                                 return null;
1013                         }
1014                 });
1015
1016                 /**
1017                  * {@inheritDoc}
1018                  */
1019                 @Override
1020                 @SuppressWarnings("synthetic-access")
1021                 public Template getTemplate(TemplateContext templateContext, String templateName) {
1022                         try {
1023                                 return templateCache.get(templateName);
1024                         } catch (CacheException ce1) {
1025                                 logger.log(Level.WARNING, "Could not get template for " + templateName + "!", ce1);
1026                                 return null;
1027                         }
1028                 }
1029
1030                 /**
1031                  * Locates a template in the class path.
1032                  *
1033                  * @param templateName
1034                  *            The name of the template to load
1035                  * @return The loaded template, or {@code null} if no template could be
1036                  *         found
1037                  */
1038                 @SuppressWarnings("synthetic-access")
1039                 private Template findTemplate(String templateName) {
1040                         Reader templateReader = createReader("/templates/" + templateName);
1041                         if (templateReader == null) {
1042                                 return null;
1043                         }
1044                         Template template = null;
1045                         try {
1046                                 template = TemplateParser.parse(templateReader);
1047                         } catch (TemplateException te1) {
1048                                 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);
1049                         }
1050                         return template;
1051                 }
1052
1053         }
1054
1055 }