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