Add AJAX handler for creating a post.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
1 /*
2  * FreenetSone - 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.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.io.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 import net.pterodactylus.sone.core.Core;
33 import net.pterodactylus.sone.core.CoreListener;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.Reply;
36 import net.pterodactylus.sone.data.Sone;
37 import net.pterodactylus.sone.freenet.L10nFilter;
38 import net.pterodactylus.sone.freenet.wot.Identity;
39 import net.pterodactylus.sone.main.SonePlugin;
40 import net.pterodactylus.sone.notify.ListNotification;
41 import net.pterodactylus.sone.template.CollectionAccessor;
42 import net.pterodactylus.sone.template.CssClassNameFilter;
43 import net.pterodactylus.sone.template.GetPagePlugin;
44 import net.pterodactylus.sone.template.IdentityAccessor;
45 import net.pterodactylus.sone.template.NotificationManagerAccessor;
46 import net.pterodactylus.sone.template.PostAccessor;
47 import net.pterodactylus.sone.template.ReplyAccessor;
48 import net.pterodactylus.sone.template.RequestChangeFilter;
49 import net.pterodactylus.sone.template.SoneAccessor;
50 import net.pterodactylus.sone.template.SubstringFilter;
51 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
52 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
53 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
54 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
55 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
56 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
57 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
58 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
59 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
60 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
61 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
62 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
63 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
64 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
65 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
66 import net.pterodactylus.sone.web.page.PageToadlet;
67 import net.pterodactylus.sone.web.page.PageToadletFactory;
68 import net.pterodactylus.sone.web.page.StaticPage;
69 import net.pterodactylus.util.logging.Logging;
70 import net.pterodactylus.util.notify.NotificationManager;
71 import net.pterodactylus.util.notify.TemplateNotification;
72 import net.pterodactylus.util.template.DateFilter;
73 import net.pterodactylus.util.template.DefaultTemplateFactory;
74 import net.pterodactylus.util.template.MatchFilter;
75 import net.pterodactylus.util.template.PaginationPlugin;
76 import net.pterodactylus.util.template.ReflectionAccessor;
77 import net.pterodactylus.util.template.Template;
78 import net.pterodactylus.util.template.TemplateException;
79 import net.pterodactylus.util.template.TemplateFactory;
80 import net.pterodactylus.util.template.TemplateProvider;
81 import net.pterodactylus.util.template.XmlFilter;
82 import net.pterodactylus.util.thread.Ticker;
83 import freenet.clients.http.SessionManager;
84 import freenet.clients.http.ToadletContainer;
85 import freenet.l10n.BaseL10n;
86
87 /**
88  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
89  * references to l10n helpers.
90  *
91  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
92  */
93 public class WebInterface implements CoreListener {
94
95         /** The logger. */
96         private static final Logger logger = Logging.getLogger(WebInterface.class);
97
98         /** The notification manager. */
99         private final NotificationManager notificationManager = new NotificationManager();
100
101         /** The Sone plugin. */
102         private final SonePlugin sonePlugin;
103
104         /** The registered toadlets. */
105         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
106
107         /** The form password. */
108         private final String formPassword;
109
110         /** The template factory. */
111         private DefaultTemplateFactory templateFactory;
112
113         /** The “new Sone” notification. */
114         private final ListNotification<Sone> newSoneNotification;
115
116         /** The “new post” notification. */
117         private final ListNotification<Post> newPostNotification;
118
119         /** The “new reply” notification. */
120         private final ListNotification<Reply> newReplyNotification;
121
122         /** The “rescuing Sone” notification. */
123         private final ListNotification<Sone> rescuingSonesNotification;
124
125         /** The “Sone rescued” notification. */
126         private final ListNotification<Sone> sonesRescuedNotification;
127
128         /**
129          * Creates a new web interface.
130          *
131          * @param sonePlugin
132          *            The Sone plugin
133          */
134         public WebInterface(SonePlugin sonePlugin) {
135                 this.sonePlugin = sonePlugin;
136                 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
137
138                 templateFactory = new DefaultTemplateFactory();
139                 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
140                 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
141                 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
142                 templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
143                 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
144                 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
145                 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
146                 templateFactory.addFilter("date", new DateFilter());
147                 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
148                 templateFactory.addFilter("substring", new SubstringFilter());
149                 templateFactory.addFilter("xml", new XmlFilter());
150                 templateFactory.addFilter("change", new RequestChangeFilter());
151                 templateFactory.addFilter("match", new MatchFilter());
152                 templateFactory.addFilter("css", new CssClassNameFilter());
153                 templateFactory.addPlugin("getpage", new GetPagePlugin());
154                 templateFactory.addPlugin("paginate", new PaginationPlugin());
155                 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
156                 templateFactory.addTemplateObject("formPassword", formPassword);
157
158                 /* create notifications. */
159                 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
160                 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
161
162                 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
163                 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
164
165                 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
166                 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
167
168                 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
169                 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
170
171                 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
172                 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
173         }
174
175         //
176         // ACCESSORS
177         //
178
179         /**
180          * Returns the Sone core used by the Sone plugin.
181          *
182          * @return The Sone core
183          */
184         public Core getCore() {
185                 return sonePlugin.core();
186         }
187
188         /**
189          * Returns the notification manager.
190          *
191          * @return The notification manager
192          */
193         public NotificationManager getNotifications() {
194                 return notificationManager;
195         }
196
197         /**
198          * Returns the l10n helper of the node.
199          *
200          * @return The node’s l10n helper
201          */
202         public BaseL10n getL10n() {
203                 return sonePlugin.l10n().getBase();
204         }
205
206         /**
207          * Returns the session manager of the node.
208          *
209          * @return The node’s session manager
210          */
211         public SessionManager getSessionManager() {
212                 return sonePlugin.pluginRespirator().getSessionManager("Sone");
213         }
214
215         /**
216          * Returns the node’s form password.
217          *
218          * @return The form password
219          */
220         public String getFormPassword() {
221                 return formPassword;
222         }
223
224         /**
225          * Returns the posts that have been announced as new in the
226          * {@link #newPostNotification}.
227          *
228          * @return The new posts
229          */
230         public Set<Post> getNewPosts() {
231                 return new HashSet<Post>(newPostNotification.getElements());
232         }
233
234         /**
235          * Returns the replies that have been announced as new in the
236          * {@link #newReplyNotification}.
237          *
238          * @return The new replies
239          */
240         public Set<Reply> getNewReplies() {
241                 return new HashSet<Reply>(newReplyNotification.getElements());
242         }
243
244         //
245         // ACTIONS
246         //
247
248         /**
249          * Starts the web interface and registers all toadlets.
250          */
251         public void start() {
252                 registerToadlets();
253
254                 /* notification templates. */
255                 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
256
257                 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
258                 notificationManager.addNotification(startupNotification);
259
260                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
261
262                         @Override
263                         public void run() {
264                                 startupNotification.dismiss();
265                         }
266                 }, "Sone Startup Notification Remover");
267
268                 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
269                 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
270                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
271
272                         @Override
273                         @SuppressWarnings("synthetic-access")
274                         public void run() {
275                                 if (getCore().getIdentityManager().isConnected()) {
276                                         wotMissingNotification.dismiss();
277                                 } else {
278                                         notificationManager.addNotification(wotMissingNotification);
279                                 }
280                                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
281                         }
282
283                 }, "Sone WoT Connector Checker");
284         }
285
286         /**
287          * Stops the web interface and unregisters all toadlets.
288          */
289         public void stop() {
290                 unregisterToadlets();
291                 Ticker.getInstance().stop();
292         }
293
294         //
295         // PRIVATE METHODS
296         //
297
298         /**
299          * Register all toadlets.
300          */
301         private void registerToadlets() {
302                 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
303                 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
304                 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
305                 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
306                 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
307                 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
308                 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
309                 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
310                 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
311                 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
312                 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
313                 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
314                 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
315                 Template lockSoneTemplate = templateFactory.createTemplate(createReader("/templates/lockSone.html"));
316                 Template unlockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unlockSone.html"));
317                 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
318                 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
319                 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
320                 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
321                 Template dismissNotificationTemplate = templateFactory.createTemplate(createReader("/templates/dismissNotification.html"));
322                 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
323                 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
324                 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
325                 Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
326                 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
327
328                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
329                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
330                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
331                 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
332                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
333                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
334                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
335                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
336                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
337                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(likePostTemplate, this)));
338                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
339                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
340                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
341                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(lockSoneTemplate, this)));
342                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(unlockSoneTemplate, this)));
343                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
344                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
345                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
346                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
347                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
348                 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
349                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
350                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
351                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(dismissNotificationTemplate, this)));
352                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
353                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
354                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
355                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
356                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
357                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
358                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
359                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
360                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
361                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
362                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
363                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
364                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
365                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
366                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
367                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
368                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
369                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
370
371                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
372                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
373                 for (PageToadlet toadlet : pageToadlets) {
374                         String menuName = toadlet.getMenuName();
375                         if (menuName != null) {
376                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
377                         } else {
378                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
379                         }
380                 }
381         }
382
383         /**
384          * Unregisters all toadlets.
385          */
386         private void unregisterToadlets() {
387                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
388                 for (PageToadlet pageToadlet : pageToadlets) {
389                         toadletContainer.unregister(pageToadlet);
390                 }
391                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
392         }
393
394         /**
395          * Creates a {@link Reader} from the {@link InputStream} for the resource
396          * with the given name.
397          *
398          * @param resourceName
399          *            The name of the resource
400          * @return A {@link Reader} for the resource
401          */
402         private Reader createReader(String resourceName) {
403                 try {
404                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
405                 } catch (UnsupportedEncodingException uee1) {
406                         return null;
407                 }
408         }
409
410         //
411         // CORELISTENER METHODS
412         //
413
414         /**
415          * {@inheritDoc}
416          */
417         @Override
418         public void rescuingSone(Sone sone) {
419                 rescuingSonesNotification.add(sone);
420                 notificationManager.addNotification(rescuingSonesNotification);
421         }
422
423         /**
424          * {@inheritDoc}
425          */
426         @Override
427         public void rescuedSone(Sone sone) {
428                 rescuingSonesNotification.remove(sone);
429                 sonesRescuedNotification.add(sone);
430                 notificationManager.addNotification(sonesRescuedNotification);
431         }
432
433         /**
434          * {@inheritDoc}
435          */
436         @Override
437         public void newSoneFound(Sone sone) {
438                 newSoneNotification.add(sone);
439                 notificationManager.addNotification(newSoneNotification);
440         }
441
442         /**
443          * {@inheritDoc}
444          */
445         @Override
446         public void newPostFound(Post post) {
447                 newPostNotification.add(post);
448                 notificationManager.addNotification(newPostNotification);
449         }
450
451         /**
452          * {@inheritDoc}
453          */
454         @Override
455         public void newReplyFound(Reply reply) {
456                 if (reply.getPost().getSone() == null) {
457                         return;
458                 }
459                 newReplyNotification.add(reply);
460                 notificationManager.addNotification(newReplyNotification);
461         }
462
463         /**
464          * {@inheritDoc}
465          */
466         @Override
467         public void markSoneKnown(Sone sone) {
468                 newSoneNotification.remove(sone);
469         }
470
471         /**
472          * {@inheritDoc}
473          */
474         @Override
475         public void markPostKnown(Post post) {
476                 newPostNotification.remove(post);
477         }
478
479         /**
480          * {@inheritDoc}
481          */
482         @Override
483         public void markReplyKnown(Reply reply) {
484                 newReplyNotification.remove(reply);
485         }
486
487         /**
488          * Template provider implementation that uses
489          * {@link WebInterface#createReader(String)} to load templates for
490          * inclusion.
491          *
492          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
493          */
494         private class ClassPathTemplateProvider implements TemplateProvider {
495
496                 /** The template factory. */
497                 @SuppressWarnings("hiding")
498                 private final TemplateFactory templateFactory;
499
500                 /**
501                  * Creates a new template provider that locates templates on the
502                  * classpath.
503                  *
504                  * @param templateFactory
505                  *            The template factory to create the templates
506                  */
507                 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
508                         this.templateFactory = templateFactory;
509                 }
510
511                 /**
512                  * {@inheritDoc}
513                  */
514                 @Override
515                 @SuppressWarnings("synthetic-access")
516                 public Template getTemplate(String templateName) {
517                         Reader templateReader = createReader("/templates/" + templateName);
518                         if (templateReader == null) {
519                                 return null;
520                         }
521                         Template template = templateFactory.createTemplate(templateReader);
522                         try {
523                                 template.parse();
524                         } catch (TemplateException te1) {
525                                 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);
526                         }
527                         return template;
528                 }
529
530         }
531
532 }