2 * WoTNS - WebInterface.java - Copyright © 2011 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.wotns.ui.web;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.io.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import net.pterodactylus.util.template.HtmlFilter;
28 import net.pterodactylus.util.template.ReflectionAccessor;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContextFactory;
31 import net.pterodactylus.util.template.TemplateParser;
32 import net.pterodactylus.wotns.freenet.wot.Identity;
33 import net.pterodactylus.wotns.main.WoTNSPlugin;
34 import net.pterodactylus.wotns.template.IdentityAccessor;
35 import net.pterodactylus.wotns.web.PageToadlet;
36 import net.pterodactylus.wotns.web.PageToadletFactory;
37 import freenet.clients.http.ToadletContainer;
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class WebInterface {
46 private final WoTNSPlugin wotNSPlugin;
48 private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
50 /** The registered toadlets. */
51 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
53 public WebInterface(WoTNSPlugin woTNSPlugin) {
54 this.wotNSPlugin = woTNSPlugin;
56 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
57 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor());
58 templateContextFactory.addFilter("html", new HtmlFilter());
65 public WoTNSPlugin getWoTNSPlugin() {
69 public TemplateContextFactory getTemplateContextFactory() {
70 return templateContextFactory;
89 private void registerToadlets() {
90 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
91 Template unknownTemplate = TemplateParser.parse(createReader("/templates/unknown.html"));
92 Template manageTemplate = TemplateParser.parse(createReader("/templates/manage.html"));
93 Template addTargetTemplate = TemplateParser.parse(createReader("/templates/addTarget.html"));
95 PageToadletFactory pageToadletFactory = new PageToadletFactory(wotNSPlugin.getHighLevelSimpleClient(), "/tns/");
96 pageToadlets.add(pageToadletFactory.createPageToadlet(new ResolverPage(unknownTemplate, this, wotNSPlugin.getResolver())));
97 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
98 pageToadlets.add(pageToadletFactory.createPageToadlet(new ManagePage(manageTemplate, this)));
99 pageToadlets.add(pageToadletFactory.createPageToadlet(new AddTargetPage(addTargetTemplate, this)));
101 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
102 toadletContainer.getPageMaker().addNavigationCategory("/tns/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", wotNSPlugin);
103 for (PageToadlet toadlet : pageToadlets) {
104 String menuName = toadlet.getMenuName();
105 if (menuName != null) {
106 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
108 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
114 * Unregisters all toadlets.
116 private void unregisterToadlets() {
117 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
118 for (PageToadlet pageToadlet : pageToadlets) {
119 toadletContainer.unregister(pageToadlet);
121 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
125 * Creates a {@link Reader} from the {@link InputStream} for the resource
126 * with the given name.
128 * @param resourceName
129 * The name of the resource
130 * @return A {@link Reader} for the resource
132 private Reader createReader(String resourceName) {
134 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
135 } catch (UnsupportedEncodingException uee1) {