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