Store the form password.
[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.data.Post;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.freenet.L10nFilter;
35 import net.pterodactylus.sone.main.SonePlugin;
36 import net.pterodactylus.sone.template.GetPagePlugin;
37 import net.pterodactylus.sone.template.PostAccessor;
38 import net.pterodactylus.sone.template.RequestChangeFilter;
39 import net.pterodactylus.sone.template.SoneAccessor;
40 import net.pterodactylus.sone.template.SubstringFilter;
41 import net.pterodactylus.sone.web.ajax.GetSoneStatusPage;
42 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
43 import net.pterodactylus.sone.web.page.PageToadlet;
44 import net.pterodactylus.sone.web.page.PageToadletFactory;
45 import net.pterodactylus.sone.web.page.StaticPage;
46 import net.pterodactylus.util.logging.Logging;
47 import net.pterodactylus.util.service.AbstractService;
48 import net.pterodactylus.util.template.DateFilter;
49 import net.pterodactylus.util.template.DefaultTemplateFactory;
50 import net.pterodactylus.util.template.PaginationPlugin;
51 import net.pterodactylus.util.template.ReflectionAccessor;
52 import net.pterodactylus.util.template.Template;
53 import net.pterodactylus.util.template.TemplateException;
54 import net.pterodactylus.util.template.TemplateFactory;
55 import net.pterodactylus.util.template.TemplateProvider;
56 import net.pterodactylus.util.template.XmlFilter;
57 import freenet.clients.http.SessionManager;
58 import freenet.clients.http.ToadletContainer;
59 import freenet.l10n.BaseL10n;
60
61 /**
62  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
63  * references to l10n helpers.
64  *
65  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
66  */
67 public class WebInterface extends AbstractService {
68
69         /** The logger. */
70         private static final Logger logger = Logging.getLogger(WebInterface.class);
71
72         /** The Sone plugin. */
73         private final SonePlugin sonePlugin;
74
75         /** The registered toadlets. */
76         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
77
78         /** The form password. */
79         private final String formPassword;
80
81         /**
82          * Creates a new web interface.
83          *
84          * @param sonePlugin
85          *            The Sone plugin
86          */
87         public WebInterface(SonePlugin sonePlugin) {
88                 super("Sone Web Interface", false);
89                 this.sonePlugin = sonePlugin;
90                 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
91         }
92
93         //
94         // ACCESSORS
95         //
96
97         /**
98          * Returns the Sone core used by the Sone plugin.
99          *
100          * @return The Sone core
101          */
102         public Core core() {
103                 return sonePlugin.core();
104         }
105
106         /**
107          * Returns the l10n helper of the node.
108          *
109          * @return The node’s l10n helper
110          */
111         public BaseL10n l10n() {
112                 return sonePlugin.l10n().getBase();
113         }
114
115         /**
116          * Returns the session manager of the node.
117          *
118          * @return The node’s session manager
119          */
120         public SessionManager sessionManager() {
121                 try {
122                         return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
123                 } catch (URISyntaxException use1) {
124                         logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
125                         return null;
126                 }
127         }
128
129         /**
130          * Returns the node’s form password.
131          *
132          * @return The form password
133          */
134         public String formPassword() {
135                 return formPassword;
136         }
137
138         //
139         // SERVICE METHODS
140         //
141
142         /**
143          * {@inheritDoc}
144          */
145         @Override
146         protected void serviceStart() {
147                 registerToadlets();
148         }
149
150         /**
151          * {@inheritDoc}
152          */
153         @Override
154         protected void serviceStop() {
155                 unregisterToadlets();
156         }
157
158         //
159         // PRIVATE METHODS
160         //
161
162         /**
163          * Register all toadlets.
164          */
165         private void registerToadlets() {
166                 DefaultTemplateFactory templateFactory = new DefaultTemplateFactory();
167                 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
168                 templateFactory.addAccessor(Sone.class, new SoneAccessor(core()));
169                 templateFactory.addAccessor(Post.class, new PostAccessor(core()));
170                 templateFactory.addFilter("date", new DateFilter());
171                 templateFactory.addFilter("l10n", new L10nFilter(l10n()));
172                 templateFactory.addFilter("substring", new SubstringFilter());
173                 templateFactory.addFilter("xml", new XmlFilter());
174                 templateFactory.addFilter("change", new RequestChangeFilter());
175                 templateFactory.addPlugin("getpage", new GetPagePlugin());
176                 templateFactory.addPlugin("paginate", new PaginationPlugin());
177                 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
178                 templateFactory.addTemplateObject("formPassword", formPassword);
179
180                 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
181                 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
182                 Template addSoneTemplate = templateFactory.createTemplate(createReader("/templates/addSone.html"));
183                 Template loadSoneTemplate = templateFactory.createTemplate(createReader("/templates/loadSone.html"));
184                 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
185                 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
186                 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
187                 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
188                 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
189                 Template backupProfileTemplate = templateFactory.createTemplate(createReader("/templates/backup.xml"));
190                 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
191                 Template blockSoneTemplate = templateFactory.createTemplate(createReader("/templates/blockSone.html"));
192                 Template unblockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unblockSone.html"));
193                 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
194                 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
195                 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
196                 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
197                 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
198                 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
199                 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
200                 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
201
202                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
203                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
204                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
205                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoadSonePage(loadSoneTemplate, this)));
206                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AddSonePage(addSoneTemplate, this)));
207                 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
208                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
209                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BackupProfilePage(backupProfileTemplate, this)));
210                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
211                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
212                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
213                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BlockSonePage(blockSoneTemplate, this)));
214                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnblockSonePage(unblockSoneTemplate, this)));
215                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
216                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
217                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
218                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
219                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
220                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
221                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
222                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
223                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
224                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
225                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
226                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
227                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
228                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetSoneStatusPage(core())));
229
230                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
231                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
232                 for (PageToadlet toadlet : pageToadlets) {
233                         String menuName = toadlet.getMenuName();
234                         if (menuName != null) {
235                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
236                         } else {
237                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
238                         }
239                 }
240         }
241
242         /**
243          * Unregisters all toadlets.
244          */
245         private void unregisterToadlets() {
246                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
247                 for (PageToadlet pageToadlet : pageToadlets) {
248                         toadletContainer.unregister(pageToadlet);
249                 }
250                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
251         }
252
253         /**
254          * Creates a {@link Reader} from the {@link InputStream} for the resource
255          * with the given name.
256          *
257          * @param resourceName
258          *            The name of the resource
259          * @return A {@link Reader} for the resource
260          */
261         private Reader createReader(String resourceName) {
262                 try {
263                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
264                 } catch (UnsupportedEncodingException uee1) {
265                         return null;
266                 }
267         }
268
269         /**
270          * Template provider implementation that uses
271          * {@link WebInterface#createReader(String)} to load templates for
272          * inclusion.
273          *
274          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
275          */
276         private class ClassPathTemplateProvider implements TemplateProvider {
277
278                 /** The template factory. */
279                 private final TemplateFactory templateFactory;
280
281                 /**
282                  * Creates a new template provider that locates templates on the
283                  * classpath.
284                  *
285                  * @param templateFactory
286                  *            The template factory to create the templates
287                  */
288                 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
289                         this.templateFactory = templateFactory;
290                 }
291
292                 /**
293                  * {@inheritDoc}
294                  */
295                 @Override
296                 @SuppressWarnings("synthetic-access")
297                 public Template getTemplate(String templateName) {
298                         Reader templateReader = createReader("/templates/" + templateName);
299                         if (templateReader == null) {
300                                 return null;
301                         }
302                         Template template = templateFactory.createTemplate(templateReader);
303                         try {
304                                 template.parse();
305                         } catch (TemplateException te1) {
306                                 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);
307                         }
308                         return template;
309                 }
310
311         }
312 }