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