e6285330498f1cf8fb61c3986b728afd2f900199
[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.PostAccessor;
37 import net.pterodactylus.sone.template.SoneAccessor;
38 import net.pterodactylus.sone.web.page.PageToadlet;
39 import net.pterodactylus.sone.web.page.PageToadletFactory;
40 import net.pterodactylus.sone.web.page.StaticPage;
41 import net.pterodactylus.util.logging.Logging;
42 import net.pterodactylus.util.service.AbstractService;
43 import net.pterodactylus.util.template.DateFilter;
44 import net.pterodactylus.util.template.DefaultTemplateFactory;
45 import net.pterodactylus.util.template.ReflectionAccessor;
46 import net.pterodactylus.util.template.Template;
47 import freenet.clients.http.LinkEnabledCallback;
48 import freenet.clients.http.SessionManager;
49 import freenet.clients.http.ToadletContainer;
50 import freenet.clients.http.ToadletContext;
51 import freenet.l10n.BaseL10n;
52
53 /**
54  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
55  * references to l10n helpers.
56  *
57  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
58  */
59 public class WebInterface extends AbstractService {
60
61         /** The logger. */
62         private static final Logger logger = Logging.getLogger(WebInterface.class);
63
64         /** The Sone plugin. */
65         private final SonePlugin sonePlugin;
66
67         /** The registered toadlets. */
68         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
69
70         /**
71          * Creates a new web interface.
72          *
73          * @param sonePlugin
74          *            The Sone plugin
75          */
76         public WebInterface(SonePlugin sonePlugin) {
77                 super("Sone Web Interface");
78                 this.sonePlugin = sonePlugin;
79         }
80
81         //
82         // ACCESSORS
83         //
84
85         /**
86          * Returns the Sone core used by the Sone plugin.
87          *
88          * @return The Sone core
89          */
90         public Core core() {
91                 return sonePlugin.core();
92         }
93
94         /**
95          * Returns the l10n helper of the node.
96          *
97          * @return The node’s l10n helper
98          */
99         public BaseL10n l10n() {
100                 return sonePlugin.l10n().getBase();
101         }
102
103         /**
104          * Returns the session manager of the node.
105          *
106          * @return The node’s session manager
107          */
108         public SessionManager sessionManager() {
109                 try {
110                         return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
111                 } catch (URISyntaxException use1) {
112                         logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
113                         return null;
114                 }
115         }
116
117         //
118         // SERVICE METHODS
119         //
120
121         /**
122          * {@inheritDoc}
123          */
124         @Override
125         protected void serviceStart() {
126                 registerToadlets();
127         }
128
129         /**
130          * {@inheritDoc}
131          */
132         @Override
133         protected void serviceStop() {
134                 unregisterToadlets();
135         }
136
137         //
138         // PRIVATE METHODS
139         //
140
141         /**
142          * Register all toadlets.
143          */
144         private void registerToadlets() {
145                 DefaultTemplateFactory templateFactory = new DefaultTemplateFactory();
146                 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
147                 templateFactory.addAccessor(Sone.class, new SoneAccessor());
148                 templateFactory.addAccessor(Post.class, new PostAccessor(core()));
149                 templateFactory.addFilter("date", new DateFilter());
150                 templateFactory.addFilter("l10n", new L10nFilter(l10n()));
151
152                 String formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
153
154                 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
155                 loginTemplate.set("formPassword", formPassword);
156
157                 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
158                 indexTemplate.set("formPassword", formPassword);
159
160                 Template addSoneTemplate = templateFactory.createTemplate(createReader("/templates/addSone.html"));
161                 addSoneTemplate.set("formPassword", formPassword);
162
163                 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
164                 createSoneTemplate.set("formPassword", formPassword);
165
166                 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
167                 createPostTemplate.set("formPassword", formPassword);
168
169                 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
170                 createReplyTemplate.set("formPassword", formPassword);
171
172                 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
173                 editProfileTemplate.set("formPassword", formPassword);
174
175                 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
176                 viewSoneTemplate.set("formPassword", formPassword);
177
178                 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
179                 viewPostTemplate.set("formPassword", formPassword);
180
181                 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
182                 followSoneTemplate.set("formPassword", formPassword);
183
184                 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
185                 deleteSoneTemplate.set("formPassword", formPassword);
186
187                 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
188
189                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
190                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
191                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
192                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AddSonePage(addSoneTemplate, this), "AddSone"));
193                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
194                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
195                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
196                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
197                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
198                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
199                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
200                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
201                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
202                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
203                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
204
205                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
206                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
207                 for (PageToadlet toadlet : pageToadlets) {
208                         String menuName = toadlet.getMenuName();
209                         if (menuName != null) {
210                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
211                         } else {
212                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
213                         }
214                 }
215         }
216
217         /**
218          * Unregisters all toadlets.
219          */
220         private void unregisterToadlets() {
221                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
222                 for (PageToadlet pageToadlet : pageToadlets) {
223                         toadletContainer.unregister(pageToadlet);
224                 }
225                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
226         }
227
228         /**
229          * Creates a {@link Reader} from the {@link InputStream} for the resource
230          * with the given name.
231          *
232          * @param resourceName
233          *            The name of the resource
234          * @return A {@link Reader} for the resource
235          */
236         private Reader createReader(String resourceName) {
237                 try {
238                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
239                 } catch (UnsupportedEncodingException uee1) {
240                         return null;
241                 }
242         }
243
244         /**
245          * {@link LinkEnabledCallback} implementation that always returns
246          * {@code true} when {@link LinkEnabledCallback#isEnabled(ToadletContext)}
247          * is called.
248          *
249          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
250          */
251         public class AlwaysEnabledCallback implements LinkEnabledCallback {
252
253                 /**
254                  * {@inheritDoc}
255                  */
256                 @Override
257                 public boolean isEnabled(ToadletContext toadletContext) {
258                         return true;
259                 }
260         }
261
262 }