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;
25 import java.net.URISyntaxException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 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.CreateReplyAjaxPage;
52 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
53 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
54 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
55 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
56 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
57 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
58 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
59 import net.pterodactylus.sone.web.ajax.GetSoneStatusPage;
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;
88 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
89 * references to l10n helpers.
91 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
93 public class WebInterface implements CoreListener {
96 private static final Logger logger = Logging.getLogger(WebInterface.class);
98 /** The notification manager. */
99 private final NotificationManager notificationManager = new NotificationManager();
101 /** The Sone plugin. */
102 private final SonePlugin sonePlugin;
104 /** The registered toadlets. */
105 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
107 /** The form password. */
108 private final String formPassword;
110 /** The template factory. */
111 private DefaultTemplateFactory templateFactory;
113 /** The “new Sone” notification. */
114 private final ListNotification<Sone> newSoneNotification;
116 /** The “new post” notification. */
117 private final ListNotification<Post> newPostNotification;
119 /** The “new reply” notification. */
120 private final ListNotification<Reply> newReplyNotification;
123 * Creates a new web interface.
128 public WebInterface(SonePlugin sonePlugin) {
129 this.sonePlugin = sonePlugin;
130 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
132 templateFactory = new DefaultTemplateFactory();
133 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
134 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
135 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
136 templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
137 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
138 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
139 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
140 templateFactory.addFilter("date", new DateFilter());
141 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
142 templateFactory.addFilter("substring", new SubstringFilter());
143 templateFactory.addFilter("xml", new XmlFilter());
144 templateFactory.addFilter("change", new RequestChangeFilter());
145 templateFactory.addFilter("match", new MatchFilter());
146 templateFactory.addFilter("css", new CssClassNameFilter());
147 templateFactory.addPlugin("getpage", new GetPagePlugin());
148 templateFactory.addPlugin("paginate", new PaginationPlugin());
149 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
150 templateFactory.addTemplateObject("formPassword", formPassword);
152 /* create notifications. */
153 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
154 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
156 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
157 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
159 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
160 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
168 * Returns the Sone core used by the Sone plugin.
170 * @return The Sone core
172 public Core getCore() {
173 return sonePlugin.core();
177 * Returns the notification manager.
179 * @return The notification manager
181 public NotificationManager getNotifications() {
182 return notificationManager;
186 * Returns the l10n helper of the node.
188 * @return The node’s l10n helper
190 public BaseL10n getL10n() {
191 return sonePlugin.l10n().getBase();
195 * Returns the session manager of the node.
197 * @return The node’s session manager
199 public SessionManager getSessionManager() {
201 return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
202 } catch (URISyntaxException use1) {
203 logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
209 * Returns the node’s form password.
211 * @return The form password
213 public String getFormPassword() {
222 * Starts the web interface and registers all toadlets.
224 public void start() {
227 /* notification templates. */
228 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
230 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
231 notificationManager.addNotification(startupNotification);
233 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
237 startupNotification.dismiss();
239 }, "Sone Startup Notification Remover");
241 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
242 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
243 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
246 @SuppressWarnings("synthetic-access")
248 if (getCore().getIdentityManager().isConnected()) {
249 wotMissingNotification.dismiss();
251 notificationManager.addNotification(wotMissingNotification);
253 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
256 }, "Sone WoT Connector Checker");
260 * Stops the web interface and unregisters all toadlets.
263 unregisterToadlets();
264 Ticker.getInstance().stop();
272 * Register all toadlets.
274 private void registerToadlets() {
275 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
276 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
277 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
278 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
279 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
280 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
281 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
282 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
283 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
284 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
285 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
286 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
287 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
288 Template lockSoneTemplate = templateFactory.createTemplate(createReader("/templates/lockSone.html"));
289 Template unlockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unlockSone.html"));
290 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
291 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
292 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
293 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
294 Template dismissNotificationTemplate = templateFactory.createTemplate(createReader("/templates/dismissNotification.html"));
295 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
296 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
297 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
298 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
300 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
301 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
302 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
303 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
304 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
305 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
306 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
307 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
308 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
309 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(likePostTemplate, this)));
310 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
311 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
312 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
313 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(lockSoneTemplate, this)));
314 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(unlockSoneTemplate, this)));
315 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
316 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
317 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
318 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
319 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
320 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
321 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
322 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
323 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(dismissNotificationTemplate, this)));
324 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
325 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
326 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
327 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
328 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
329 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
330 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetSoneStatusPage(this)));
331 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
332 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
333 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
334 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
335 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
336 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
337 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
338 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
339 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
340 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
341 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
343 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
344 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
345 for (PageToadlet toadlet : pageToadlets) {
346 String menuName = toadlet.getMenuName();
347 if (menuName != null) {
348 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
350 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
356 * Unregisters all toadlets.
358 private void unregisterToadlets() {
359 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
360 for (PageToadlet pageToadlet : pageToadlets) {
361 toadletContainer.unregister(pageToadlet);
363 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
367 * Creates a {@link Reader} from the {@link InputStream} for the resource
368 * with the given name.
370 * @param resourceName
371 * The name of the resource
372 * @return A {@link Reader} for the resource
374 private Reader createReader(String resourceName) {
376 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
377 } catch (UnsupportedEncodingException uee1) {
383 // CORELISTENER METHODS
390 public void newSoneFound(Sone sone) {
391 newSoneNotification.add(sone);
392 notificationManager.addNotification(newSoneNotification);
399 public void newPostFound(Post post) {
400 newPostNotification.add(post);
401 notificationManager.addNotification(newPostNotification);
408 public void newReplyFound(Reply reply) {
409 if (reply.getPost().getSone() == null) {
412 newReplyNotification.add(reply);
413 notificationManager.addNotification(newReplyNotification);
420 public void markSoneKnown(Sone sone) {
421 newSoneNotification.remove(sone);
422 if (newSoneNotification.isEmpty()) {
423 newSoneNotification.dismiss();
431 public void markPostKnown(Post post) {
432 newPostNotification.remove(post);
433 if (newPostNotification.isEmpty()) {
434 newPostNotification.dismiss();
442 public void markReplyKnown(Reply reply) {
443 newReplyNotification.remove(reply);
444 if (newReplyNotification.isEmpty()) {
445 newReplyNotification.dismiss();
450 * Template provider implementation that uses
451 * {@link WebInterface#createReader(String)} to load templates for
454 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
456 private class ClassPathTemplateProvider implements TemplateProvider {
458 /** The template factory. */
459 @SuppressWarnings("hiding")
460 private final TemplateFactory templateFactory;
463 * Creates a new template provider that locates templates on the
466 * @param templateFactory
467 * The template factory to create the templates
469 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
470 this.templateFactory = templateFactory;
477 @SuppressWarnings("synthetic-access")
478 public Template getTemplate(String templateName) {
479 Reader templateReader = createReader("/templates/" + templateName);
480 if (templateReader == null) {
483 Template template = templateFactory.createTemplate(templateReader);
486 } catch (TemplateException te1) {
487 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);