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