18daf46354dbff53f1da6a07ea35275d68fb7d47
[WoTNS.git] / src / main / java / net / pterodactylus / wotns / ui / web / WebInterface.java
1 /*
2  * WoTNS - WebInterface.java - Copyright © 2011 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.wotns.ui.web;
19
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;
26
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.IdentityAccessor;
39 import net.pterodactylus.wotns.web.FreenetRequest;
40 import net.pterodactylus.wotns.web.PageToadlet;
41 import net.pterodactylus.wotns.web.PageToadletFactory;
42 import freenet.clients.http.ToadletContainer;
43
44 /**
45  * TODO
46  *
47  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48  */
49 public class WebInterface {
50
51         private final WoTNSPlugin wotNSPlugin;
52
53         private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
54
55         /** The registered toadlets. */
56         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
57
58         public WebInterface(WoTNSPlugin woTNSPlugin) {
59                 this.wotNSPlugin = woTNSPlugin;
60
61                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
62                 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor());
63                 templateContextFactory.addFilter("html", new HtmlFilter());
64                 CollectionSortFilter sortFilter = new CollectionSortFilter();
65                 sortFilter.addComparator(Identity.class, IdentityComparator.NAME);
66                 templateContextFactory.addFilter("sort", sortFilter);
67                 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class));
68         }
69
70         //
71         // ACCESSORS
72         //
73
74         public WoTNSPlugin getWoTNSPlugin() {
75                 return wotNSPlugin;
76         }
77
78         public TemplateContextFactory getTemplateContextFactory() {
79                 return templateContextFactory;
80         }
81
82         //
83         // ACTIONS
84         //
85
86         public void start() {
87                 registerToadlets();
88         }
89
90         public void stop() {
91                 unregisterToadlets();
92         }
93
94         //
95         // PRIVATE METHODS
96         //
97
98         private void registerToadlets() {
99                 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
100                 Template unknownTemplate = TemplateParser.parse(createReader("/templates/unknown.html"));
101                 Template manageTemplate = TemplateParser.parse(createReader("/templates/manage.html"));
102                 Template addTargetTemplate = TemplateParser.parse(createReader("/templates/addTarget.html"));
103
104                 PageToadletFactory pageToadletFactory = new PageToadletFactory(wotNSPlugin.getHighLevelSimpleClient(), "/tns/");
105                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ResolverPage(unknownTemplate, this, wotNSPlugin.getResolver())));
106                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
107                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ManagePage(manageTemplate, this)));
108                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EnableIdentityPage(new Template(), this)));
109                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AddTargetPage(addTargetTemplate, this)));
110                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditTargetPage(new Template(), this)));
111                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
112
113                 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
114                 toadletContainer.getPageMaker().addNavigationCategory("/tns/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", wotNSPlugin);
115                 for (PageToadlet toadlet : pageToadlets) {
116                         String menuName = toadlet.getMenuName();
117                         if (menuName != null) {
118                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
119                         } else {
120                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
121                         }
122                 }
123         }
124
125         /**
126          * Unregisters all toadlets.
127          */
128         private void unregisterToadlets() {
129                 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
130                 for (PageToadlet pageToadlet : pageToadlets) {
131                         toadletContainer.unregister(pageToadlet);
132                 }
133                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
134         }
135
136         /**
137          * Creates a {@link Reader} from the {@link InputStream} for the resource
138          * with the given name.
139          *
140          * @param resourceName
141          *            The name of the resource
142          * @return A {@link Reader} for the resource
143          */
144         private Reader createReader(String resourceName) {
145                 try {
146                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
147                 } catch (UnsupportedEncodingException uee1) {
148                         return null;
149                 }
150         }
151
152 }