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