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.freenet.L10nFilter;
33 import net.pterodactylus.sone.main.SonePlugin;
34 import net.pterodactylus.sone.web.page.CSSPage;
35 import net.pterodactylus.sone.web.page.PageToadlet;
36 import net.pterodactylus.sone.web.page.PageToadletFactory;
37 import net.pterodactylus.util.logging.Logging;
38 import net.pterodactylus.util.service.AbstractService;
39 import net.pterodactylus.util.template.DateFilter;
40 import net.pterodactylus.util.template.DefaultTemplateFactory;
41 import net.pterodactylus.util.template.ReflectionAccessor;
42 import net.pterodactylus.util.template.Template;
43 import freenet.clients.http.LinkEnabledCallback;
44 import freenet.clients.http.SessionManager;
45 import freenet.clients.http.ToadletContainer;
46 import freenet.clients.http.ToadletContext;
47 import freenet.l10n.BaseL10n;
50 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
51 * references to l10n helpers.
53 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
55 public class WebInterface extends AbstractService {
58 private static final Logger logger = Logging.getLogger(WebInterface.class);
60 /** The Sone plugin. */
61 private final SonePlugin sonePlugin;
63 /** The registered toadlets. */
64 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
67 * Creates a new web interface.
72 public WebInterface(SonePlugin sonePlugin) {
73 super("Sone Web Interface");
74 this.sonePlugin = sonePlugin;
82 * Returns the Sone core used by the Sone plugin.
84 * @return The Sone core
87 return sonePlugin.core();
91 * Returns the l10n helper of the node.
93 * @return The node’s l10n helper
95 public BaseL10n l10n() {
96 return sonePlugin.l10n().getBase();
100 * Returns the session manager of the node.
102 * @return The node’s session manager
104 public SessionManager sessionManager() {
106 return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
107 } catch (URISyntaxException use1) {
108 logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
121 protected void serviceStart() {
129 protected void serviceStop() {
130 unregisterToadlets();
138 * Register all toadlets.
140 private void registerToadlets() {
141 DefaultTemplateFactory templateFactory = new DefaultTemplateFactory();
142 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
143 templateFactory.addFilter("date", new DateFilter());
144 templateFactory.addFilter("l10n", new L10nFilter(l10n()));
146 String formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
148 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
149 loginTemplate.set("formPassword", formPassword);
151 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
152 indexTemplate.set("formPassword", formPassword);
154 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
155 createSoneTemplate.set("formPassword", formPassword);
157 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
158 createPostTemplate.set("formPassword", formPassword);
160 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
161 editProfileTemplate.set("formPassword", formPassword);
163 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
164 deleteSoneTemplate.set("formPassword", formPassword);
166 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
168 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
169 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
170 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
171 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
172 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
173 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
174 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
175 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
176 pageToadlets.add(pageToadletFactory.createPageToadlet(new CSSPage("css/", "/static/css/")));
178 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
179 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
180 for (PageToadlet toadlet : pageToadlets) {
181 String menuName = toadlet.getMenuName();
182 if (menuName != null) {
183 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
185 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
191 * Unregisters all toadlets.
193 private void unregisterToadlets() {
194 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
195 for (PageToadlet pageToadlet : pageToadlets) {
196 toadletContainer.unregister(pageToadlet);
198 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
202 * Creates a {@link Reader} from the {@link InputStream} for the resource
203 * with the given name.
205 * @param resourceName
206 * The name of the resource
207 * @return A {@link Reader} for the resource
209 private Reader createReader(String resourceName) {
211 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
212 } catch (UnsupportedEncodingException uee1) {
218 * {@link LinkEnabledCallback} implementation that always returns
219 * {@code true} when {@link LinkEnabledCallback#isEnabled(ToadletContext)}
222 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
224 public class AlwaysEnabledCallback implements LinkEnabledCallback {
230 public boolean isEnabled(ToadletContext toadletContext) {