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