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.collect.ImmutableList;
38 import com.google.common.collect.ImmutableMap;
40 import freenet.clients.http.ToadletContext;
41 import freenet.support.api.HTTPRequest;
44 * Base page for the Sone web interface.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class SoneTemplatePage extends FreenetTemplatePage {
51 protected final WebInterface webInterface;
53 /** The page title l10n key. */
54 private final String pageTitleKey;
56 /** Whether to require a login. */
57 private final boolean requireLogin;
60 * Creates a new template page for Sone that does not require the user to be
64 * The path of the page
66 * The template to render
68 * The Sone web interface
70 public SoneTemplatePage(String path, Template template, WebInterface webInterface) {
71 this(path, template, null, webInterface, false);
75 * Creates a new template page for Sone that does not require the user to be
79 * The path of the page
81 * The template to render
83 * The l10n key of the page title
85 * The Sone web interface
87 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
88 this(path, template, pageTitleKey, webInterface, false);
92 * Creates a new template page for Sone.
95 * The path of the page
97 * The template to render
99 * The Sone web interface
100 * @param requireLogin
101 * Whether this page requires a login
103 public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
104 this(path, template, null, webInterface, requireLogin);
108 * Creates a new template page for Sone.
111 * The path of the page
113 * The template to render
114 * @param pageTitleKey
115 * The l10n key of the page title
116 * @param webInterface
117 * The Sone web interface
118 * @param requireLogin
119 * Whether this page requires a login
121 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
122 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
123 this.pageTitleKey = pageTitleKey;
124 this.webInterface = webInterface;
125 this.requireLogin = requireLogin;
133 * Returns the currently logged in Sone.
135 * @param toadletContext
136 * The toadlet context
137 * @return The currently logged in Sone, or {@code null} if no Sone is
138 * currently logged in
140 protected Sone getCurrentSone(ToadletContext toadletContext) {
141 return webInterface.getCurrentSone(toadletContext);
145 * Returns the currently logged in Sone.
147 * @param toadletContext
148 * The toadlet context
150 * {@code true} to create a new session if no session exists,
151 * {@code false} to not create a new session
152 * @return The currently logged in Sone, or {@code null} if no Sone is
153 * currently logged in
155 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
156 return webInterface.getCurrentSone(toadletContext, create);
160 * Sets the currently logged in Sone.
162 * @param toadletContext
163 * The toadlet context
165 * The Sone to set as currently logged in
167 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
168 webInterface.setCurrentSone(toadletContext, sone);
172 // TEMPLATEPAGE METHODS
179 protected String getPageTitle(FreenetRequest request) {
180 if (pageTitleKey != null) {
181 return webInterface.getL10n().getString(pageTitleKey);
190 protected List<Map<String, String>> getAdditionalLinkNodes(FreenetRequest request) {
191 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();
198 protected Collection<String> getStyleSheets() {
199 return Arrays.asList("css/sone.css");
206 protected String getShortcutIcon() {
207 return "images/icon.png";
211 * Returns whether this page requires the user to log in.
213 * @return {@code true} if the user is required to be logged in to use this
214 * page, {@code false} otherwise
216 protected boolean requiresLogin() {
224 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
225 super.processTemplate(request, templateContext);
226 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
227 templateContext.set("core", webInterface.getCore());
228 templateContext.set("currentSone", currentSone);
229 templateContext.set("localSones", webInterface.getCore().getLocalSones());
230 templateContext.set("request", request);
231 templateContext.set("currentVersion", SonePlugin.VERSION);
232 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
233 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
234 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
235 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
236 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
237 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
238 templateContext.set("notifications", notifications);
239 templateContext.set("notificationHash", notifications.hashCode());
246 protected String getRedirectTarget(FreenetRequest request) {
247 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
248 HTTPRequest httpRequest = request.getHttpRequest();
249 String originalUrl = httpRequest.getPath();
250 if (httpRequest.hasParameters()) {
251 StringBuilder requestParameters = new StringBuilder();
252 for (String parameterName : httpRequest.getParameterNames()) {
253 if (requestParameters.length() > 0) {
254 requestParameters.append("%26");
256 String[] parameterValues = httpRequest.getMultipleParam(parameterName);
257 for (String parameterValue : parameterValues) {
259 requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
260 } catch (UnsupportedEncodingException uee1) {
261 /* A JVM without UTF-8? I don’t think so. */
265 originalUrl += "?" + requestParameters.toString();
267 return "login.html?target=" + originalUrl;
276 protected boolean isFullAccessOnly() {
277 return webInterface.getCore().getPreferences().isRequireFullAccess();
284 public boolean isEnabled(ToadletContext toadletContext) {
285 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
288 if (requiresLogin()) {
289 return getCurrentSone(toadletContext, false) != null;