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.ClassPathTemplateProvider;
28 import net.pterodactylus.util.template.CollectionSortFilter;
29 import net.pterodactylus.util.template.HtmlFilter;
30 import net.pterodactylus.util.template.ReflectionAccessor;
31 import net.pterodactylus.util.template.Template;
32 import net.pterodactylus.util.template.TemplateContextFactory;
33 import net.pterodactylus.util.template.TemplateParser;
34 import net.pterodactylus.util.web.StaticPage;
35 import net.pterodactylus.wotns.freenet.wot.Identity;
36 import net.pterodactylus.wotns.main.IdentityComparator;
37 import net.pterodactylus.wotns.main.WoTNSPlugin;
38 import net.pterodactylus.wotns.template.HttpRequestAccessor;
39 import net.pterodactylus.wotns.template.IdentityAccessor;
40 import net.pterodactylus.wotns.web.FreenetRequest;
41 import net.pterodactylus.wotns.web.PageToadlet;
42 import net.pterodactylus.wotns.web.PageToadletFactory;
43 import freenet.clients.http.ToadletContainer;
44 import freenet.support.api.HTTPRequest;
49 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51 public class WebInterface {
53 private final WoTNSPlugin wotNSPlugin;
55 private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
57 /** The registered toadlets. */
58 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
60 public WebInterface(WoTNSPlugin woTNSPlugin) {
61 this.wotNSPlugin = woTNSPlugin;
63 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
64 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor());
65 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
66 templateContextFactory.addFilter("html", new HtmlFilter());
67 CollectionSortFilter sortFilter = new CollectionSortFilter();
68 sortFilter.addComparator(Identity.class, IdentityComparator.NAME);
69 templateContextFactory.addFilter("sort", sortFilter);
70 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class));
77 public WoTNSPlugin getWoTNSPlugin() {
81 public TemplateContextFactory getTemplateContextFactory() {
82 return templateContextFactory;
101 private void registerToadlets() {
102 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
103 Template unknownTemplate = TemplateParser.parse(createReader("/templates/unknown.html"));
104 Template manageTemplate = TemplateParser.parse(createReader("/templates/manage.html"));
105 Template addTargetTemplate = TemplateParser.parse(createReader("/templates/addTarget.html"));
107 PageToadletFactory pageToadletFactory = new PageToadletFactory(wotNSPlugin.getHighLevelSimpleClient(), "/tns/");
108 pageToadlets.add(pageToadletFactory.createPageToadlet(new ResolverPage(unknownTemplate, this, wotNSPlugin.getResolver())));
109 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
110 pageToadlets.add(pageToadletFactory.createPageToadlet(new ManagePage(manageTemplate, this)));
111 pageToadlets.add(pageToadletFactory.createPageToadlet(new EnableIdentityPage(new Template(), this)));
112 pageToadlets.add(pageToadletFactory.createPageToadlet(new AddTargetPage(addTargetTemplate, this)));
113 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditTargetPage(new Template(), this)));
114 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
116 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
117 toadletContainer.getPageMaker().addNavigationCategory("/tns/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", wotNSPlugin);
118 for (PageToadlet toadlet : pageToadlets) {
119 String menuName = toadlet.getMenuName();
120 if (menuName != null) {
121 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
123 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
129 * Unregisters all toadlets.
131 private void unregisterToadlets() {
132 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
133 for (PageToadlet pageToadlet : pageToadlets) {
134 toadletContainer.unregister(pageToadlet);
136 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
140 * Creates a {@link Reader} from the {@link InputStream} for the resource
141 * with the given name.
143 * @param resourceName
144 * The name of the resource
145 * @return A {@link Reader} for the resource
147 private Reader createReader(String resourceName) {
149 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
150 } catch (UnsupportedEncodingException uee1) {