2 * Sone - SoneTemplatePage.java - Copyright © 2010–2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.main.SonePlugin;
30 import net.pterodactylus.sone.notify.ListNotificationFilters;
31 import net.pterodactylus.sone.web.page.FreenetRequest;
32 import net.pterodactylus.sone.web.page.FreenetTemplatePage;
33 import net.pterodactylus.util.notify.Notification;
34 import net.pterodactylus.util.template.Template;
35 import net.pterodactylus.util.template.TemplateContext;
37 import com.google.common.base.Optional;
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.collect.ImmutableMap;
41 import freenet.clients.http.ToadletContext;
42 import freenet.support.api.HTTPRequest;
45 * Base page for the Sone web interface.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public class SoneTemplatePage extends FreenetTemplatePage {
52 protected final WebInterface webInterface;
54 /** The page title l10n key. */
55 private final String pageTitleKey;
57 /** Whether to require a login. */
58 private final boolean requireLogin;
61 * Creates a new template page for Sone that does not require the user to be
65 * The path of the page
67 * The template to render
69 * The Sone web interface
71 public SoneTemplatePage(String path, Template template, WebInterface webInterface) {
72 this(path, template, null, webInterface, false);
76 * Creates a new template page for Sone that does not require the user to be
80 * The path of the page
82 * The template to render
84 * The l10n key of the page title
86 * The Sone web interface
88 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
89 this(path, template, pageTitleKey, webInterface, false);
93 * Creates a new template page for Sone.
96 * The path of the page
98 * The template to render
100 * The Sone web interface
101 * @param requireLogin
102 * Whether this page requires a login
104 public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
105 this(path, template, null, webInterface, requireLogin);
109 * Creates a new template page for Sone.
112 * The path of the page
114 * The template to render
115 * @param pageTitleKey
116 * The l10n key of the page title
117 * @param webInterface
118 * The Sone web interface
119 * @param requireLogin
120 * Whether this page requires a login
122 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
123 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
124 this.pageTitleKey = pageTitleKey;
125 this.webInterface = webInterface;
126 this.requireLogin = requireLogin;
134 * Returns the currently logged in Sone.
136 * @param toadletContext
137 * The toadlet context
138 * @return The currently logged in Sone, or {@code null} if no Sone is
139 * currently logged in
141 protected Optional<Sone> getCurrentSone(ToadletContext toadletContext) {
142 return webInterface.getCurrentSone(toadletContext);
146 * Returns the currently logged in Sone.
148 * @param toadletContext
149 * The toadlet context
151 * {@code true} to create a new session if no session exists,
152 * {@code false} to not create a new session
153 * @return The currently logged in Sone, or {@code null} if no Sone is
154 * currently logged in
156 protected Optional<Sone> getCurrentSone(ToadletContext toadletContext, boolean create) {
157 return webInterface.getCurrentSone(toadletContext, create);
161 * Sets the currently logged in Sone.
163 * @param toadletContext
164 * The toadlet context
166 * The Sone to set as currently logged in
168 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
169 webInterface.setCurrentSone(toadletContext, sone);
173 // TEMPLATEPAGE METHODS
180 protected String getPageTitle(FreenetRequest request) {
181 if (pageTitleKey != null) {
182 return webInterface.getL10n().getString(pageTitleKey);
191 protected List<Map<String, String>> getAdditionalLinkNodes(FreenetRequest request) {
192 return ImmutableList.<Map<String, String>> builder().add(ImmutableMap.<String, String> builder().put("rel", "search").put("type", "application/opensearchdescription+xml").put("title", "Sone").put("href", "http://" + request.getHttpRequest().getHeader("host") + "/Sone/OpenSearch.xml").build()).build();
199 protected Collection<String> getStyleSheets() {
200 return Arrays.asList("css/sone.css");
207 protected String getShortcutIcon() {
208 return "images/icon.png";
212 * Returns whether this page requires the user to log in.
214 * @return {@code true} if the user is required to be logged in to use this
215 * page, {@code false} otherwise
217 protected boolean requiresLogin() {
225 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
226 super.processTemplate(request, templateContext);
227 Optional<Sone> currentSone = getCurrentSone(request.getToadletContext(), false);
228 templateContext.set("core", webInterface.getCore());
229 templateContext.set("currentSone", currentSone.orNull());
230 templateContext.set("localSones", webInterface.getCore().getLocalSones());
231 templateContext.set("request", request);
232 templateContext.set("currentVersion", SonePlugin.VERSION);
233 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
234 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
235 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
236 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
237 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone.orNull());
238 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
239 templateContext.set("notifications", notifications);
240 templateContext.set("notificationHash", notifications.hashCode());
247 protected String getRedirectTarget(FreenetRequest request) {
248 if (requiresLogin() && !getCurrentSone(request.getToadletContext(), false).isPresent()) {
249 HTTPRequest httpRequest = request.getHttpRequest();
250 String originalUrl = httpRequest.getPath();
251 if (httpRequest.hasParameters()) {
252 StringBuilder requestParameters = new StringBuilder();
253 for (String parameterName : httpRequest.getParameterNames()) {
254 if (requestParameters.length() > 0) {
255 requestParameters.append("%26");
257 String[] parameterValues = httpRequest.getMultipleParam(parameterName);
258 for (String parameterValue : parameterValues) {
260 requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
261 } catch (UnsupportedEncodingException uee1) {
262 /* A JVM without UTF-8? I don’t think so. */
266 originalUrl += "?" + requestParameters.toString();
268 return "login.html?target=" + originalUrl;
277 protected boolean isFullAccessOnly() {
278 return webInterface.getCore().getPreferences().isRequireFullAccess();
285 public boolean isEnabled(ToadletContext toadletContext) {
286 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
289 if (requiresLogin()) {
290 return getCurrentSone(toadletContext, false).isPresent();