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