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