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