2 * Sone - SoneTemplatePage.java - Copyright © 2010–2016 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.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
29 import javax.annotation.Nonnull;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.main.SonePlugin;
33 import net.pterodactylus.sone.web.page.FreenetRequest;
34 import net.pterodactylus.sone.web.page.FreenetTemplatePage;
35 import net.pterodactylus.util.notify.Notification;
36 import net.pterodactylus.util.template.Template;
37 import net.pterodactylus.util.template.TemplateContext;
39 import freenet.clients.http.SessionManager.Session;
40 import freenet.clients.http.ToadletContext;
41 import freenet.support.api.HTTPRequest;
43 import com.google.common.collect.ImmutableList;
44 import com.google.common.collect.ImmutableMap;
47 * Base page for the Sone web interface.
49 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51 public class SoneTemplatePage extends FreenetTemplatePage {
54 protected final WebInterface webInterface;
56 /** The page title l10n key. */
57 private final String pageTitleKey;
59 /** Whether to require a login. */
60 private final boolean requireLogin;
63 * Creates a new template page for Sone that does not require the user to be
67 * The path of the page
69 * The template to render
71 * The l10n key of the page title
73 * The Sone web interface
75 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
76 this(path, template, pageTitleKey, webInterface, false);
80 * Creates a new template page for Sone.
83 * The path of the page
85 * The template to render
87 * The Sone web interface
89 * Whether this page requires a login
91 public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
92 this(path, template, null, webInterface, requireLogin);
96 * Creates a new template page for Sone.
99 * The path of the page
101 * The template to render
102 * @param pageTitleKey
103 * The l10n key of the page title
104 * @param webInterface
105 * The Sone web interface
106 * @param requireLogin
107 * Whether this page requires a login
109 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
110 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
111 this.pageTitleKey = pageTitleKey;
112 this.webInterface = webInterface;
113 this.requireLogin = requireLogin;
121 * Returns the currently logged in Sone.
123 * @param toadletContext
124 * The toadlet context
125 * @return The currently logged in Sone, or {@code null} if no Sone is
126 * currently logged in
128 protected Sone getCurrentSone(ToadletContext toadletContext) {
129 return webInterface.getCurrentSone(toadletContext);
133 * Returns the currently logged in Sone.
135 * @param toadletContext
136 * The toadlet context
138 * {@code true} to create a new session if no session exists,
139 * {@code false} to not create a new session
140 * @return The currently logged in Sone, or {@code null} if no Sone is
141 * currently logged in
143 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
144 return webInterface.getCurrentSone(toadletContext, create);
148 * Sets the currently logged in Sone.
150 * @param toadletContext
151 * The toadlet context
153 * The Sone to set as currently logged in
155 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
156 webInterface.setCurrentSone(toadletContext, sone);
160 // TEMPLATEPAGE METHODS
167 protected String getPageTitle(FreenetRequest request) {
168 if (pageTitleKey != null) {
169 return webInterface.getL10n().getString(pageTitleKey);
178 protected List<Map<String, String>> getAdditionalLinkNodes(FreenetRequest request) {
179 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();
186 protected Collection<String> getStyleSheets() {
187 return Arrays.asList("css/sone.css");
194 protected String getShortcutIcon() {
195 return "images/icon.png";
199 * Returns whether this page requires the user to log in.
201 * @return {@code true} if the user is required to be logged in to use this
202 * page, {@code false} otherwise
204 protected boolean requiresLogin() {
212 protected final void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
213 super.processTemplate(request, templateContext);
214 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
215 templateContext.set("core", webInterface.getCore());
216 templateContext.set("currentSone", currentSone);
217 templateContext.set("localSones", webInterface.getCore().getLocalSones());
218 templateContext.set("request", request);
219 templateContext.set("currentVersion", SonePlugin.getPluginVersion());
220 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
221 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
222 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
223 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
224 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications(currentSone));
225 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
226 templateContext.set("notifications", notifications);
227 templateContext.set("notificationHash", notifications.hashCode());
228 handleRequest(request, templateContext);
231 protected void handleRequest(@Nonnull FreenetRequest request, @Nonnull TemplateContext templateContext) throws RedirectException {
238 protected String getRedirectTarget(FreenetRequest request) {
239 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
240 HTTPRequest httpRequest = request.getHttpRequest();
241 String originalUrl = httpRequest.getPath();
242 if (httpRequest.hasParameters()) {
243 StringBuilder requestParameters = new StringBuilder();
244 for (String parameterName : httpRequest.getParameterNames()) {
245 if (requestParameters.length() > 0) {
246 requestParameters.append("&");
248 String[] parameterValues = httpRequest.getMultipleParam(parameterName);
249 for (String parameterValue : parameterValues) {
250 requestParameters.append(urlEncode(parameterName)).append("=").append(urlEncode(parameterValue));
253 originalUrl += "?" + requestParameters.toString();
255 return "login.html?target=" + urlEncode(originalUrl);
260 private static String urlEncode(String value) {
262 return URLEncoder.encode(value, "UTF-8");
263 } catch (UnsupportedEncodingException uee1) {
264 /* A JVM without UTF-8? I don’t think so. */
265 throw new RuntimeException(uee1);
273 protected boolean isFullAccessOnly() {
274 return webInterface.getCore().getPreferences().isRequireFullAccess();
281 public boolean isEnabled(ToadletContext toadletContext) {
282 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
285 if (requiresLogin()) {
286 return getCurrentSone(toadletContext, false) != null;