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