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