f969208ee4c6efa653ef828fe3911831a138a59d
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SoneTemplatePage.java
1 /*
2  * Freetalk - FreetalkTemplatePage.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.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.util.Arrays;
23 import java.util.Collection;
24
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.main.SonePlugin;
27 import net.pterodactylus.sone.web.page.Page;
28 import net.pterodactylus.sone.web.page.FreenetTemplatePage;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContext;
31 import freenet.clients.http.SessionManager.Session;
32 import freenet.clients.http.ToadletContext;
33 import freenet.support.api.HTTPRequest;
34
35 /**
36  * Base page for the Freetalk web interface.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class SoneTemplatePage extends FreenetTemplatePage {
41
42         /** The Sone core. */
43         protected final WebInterface webInterface;
44
45         /** The page title l10n key. */
46         private final String pageTitleKey;
47
48         /** Whether to require a login. */
49         private final boolean requireLogin;
50
51         /**
52          * Creates a new template page for Freetalk that does not require the user
53          * to be logged in.
54          *
55          * @param path
56          *            The path of the page
57          * @param template
58          *            The template to render
59          * @param webInterface
60          *            The Sone web interface
61          */
62         public SoneTemplatePage(String path, Template template, WebInterface webInterface) {
63                 this(path, template, null, webInterface, false);
64         }
65
66         /**
67          * Creates a new template page for Freetalk that does not require the user
68          * to be logged in.
69          *
70          * @param path
71          *            The path of the page
72          * @param template
73          *            The template to render
74          * @param pageTitleKey
75          *            The l10n key of the page title
76          * @param webInterface
77          *            The Sone web interface
78          */
79         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
80                 this(path, template, pageTitleKey, webInterface, false);
81         }
82
83         /**
84          * Creates a new template page for Freetalk.
85          *
86          * @param path
87          *            The path of the page
88          * @param template
89          *            The template to render
90          * @param webInterface
91          *            The Sone web interface
92          * @param requireLogin
93          *            Whether this page requires a login
94          */
95         public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
96                 this(path, template, null, webInterface, requireLogin);
97         }
98
99         /**
100          * Creates a new template page for Freetalk.
101          *
102          * @param path
103          *            The path of the page
104          * @param template
105          *            The template to render
106          * @param pageTitleKey
107          *            The l10n key of the page title
108          * @param webInterface
109          *            The Sone web interface
110          * @param requireLogin
111          *            Whether this page requires a login
112          */
113         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
114                 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
115                 this.pageTitleKey = pageTitleKey;
116                 this.webInterface = webInterface;
117                 this.requireLogin = requireLogin;
118                 template.getInitialContext().set("webInterface", webInterface);
119         }
120
121         //
122         // PROTECTED METHODS
123         //
124
125         /**
126          * Returns the current session, creating a new session if there is no
127          * current session.
128          *
129          * @param toadletContenxt
130          *            The toadlet context
131          * @return The current session, or {@code null} if there is no current
132          *         session
133          */
134         protected Session getCurrentSession(ToadletContext toadletContenxt) {
135                 return webInterface.getCurrentSession(toadletContenxt);
136         }
137
138         /**
139          * Returns the current session, creating a new session if there is no
140          * current session and {@code create} is {@code true}.
141          *
142          * @param toadletContenxt
143          *            The toadlet context
144          * @param create
145          *            {@code true} to create a new session if there is no current
146          *            session, {@code false} otherwise
147          * @return The current session, or {@code null} if there is no current
148          *         session
149          */
150         protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
151                 return webInterface.getCurrentSession(toadletContenxt, create);
152         }
153
154         /**
155          * Returns the currently logged in Sone.
156          *
157          * @param toadletContext
158          *            The toadlet context
159          * @return The currently logged in Sone, or {@code null} if no Sone is
160          *         currently logged in
161          */
162         protected Sone getCurrentSone(ToadletContext toadletContext) {
163                 return webInterface.getCurrentSone(toadletContext);
164         }
165
166         /**
167          * Returns the currently logged in Sone.
168          *
169          * @param toadletContext
170          *            The toadlet context
171          * @param create
172          *            {@code true} to create a new session if no session exists,
173          *            {@code false} to not create a new session
174          * @return The currently logged in Sone, or {@code null} if no Sone is
175          *         currently logged in
176          */
177         protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
178                 return webInterface.getCurrentSone(toadletContext, create);
179         }
180
181         /**
182          * Sets the currently logged in Sone.
183          *
184          * @param toadletContext
185          *            The toadlet context
186          * @param sone
187          *            The Sone to set as currently logged in
188          */
189         protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
190                 webInterface.setCurrentSone(toadletContext, sone);
191         }
192
193         //
194         // TEMPLATEPAGE METHODS
195         //
196
197         /**
198          * {@inheritDoc}
199          */
200         @Override
201         protected String getPageTitle(Request request) {
202                 if (pageTitleKey != null) {
203                         return webInterface.getL10n().getString(pageTitleKey);
204                 }
205                 return "";
206         }
207
208         /**
209          * {@inheritDoc}
210          */
211         @Override
212         protected Collection<String> getStyleSheets() {
213                 return Arrays.asList("css/sone.css");
214         }
215
216         /**
217          * {@inheritDoc}
218          */
219         @Override
220         protected String getShortcutIcon() {
221                 return "images/icon.png";
222         }
223
224         /**
225          * Returns whether this page requires the user to log in.
226          *
227          * @return {@code true} if the user is required to be logged in to use this
228          *         page, {@code false} otherwise
229          */
230         protected boolean requiresLogin() {
231                 return requireLogin;
232         }
233
234         /**
235          * {@inheritDoc}
236          */
237         @Override
238         protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
239                 super.processTemplate(request, templateContext);
240                 templateContext.set("currentSone", getCurrentSone(request.getToadletContext(), false));
241                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
242                 templateContext.set("request", request);
243                 templateContext.set("currentVersion", SonePlugin.VERSION);
244                 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
245                 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
246                 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
247                 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
248         }
249
250         /**
251          * {@inheritDoc}
252          */
253         @Override
254         protected String getRedirectTarget(Page.Request request) {
255                 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
256                         HTTPRequest httpRequest = request.getHttpRequest();
257                         String originalUrl = httpRequest.getPath();
258                         if (httpRequest.hasParameters()) {
259                                 StringBuilder requestParameters = new StringBuilder();
260                                 for (String parameterName : httpRequest.getParameterNames()) {
261                                         if (requestParameters.length() > 0) {
262                                                 requestParameters.append("%26");
263                                         }
264                                         String[] parameterValues = httpRequest.getMultipleParam(parameterName);
265                                         for (String parameterValue : parameterValues) {
266                                                 try {
267                                                         requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
268                                                 } catch (UnsupportedEncodingException uee1) {
269                                                         /* A JVM without UTF-8? I don’t think so. */
270                                                 }
271                                         }
272                                 }
273                                 originalUrl += "?" + requestParameters.toString();
274                         }
275                         return "login.html?target=" + originalUrl;
276                 }
277                 return null;
278         }
279
280         /**
281          * {@inheritDoc}
282          */
283         @Override
284         public boolean isEnabled(ToadletContext toadletContext) {
285                 if (requiresLogin()) {
286                         return getCurrentSone(toadletContext, false) != null;
287                 }
288                 return true;
289         }
290
291 }