Store the request in all template contexts.
[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.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;
45
46 /**
47  * TODO
48  *
49  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50  */
51 public class WebInterface {
52
53         private final WoTNSPlugin wotNSPlugin;
54
55         private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
56
57         /** The registered toadlets. */
58         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
59
60         public WebInterface(WoTNSPlugin woTNSPlugin) {
61                 this.wotNSPlugin = woTNSPlugin;
62
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));
71         }
72
73         //
74         // ACCESSORS
75         //
76
77         public WoTNSPlugin getWoTNSPlugin() {
78                 return wotNSPlugin;
79         }
80
81         public TemplateContextFactory getTemplateContextFactory() {
82                 return templateContextFactory;
83         }
84
85         //
86         // ACTIONS
87         //
88
89         public void start() {
90                 registerToadlets();
91         }
92
93         public void stop() {
94                 unregisterToadlets();
95         }
96
97         //
98         // PRIVATE METHODS
99         //
100
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"));
106
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")));
115
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);
122                         } else {
123                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
124                         }
125                 }
126         }
127
128         /**
129          * Unregisters all toadlets.
130          */
131         private void unregisterToadlets() {
132                 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
133                 for (PageToadlet pageToadlet : pageToadlets) {
134                         toadletContainer.unregister(pageToadlet);
135                 }
136                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
137         }
138
139         /**
140          * Creates a {@link Reader} from the {@link InputStream} for the resource
141          * with the given name.
142          *
143          * @param resourceName
144          *            The name of the resource
145          * @return A {@link Reader} for the resource
146          */
147         private Reader createReader(String resourceName) {
148                 try {
149                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
150                 } catch (UnsupportedEncodingException uee1) {
151                         return null;
152                 }
153         }
154
155 }