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.List;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.sone.core.Core;
32 import net.pterodactylus.sone.data.Post;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.freenet.L10nFilter;
35 import net.pterodactylus.sone.main.SonePlugin;
36 import net.pterodactylus.sone.template.GetPagePlugin;
37 import net.pterodactylus.sone.template.PostAccessor;
38 import net.pterodactylus.sone.template.RequestChangeFilter;
39 import net.pterodactylus.sone.template.SoneAccessor;
40 import net.pterodactylus.sone.template.SubstringFilter;
41 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
42 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
43 import net.pterodactylus.sone.web.ajax.GetSoneStatusPage;
44 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
45 import net.pterodactylus.sone.web.page.PageToadlet;
46 import net.pterodactylus.sone.web.page.PageToadletFactory;
47 import net.pterodactylus.sone.web.page.StaticPage;
48 import net.pterodactylus.util.logging.Logging;
49 import net.pterodactylus.util.service.AbstractService;
50 import net.pterodactylus.util.template.DateFilter;
51 import net.pterodactylus.util.template.DefaultTemplateFactory;
52 import net.pterodactylus.util.template.PaginationPlugin;
53 import net.pterodactylus.util.template.ReflectionAccessor;
54 import net.pterodactylus.util.template.Template;
55 import net.pterodactylus.util.template.TemplateException;
56 import net.pterodactylus.util.template.TemplateFactory;
57 import net.pterodactylus.util.template.TemplateProvider;
58 import net.pterodactylus.util.template.XmlFilter;
59 import freenet.clients.http.SessionManager;
60 import freenet.clients.http.ToadletContainer;
61 import freenet.l10n.BaseL10n;
64 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
65 * references to l10n helpers.
67 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
69 public class WebInterface extends AbstractService {
72 private static final Logger logger = Logging.getLogger(WebInterface.class);
74 /** The Sone plugin. */
75 private final SonePlugin sonePlugin;
77 /** The registered toadlets. */
78 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
80 /** The form password. */
81 private final String formPassword;
84 * Creates a new web interface.
89 public WebInterface(SonePlugin sonePlugin) {
90 super("Sone Web Interface", false);
91 this.sonePlugin = sonePlugin;
92 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
100 * Returns the Sone core used by the Sone plugin.
102 * @return The Sone core
105 return sonePlugin.core();
109 * Returns the l10n helper of the node.
111 * @return The node’s l10n helper
113 public BaseL10n l10n() {
114 return sonePlugin.l10n().getBase();
118 * Returns the session manager of the node.
120 * @return The node’s session manager
122 public SessionManager sessionManager() {
124 return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
125 } catch (URISyntaxException use1) {
126 logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
132 * Returns the node’s form password.
134 * @return The form password
136 public String formPassword() {
148 protected void serviceStart() {
156 protected void serviceStop() {
157 unregisterToadlets();
165 * Register all toadlets.
167 private void registerToadlets() {
168 DefaultTemplateFactory templateFactory = new DefaultTemplateFactory();
169 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
170 templateFactory.addAccessor(Sone.class, new SoneAccessor(core()));
171 templateFactory.addAccessor(Post.class, new PostAccessor(core()));
172 templateFactory.addFilter("date", new DateFilter());
173 templateFactory.addFilter("l10n", new L10nFilter(l10n()));
174 templateFactory.addFilter("substring", new SubstringFilter());
175 templateFactory.addFilter("xml", new XmlFilter());
176 templateFactory.addFilter("change", new RequestChangeFilter());
177 templateFactory.addPlugin("getpage", new GetPagePlugin());
178 templateFactory.addPlugin("paginate", new PaginationPlugin());
179 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
180 templateFactory.addTemplateObject("formPassword", formPassword);
182 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
183 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
184 Template addSoneTemplate = templateFactory.createTemplate(createReader("/templates/addSone.html"));
185 Template loadSoneTemplate = templateFactory.createTemplate(createReader("/templates/loadSone.html"));
186 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
187 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
188 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
189 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
190 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
191 Template backupProfileTemplate = templateFactory.createTemplate(createReader("/templates/backup.xml"));
192 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
193 Template blockSoneTemplate = templateFactory.createTemplate(createReader("/templates/blockSone.html"));
194 Template unblockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unblockSone.html"));
195 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
196 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
197 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
198 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
199 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
200 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
201 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
202 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
204 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
205 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
206 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
207 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoadSonePage(loadSoneTemplate, this)));
208 pageToadlets.add(pageToadletFactory.createPageToadlet(new AddSonePage(addSoneTemplate, this)));
209 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
210 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
211 pageToadlets.add(pageToadletFactory.createPageToadlet(new BackupProfilePage(backupProfileTemplate, this)));
212 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
213 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
214 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
215 pageToadlets.add(pageToadletFactory.createPageToadlet(new BlockSonePage(blockSoneTemplate, this)));
216 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnblockSonePage(unblockSoneTemplate, this)));
217 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
218 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
219 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
220 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
221 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
222 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
223 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
224 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
225 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
226 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
227 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
228 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
229 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
230 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetSoneStatusPage(this)));
231 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
232 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
234 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
235 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
236 for (PageToadlet toadlet : pageToadlets) {
237 String menuName = toadlet.getMenuName();
238 if (menuName != null) {
239 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
241 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
247 * Unregisters all toadlets.
249 private void unregisterToadlets() {
250 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
251 for (PageToadlet pageToadlet : pageToadlets) {
252 toadletContainer.unregister(pageToadlet);
254 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
258 * Creates a {@link Reader} from the {@link InputStream} for the resource
259 * with the given name.
261 * @param resourceName
262 * The name of the resource
263 * @return A {@link Reader} for the resource
265 private Reader createReader(String resourceName) {
267 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
268 } catch (UnsupportedEncodingException uee1) {
274 * Template provider implementation that uses
275 * {@link WebInterface#createReader(String)} to load templates for
278 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
280 private class ClassPathTemplateProvider implements TemplateProvider {
282 /** The template factory. */
283 private final TemplateFactory templateFactory;
286 * Creates a new template provider that locates templates on the
289 * @param templateFactory
290 * The template factory to create the templates
292 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
293 this.templateFactory = templateFactory;
300 @SuppressWarnings("synthetic-access")
301 public Template getTemplate(String templateName) {
302 Reader templateReader = createReader("/templates/" + templateName);
303 if (templateReader == null) {
306 Template template = templateFactory.createTemplate(templateReader);
309 } catch (TemplateException te1) {
310 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);