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