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.data.Post;
34 import net.pterodactylus.sone.data.Reply;
35 import net.pterodactylus.sone.data.Sone;
36 import net.pterodactylus.sone.freenet.L10nFilter;
37 import net.pterodactylus.sone.freenet.wot.Identity;
38 import net.pterodactylus.sone.main.SonePlugin;
39 import net.pterodactylus.sone.template.CollectionAccessor;
40 import net.pterodactylus.sone.template.GetPagePlugin;
41 import net.pterodactylus.sone.template.IdentityAccessor;
42 import net.pterodactylus.sone.template.PostAccessor;
43 import net.pterodactylus.sone.template.ReplyAccessor;
44 import net.pterodactylus.sone.template.RequestChangeFilter;
45 import net.pterodactylus.sone.template.SoneAccessor;
46 import net.pterodactylus.sone.template.SubstringFilter;
47 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
48 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
49 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
50 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
51 import net.pterodactylus.sone.web.ajax.GetSoneStatusPage;
52 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
53 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
54 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
55 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
56 import net.pterodactylus.sone.web.page.PageToadlet;
57 import net.pterodactylus.sone.web.page.PageToadletFactory;
58 import net.pterodactylus.sone.web.page.StaticPage;
59 import net.pterodactylus.util.logging.Logging;
60 import net.pterodactylus.util.template.DateFilter;
61 import net.pterodactylus.util.template.DefaultTemplateFactory;
62 import net.pterodactylus.util.template.MatchFilter;
63 import net.pterodactylus.util.template.PaginationPlugin;
64 import net.pterodactylus.util.template.ReflectionAccessor;
65 import net.pterodactylus.util.template.Template;
66 import net.pterodactylus.util.template.TemplateException;
67 import net.pterodactylus.util.template.TemplateFactory;
68 import net.pterodactylus.util.template.TemplateProvider;
69 import net.pterodactylus.util.template.XmlFilter;
70 import freenet.clients.http.SessionManager;
71 import freenet.clients.http.ToadletContainer;
72 import freenet.l10n.BaseL10n;
75 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
76 * references to l10n helpers.
78 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
80 public class WebInterface {
83 private static final Logger logger = Logging.getLogger(WebInterface.class);
85 /** The Sone plugin. */
86 private final SonePlugin sonePlugin;
88 /** The registered toadlets. */
89 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
91 /** The form password. */
92 private final String formPassword;
95 * Creates a new web interface.
100 public WebInterface(SonePlugin sonePlugin) {
101 this.sonePlugin = sonePlugin;
102 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
110 * Returns the Sone core used by the Sone plugin.
112 * @return The Sone core
114 public Core getCore() {
115 return sonePlugin.core();
119 * Returns the l10n helper of the node.
121 * @return The node’s l10n helper
123 public BaseL10n getL10n() {
124 return sonePlugin.l10n().getBase();
128 * Returns the session manager of the node.
130 * @return The node’s session manager
132 public SessionManager getSessionManager() {
134 return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
135 } catch (URISyntaxException use1) {
136 logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
142 * Returns the node’s form password.
144 * @return The form password
146 public String getFormPassword() {
155 * Starts the web interface and registers all toadlets.
157 public void start() {
162 * Stops the web interface and unregisters all toadlets.
165 unregisterToadlets();
173 * Register all toadlets.
175 private void registerToadlets() {
176 DefaultTemplateFactory templateFactory = new DefaultTemplateFactory();
177 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
178 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
179 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
180 templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
181 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
182 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
183 templateFactory.addFilter("date", new DateFilter());
184 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
185 templateFactory.addFilter("substring", new SubstringFilter());
186 templateFactory.addFilter("xml", new XmlFilter());
187 templateFactory.addFilter("change", new RequestChangeFilter());
188 templateFactory.addFilter("match", new MatchFilter());
189 templateFactory.addPlugin("getpage", new GetPagePlugin());
190 templateFactory.addPlugin("paginate", new PaginationPlugin());
191 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
192 templateFactory.addTemplateObject("formPassword", formPassword);
194 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
195 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
196 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
197 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
198 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
199 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
200 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
201 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
202 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
203 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
204 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
205 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
206 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
207 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
208 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
209 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
210 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
211 Template wotPluginMissingTemplate = templateFactory.createTemplate(createReader("/templates/wotPluginMissing.html"));
212 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
213 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
214 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
216 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
217 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
218 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
219 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
220 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
221 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
222 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
223 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
224 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
225 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(likePostTemplate, this)));
226 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
227 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
228 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
229 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
230 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
231 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
232 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
233 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
234 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
235 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
236 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
237 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("wotPluginMissing.html", wotPluginMissingTemplate, "Page.WotPluginMissing.Title", this)));
238 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
239 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
240 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
241 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
242 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetSoneStatusPage(this)));
243 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
244 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
245 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
246 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
247 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
248 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
249 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
251 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
252 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
253 for (PageToadlet toadlet : pageToadlets) {
254 String menuName = toadlet.getMenuName();
255 if (menuName != null) {
256 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
258 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
264 * Unregisters all toadlets.
266 private void unregisterToadlets() {
267 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
268 for (PageToadlet pageToadlet : pageToadlets) {
269 toadletContainer.unregister(pageToadlet);
271 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
275 * Creates a {@link Reader} from the {@link InputStream} for the resource
276 * with the given name.
278 * @param resourceName
279 * The name of the resource
280 * @return A {@link Reader} for the resource
282 private Reader createReader(String resourceName) {
284 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
285 } catch (UnsupportedEncodingException uee1) {
291 * Template provider implementation that uses
292 * {@link WebInterface#createReader(String)} to load templates for
295 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
297 private class ClassPathTemplateProvider implements TemplateProvider {
299 /** The template factory. */
300 private final TemplateFactory templateFactory;
303 * Creates a new template provider that locates templates on the
306 * @param templateFactory
307 * The template factory to create the templates
309 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
310 this.templateFactory = templateFactory;
317 @SuppressWarnings("synthetic-access")
318 public Template getTemplate(String templateName) {
319 Reader templateReader = createReader("/templates/" + templateName);
320 if (templateReader == null) {
323 Template template = templateFactory.createTemplate(templateReader);
326 } catch (TemplateException te1) {
327 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);