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.template.CollectionAccessor;
41 import net.pterodactylus.sone.template.CssClassNameFilter;
42 import net.pterodactylus.sone.template.GetPagePlugin;
43 import net.pterodactylus.sone.template.IdentityAccessor;
44 import net.pterodactylus.sone.template.NotificationManagerAccessor;
45 import net.pterodactylus.sone.template.PostAccessor;
46 import net.pterodactylus.sone.template.ReplyAccessor;
47 import net.pterodactylus.sone.template.RequestChangeFilter;
48 import net.pterodactylus.sone.template.SoneAccessor;
49 import net.pterodactylus.sone.template.SubstringFilter;
50 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
51 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
52 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
53 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
54 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
55 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
56 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
57 import net.pterodactylus.sone.web.ajax.GetSoneStatusPage;
58 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
59 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
60 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
61 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
62 import net.pterodactylus.sone.web.page.PageToadlet;
63 import net.pterodactylus.sone.web.page.PageToadletFactory;
64 import net.pterodactylus.sone.web.page.StaticPage;
65 import net.pterodactylus.util.logging.Logging;
66 import net.pterodactylus.util.notify.NotificationManager;
67 import net.pterodactylus.util.notify.TemplateNotification;
68 import net.pterodactylus.util.template.DateFilter;
69 import net.pterodactylus.util.template.DefaultTemplateFactory;
70 import net.pterodactylus.util.template.MatchFilter;
71 import net.pterodactylus.util.template.PaginationPlugin;
72 import net.pterodactylus.util.template.ReflectionAccessor;
73 import net.pterodactylus.util.template.Template;
74 import net.pterodactylus.util.template.TemplateException;
75 import net.pterodactylus.util.template.TemplateFactory;
76 import net.pterodactylus.util.template.TemplateProvider;
77 import net.pterodactylus.util.template.XmlFilter;
78 import net.pterodactylus.util.thread.Ticker;
79 import freenet.clients.http.SessionManager;
80 import freenet.clients.http.ToadletContainer;
81 import freenet.l10n.BaseL10n;
84 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
85 * references to l10n helpers.
87 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
89 public class WebInterface implements CoreListener {
92 private static final Logger logger = Logging.getLogger(WebInterface.class);
94 /** The notification manager. */
95 private final NotificationManager notificationManager = new NotificationManager();
97 /** The Sone plugin. */
98 private final SonePlugin sonePlugin;
100 /** The registered toadlets. */
101 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
103 /** The form password. */
104 private final String formPassword;
106 /** The template factory. */
107 private DefaultTemplateFactory templateFactory;
110 * Creates a new web interface.
115 public WebInterface(SonePlugin sonePlugin) {
116 this.sonePlugin = sonePlugin;
117 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
119 templateFactory = new DefaultTemplateFactory();
120 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
121 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
122 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
123 templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
124 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
125 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
126 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
127 templateFactory.addFilter("date", new DateFilter());
128 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
129 templateFactory.addFilter("substring", new SubstringFilter());
130 templateFactory.addFilter("xml", new XmlFilter());
131 templateFactory.addFilter("change", new RequestChangeFilter());
132 templateFactory.addFilter("match", new MatchFilter());
133 templateFactory.addFilter("css", new CssClassNameFilter());
134 templateFactory.addPlugin("getpage", new GetPagePlugin());
135 templateFactory.addPlugin("paginate", new PaginationPlugin());
136 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
137 templateFactory.addTemplateObject("formPassword", formPassword);
145 * Returns the Sone core used by the Sone plugin.
147 * @return The Sone core
149 public Core getCore() {
150 return sonePlugin.core();
154 * Returns the notification manager.
156 * @return The notification manager
158 public NotificationManager getNotifications() {
159 return notificationManager;
163 * Returns the l10n helper of the node.
165 * @return The node’s l10n helper
167 public BaseL10n getL10n() {
168 return sonePlugin.l10n().getBase();
172 * Returns the session manager of the node.
174 * @return The node’s session manager
176 public SessionManager getSessionManager() {
178 return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
179 } catch (URISyntaxException use1) {
180 logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
186 * Returns the node’s form password.
188 * @return The form password
190 public String getFormPassword() {
199 * Starts the web interface and registers all toadlets.
201 public void start() {
204 /* notification templates. */
205 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
207 final TemplateNotification startupNotification = new TemplateNotification(startupNotificationTemplate);
208 notificationManager.addNotification(startupNotification);
210 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
214 startupNotification.dismiss();
216 }, "Sone Startup Notification Remover");
220 * Stops the web interface and unregisters all toadlets.
223 unregisterToadlets();
231 * Register all toadlets.
233 private void registerToadlets() {
234 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
235 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
236 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
237 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
238 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
239 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
240 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
241 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
242 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
243 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
244 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
245 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
246 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
247 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
248 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
249 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
250 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
251 Template wotPluginMissingTemplate = templateFactory.createTemplate(createReader("/templates/wotPluginMissing.html"));
252 Template dismissNotificationTemplate = templateFactory.createTemplate(createReader("/templates/dismissNotification.html"));
253 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
254 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
255 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
256 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
258 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
259 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
260 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
261 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
262 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
263 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
264 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
265 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
266 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
267 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(likePostTemplate, this)));
268 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
269 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
270 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
271 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
272 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
273 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
274 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
275 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
276 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
277 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
278 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
279 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("wotPluginMissing.html", wotPluginMissingTemplate, "Page.WotPluginMissing.Title", this)));
280 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(dismissNotificationTemplate, this)));
281 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
282 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
283 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
284 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
285 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
286 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetSoneStatusPage(this)));
287 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
288 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
289 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
290 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
291 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
292 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
293 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
294 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
295 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
297 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
298 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
299 for (PageToadlet toadlet : pageToadlets) {
300 String menuName = toadlet.getMenuName();
301 if (menuName != null) {
302 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
304 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
310 * Unregisters all toadlets.
312 private void unregisterToadlets() {
313 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
314 for (PageToadlet pageToadlet : pageToadlets) {
315 toadletContainer.unregister(pageToadlet);
317 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
321 * Creates a {@link Reader} from the {@link InputStream} for the resource
322 * with the given name.
324 * @param resourceName
325 * The name of the resource
326 * @return A {@link Reader} for the resource
328 private Reader createReader(String resourceName) {
330 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
331 } catch (UnsupportedEncodingException uee1) {
337 // CORELISTENER METHODS
344 public void newSoneFound(Sone sone) {
345 newSoneNotification.addSone(sone);
346 notificationManager.addNotification(newSoneNotification);
353 public void newPostFound(Post post) {
361 public void newReplyFound(Reply reply) {
366 * Template provider implementation that uses
367 * {@link WebInterface#createReader(String)} to load templates for
370 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
372 private class ClassPathTemplateProvider implements TemplateProvider {
374 /** The template factory. */
375 @SuppressWarnings("hiding")
376 private final TemplateFactory templateFactory;
379 * Creates a new template provider that locates templates on the
382 * @param templateFactory
383 * The template factory to create the templates
385 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
386 this.templateFactory = templateFactory;
393 @SuppressWarnings("synthetic-access")
394 public Template getTemplate(String templateName) {
395 Reader templateReader = createReader("/templates/" + templateName);
396 if (templateReader == null) {
399 Template template = templateFactory.createTemplate(templateReader);
402 } catch (TemplateException te1) {
403 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);