Move L10nFilter to own file, remove Sone template factory, use DefaultTemplateFactory.
[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 deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
164                 deleteSoneTemplate.set("formPassword", formPassword);
165
166                 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
167
168                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
169                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
170                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
171                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
172                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
173                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
174                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
175                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
176                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CSSPage("css/", "/static/css/")));
177
178                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
179                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
180                 for (PageToadlet toadlet : pageToadlets) {
181                         String menuName = toadlet.getMenuName();
182                         if (menuName != null) {
183                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
184                         } else {
185                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
186                         }
187                 }
188         }
189
190         /**
191          * Unregisters all toadlets.
192          */
193         private void unregisterToadlets() {
194                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
195                 for (PageToadlet pageToadlet : pageToadlets) {
196                         toadletContainer.unregister(pageToadlet);
197                 }
198                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
199         }
200
201         /**
202          * Creates a {@link Reader} from the {@link InputStream} for the resource
203          * with the given name.
204          *
205          * @param resourceName
206          *            The name of the resource
207          * @return A {@link Reader} for the resource
208          */
209         private Reader createReader(String resourceName) {
210                 try {
211                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
212                 } catch (UnsupportedEncodingException uee1) {
213                         return null;
214                 }
215         }
216
217         /**
218          * {@link LinkEnabledCallback} implementation that always returns
219          * {@code true} when {@link LinkEnabledCallback#isEnabled(ToadletContext)}
220          * is called.
221          *
222          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
223          */
224         public class AlwaysEnabledCallback implements LinkEnabledCallback {
225
226                 /**
227                  * {@inheritDoc}
228                  */
229                 @Override
230                 public boolean isEnabled(ToadletContext toadletContext) {
231                         return true;
232                 }
233         }
234
235 }