2 * FreenetSone - WebInterface.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web;
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;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
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.GetPostAjaxPage;
59 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
60 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
61 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
62 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
63 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
64 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
65 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
66 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
67 import net.pterodactylus.sone.web.page.PageToadlet;
68 import net.pterodactylus.sone.web.page.PageToadletFactory;
69 import net.pterodactylus.sone.web.page.StaticPage;
70 import net.pterodactylus.util.logging.Logging;
71 import net.pterodactylus.util.notify.NotificationManager;
72 import net.pterodactylus.util.notify.TemplateNotification;
73 import net.pterodactylus.util.template.DateFilter;
74 import net.pterodactylus.util.template.DefaultTemplateFactory;
75 import net.pterodactylus.util.template.MatchFilter;
76 import net.pterodactylus.util.template.PaginationPlugin;
77 import net.pterodactylus.util.template.ReflectionAccessor;
78 import net.pterodactylus.util.template.Template;
79 import net.pterodactylus.util.template.TemplateException;
80 import net.pterodactylus.util.template.TemplateFactory;
81 import net.pterodactylus.util.template.TemplateProvider;
82 import net.pterodactylus.util.template.XmlFilter;
83 import net.pterodactylus.util.thread.Ticker;
84 import freenet.clients.http.SessionManager;
85 import freenet.clients.http.ToadletContainer;
86 import freenet.l10n.BaseL10n;
89 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
90 * references to l10n helpers.
92 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
94 public class WebInterface implements CoreListener {
97 private static final Logger logger = Logging.getLogger(WebInterface.class);
99 /** The notification manager. */
100 private final NotificationManager notificationManager = new NotificationManager();
102 /** The Sone plugin. */
103 private final SonePlugin sonePlugin;
105 /** The registered toadlets. */
106 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
108 /** The form password. */
109 private final String formPassword;
111 /** The template factory. */
112 private DefaultTemplateFactory templateFactory;
114 /** The “new Sone” notification. */
115 private final ListNotification<Sone> newSoneNotification;
117 /** The “new post” notification. */
118 private final ListNotification<Post> newPostNotification;
120 /** The “new reply” notification. */
121 private final ListNotification<Reply> newReplyNotification;
123 /** The “rescuing Sone” notification. */
124 private final ListNotification<Sone> rescuingSonesNotification;
126 /** The “Sone rescued” notification. */
127 private final ListNotification<Sone> sonesRescuedNotification;
130 * Creates a new web interface.
135 public WebInterface(SonePlugin sonePlugin) {
136 this.sonePlugin = sonePlugin;
137 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
139 templateFactory = new DefaultTemplateFactory();
140 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
141 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
142 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
143 templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
144 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
145 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
146 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
147 templateFactory.addFilter("date", new DateFilter());
148 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
149 templateFactory.addFilter("substring", new SubstringFilter());
150 templateFactory.addFilter("xml", new XmlFilter());
151 templateFactory.addFilter("change", new RequestChangeFilter());
152 templateFactory.addFilter("match", new MatchFilter());
153 templateFactory.addFilter("css", new CssClassNameFilter());
154 templateFactory.addPlugin("getpage", new GetPagePlugin());
155 templateFactory.addPlugin("paginate", new PaginationPlugin());
156 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
157 templateFactory.addTemplateObject("formPassword", formPassword);
159 /* create notifications. */
160 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
161 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
163 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
164 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
166 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
167 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
169 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
170 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
172 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
173 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
181 * Returns the Sone core used by the Sone plugin.
183 * @return The Sone core
185 public Core getCore() {
186 return sonePlugin.core();
190 * Returns the notification manager.
192 * @return The notification manager
194 public NotificationManager getNotifications() {
195 return notificationManager;
199 * Returns the l10n helper of the node.
201 * @return The node’s l10n helper
203 public BaseL10n getL10n() {
204 return sonePlugin.l10n().getBase();
208 * Returns the session manager of the node.
210 * @return The node’s session manager
212 public SessionManager getSessionManager() {
213 return sonePlugin.pluginRespirator().getSessionManager("Sone");
217 * Returns the node’s form password.
219 * @return The form password
221 public String getFormPassword() {
226 * Returns the posts that have been announced as new in the
227 * {@link #newPostNotification}.
229 * @return The new posts
231 public Set<Post> getNewPosts() {
232 return new HashSet<Post>(newPostNotification.getElements());
236 * Returns the replies that have been announced as new in the
237 * {@link #newReplyNotification}.
239 * @return The new replies
241 public Set<Reply> getNewReplies() {
242 return new HashSet<Reply>(newReplyNotification.getElements());
250 * Starts the web interface and registers all toadlets.
252 public void start() {
255 /* notification templates. */
256 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
258 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
259 notificationManager.addNotification(startupNotification);
261 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
265 startupNotification.dismiss();
267 }, "Sone Startup Notification Remover");
269 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
270 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
271 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
274 @SuppressWarnings("synthetic-access")
276 if (getCore().getIdentityManager().isConnected()) {
277 wotMissingNotification.dismiss();
279 notificationManager.addNotification(wotMissingNotification);
281 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
284 }, "Sone WoT Connector Checker");
288 * Stops the web interface and unregisters all toadlets.
291 unregisterToadlets();
292 Ticker.getInstance().stop();
300 * Register all toadlets.
302 private void registerToadlets() {
303 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
304 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
305 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
306 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
307 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
308 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
309 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
310 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
311 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
312 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
313 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
314 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
315 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
316 Template lockSoneTemplate = templateFactory.createTemplate(createReader("/templates/lockSone.html"));
317 Template unlockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unlockSone.html"));
318 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
319 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
320 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
321 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
322 Template dismissNotificationTemplate = templateFactory.createTemplate(createReader("/templates/dismissNotification.html"));
323 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
324 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
325 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
326 Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
327 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
329 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
330 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
331 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
332 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
333 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
334 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
335 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
336 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
337 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
338 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(likePostTemplate, this)));
339 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
340 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
341 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
342 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(lockSoneTemplate, this)));
343 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(unlockSoneTemplate, this)));
344 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
345 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
346 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
347 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
348 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
349 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
350 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
351 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
352 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(dismissNotificationTemplate, this)));
353 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
354 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
355 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
356 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
357 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
358 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
359 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
360 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
361 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
362 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
363 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
364 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
365 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
366 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
367 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
368 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
369 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
370 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
371 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
373 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
374 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
375 for (PageToadlet toadlet : pageToadlets) {
376 String menuName = toadlet.getMenuName();
377 if (menuName != null) {
378 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
380 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
386 * Unregisters all toadlets.
388 private void unregisterToadlets() {
389 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
390 for (PageToadlet pageToadlet : pageToadlets) {
391 toadletContainer.unregister(pageToadlet);
393 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
397 * Creates a {@link Reader} from the {@link InputStream} for the resource
398 * with the given name.
400 * @param resourceName
401 * The name of the resource
402 * @return A {@link Reader} for the resource
404 private Reader createReader(String resourceName) {
406 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
407 } catch (UnsupportedEncodingException uee1) {
413 // CORELISTENER METHODS
420 public void rescuingSone(Sone sone) {
421 rescuingSonesNotification.add(sone);
422 notificationManager.addNotification(rescuingSonesNotification);
429 public void rescuedSone(Sone sone) {
430 rescuingSonesNotification.remove(sone);
431 sonesRescuedNotification.add(sone);
432 notificationManager.addNotification(sonesRescuedNotification);
439 public void newSoneFound(Sone sone) {
440 newSoneNotification.add(sone);
441 notificationManager.addNotification(newSoneNotification);
448 public void newPostFound(Post post) {
449 newPostNotification.add(post);
450 notificationManager.addNotification(newPostNotification);
457 public void newReplyFound(Reply reply) {
458 if (reply.getPost().getSone() == null) {
461 newReplyNotification.add(reply);
462 notificationManager.addNotification(newReplyNotification);
469 public void markSoneKnown(Sone sone) {
470 newSoneNotification.remove(sone);
477 public void markPostKnown(Post post) {
478 newPostNotification.remove(post);
485 public void markReplyKnown(Reply reply) {
486 newReplyNotification.remove(reply);
490 * Template provider implementation that uses
491 * {@link WebInterface#createReader(String)} to load templates for
494 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
496 private class ClassPathTemplateProvider implements TemplateProvider {
498 /** The template factory. */
499 @SuppressWarnings("hiding")
500 private final TemplateFactory templateFactory;
503 * Creates a new template provider that locates templates on the
506 * @param templateFactory
507 * The template factory to create the templates
509 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
510 this.templateFactory = templateFactory;
517 @SuppressWarnings("synthetic-access")
518 public Template getTemplate(String templateName) {
519 Reader templateReader = createReader("/templates/" + templateName);
520 if (templateReader == null) {
523 Template template = templateFactory.createTemplate(templateReader);
526 } catch (TemplateException te1) {
527 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);