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