Add index page.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
1 /*
2  * FreenetSone - WebInterface.java - Copyright © 2010 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.sone.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.net.URI;
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;
30
31 import net.pterodactylus.sone.core.Core;
32 import net.pterodactylus.sone.main.SonePlugin;
33 import net.pterodactylus.sone.web.page.CSSPage;
34 import net.pterodactylus.sone.web.page.PageToadlet;
35 import net.pterodactylus.sone.web.page.PageToadletFactory;
36 import net.pterodactylus.util.logging.Logging;
37 import net.pterodactylus.util.service.AbstractService;
38 import net.pterodactylus.util.template.Template;
39 import net.pterodactylus.util.template.TemplateFactory;
40 import freenet.clients.http.LinkEnabledCallback;
41 import freenet.clients.http.SessionManager;
42 import freenet.clients.http.ToadletContainer;
43 import freenet.clients.http.ToadletContext;
44 import freenet.l10n.BaseL10n;
45
46 /**
47  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
48  * references to l10n helpers.
49  *
50  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51  */
52 public class WebInterface extends AbstractService {
53
54         /** The logger. */
55         private static final Logger logger = Logging.getLogger(WebInterface.class);
56
57         /** The Sone plugin. */
58         private final SonePlugin sonePlugin;
59
60         /** The registered toadlets. */
61         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
62
63         /**
64          * Creates a new web interface.
65          *
66          * @param sonePlugin
67          *            The Sone plugin
68          */
69         public WebInterface(SonePlugin sonePlugin) {
70                 super("Sone Web Interface");
71                 this.sonePlugin = sonePlugin;
72         }
73
74         //
75         // ACCESSORS
76         //
77
78         /**
79          * Returns the Sone core used by the Sone plugin.
80          *
81          * @return The Sone core
82          */
83         public Core core() {
84                 return sonePlugin.core();
85         }
86
87         /**
88          * Returns the l10n helper of the node.
89          *
90          * @return The node’s l10n helper
91          */
92         public BaseL10n l10n() {
93                 return sonePlugin.l10n().getBase();
94         }
95
96         /**
97          * Returns the session manager of the node.
98          *
99          * @return The node’s session manager
100          */
101         public SessionManager sessionManager() {
102                 try {
103                         return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
104                 } catch (URISyntaxException use1) {
105                         logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
106                         return null;
107                 }
108         }
109
110         //
111         // SERVICE METHODS
112         //
113
114         /**
115          * {@inheritDoc}
116          */
117         @Override
118         protected void serviceStart() {
119                 registerToadlets();
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         @Override
126         protected void serviceStop() {
127                 unregisterToadlets();
128         }
129
130         //
131         // PRIVATE METHODS
132         //
133
134         /**
135          * Register all toadlets.
136          */
137         private void registerToadlets() {
138                 TemplateFactory templateFactory = new SoneTemplateFactory(l10n());
139                 String formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
140
141                 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
142                 loginTemplate.set("formPassword", formPassword);
143
144                 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
145                 indexTemplate.set("formPassword", formPassword);
146
147                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
148                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
149                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
150                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CSSPage("css/", "/static/css/")));
151
152                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
153                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
154                 for (PageToadlet toadlet : pageToadlets) {
155                         String menuName = toadlet.getMenuName();
156                         if (menuName != null) {
157                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
158                         } else {
159                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
160                         }
161                 }
162         }
163
164         /**
165          * Unregisters all toadlets.
166          */
167         private void unregisterToadlets() {
168                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
169                 for (PageToadlet pageToadlet : pageToadlets) {
170                         toadletContainer.unregister(pageToadlet);
171                 }
172                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
173         }
174
175         /**
176          * Creates a {@link Reader} from the {@link InputStream} for the resource
177          * with the given name.
178          *
179          * @param resourceName
180          *            The name of the resource
181          * @return A {@link Reader} for the resource
182          */
183         private Reader createReader(String resourceName) {
184                 try {
185                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
186                 } catch (UnsupportedEncodingException uee1) {
187                         return null;
188                 }
189         }
190
191         /**
192          * {@link LinkEnabledCallback} implementation that always returns
193          * {@code true} when {@link LinkEnabledCallback#isEnabled(ToadletContext)}
194          * is called.
195          *
196          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
197          */
198         public class AlwaysEnabledCallback implements LinkEnabledCallback {
199
200                 /**
201                  * {@inheritDoc}
202                  */
203                 @Override
204                 public boolean isEnabled(ToadletContext toadletContext) {
205                         return true;
206                 }
207         }
208
209 }