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.LocalSone;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.main.SonePlugin;
31 import net.pterodactylus.sone.notify.ListNotificationFilters;
32 import net.pterodactylus.sone.web.page.FreenetRequest;
33 import net.pterodactylus.sone.web.page.FreenetTemplatePage;
34 import net.pterodactylus.util.notify.Notification;
35 import net.pterodactylus.util.template.Template;
36 import net.pterodactylus.util.template.TemplateContext;
38 import com.google.common.base.Optional;
39 import com.google.common.collect.ImmutableList;
40 import com.google.common.collect.ImmutableMap;
42 import freenet.clients.http.ToadletContext;
43 import freenet.support.api.HTTPRequest;
46 * Base page for the Sone web interface.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class SoneTemplatePage extends FreenetTemplatePage {
53 protected final WebInterface webInterface;
55 /** The page title l10n key. */
56 private final String pageTitleKey;
58 /** Whether to require a login. */
59 private final boolean requireLogin;
62 * Creates a new template page for Sone that does not require the user to be
66 * The path of the page
68 * The template to render
70 * The Sone web interface
72 public SoneTemplatePage(String path, Template template, WebInterface webInterface) {
73 this(path, template, null, webInterface, false);
77 * Creates a new template page for Sone that does not require the user to be
81 * The path of the page
83 * The template to render
85 * The l10n key of the page title
87 * The Sone web interface
89 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
90 this(path, template, pageTitleKey, webInterface, false);
94 * Creates a new template page for Sone.
97 * The path of the page
99 * The template to render
100 * @param webInterface
101 * The Sone web interface
102 * @param requireLogin
103 * Whether this page requires a login
105 public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
106 this(path, template, null, webInterface, requireLogin);
110 * Creates a new template page for Sone.
113 * The path of the page
115 * The template to render
116 * @param pageTitleKey
117 * The l10n key of the page title
118 * @param webInterface
119 * The Sone web interface
120 * @param requireLogin
121 * Whether this page requires a login
123 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
124 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
125 this.pageTitleKey = pageTitleKey;
126 this.webInterface = webInterface;
127 this.requireLogin = requireLogin;
135 * Returns the currently logged in Sone.
137 * @param toadletContext
138 * The toadlet context
139 * @return The currently logged in Sone, or {@code null} if no Sone is
140 * currently logged in
142 protected Optional<LocalSone> getCurrentSone(ToadletContext toadletContext) {
143 return webInterface.getCurrentSone(toadletContext);
147 * Returns the currently logged in Sone.
149 * @param toadletContext
150 * The toadlet context
152 * {@code true} to create a new session if no session exists,
153 * {@code false} to not create a new session
154 * @return The currently logged in Sone, or {@code null} if no Sone is
155 * currently logged in
157 protected Optional<LocalSone> getCurrentSone(ToadletContext toadletContext, boolean create) {
158 return webInterface.getCurrentSone(toadletContext, create);
162 * Sets the currently logged in Sone.
164 * @param toadletContext
165 * The toadlet context
167 * The Sone to set as currently logged in
169 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
170 webInterface.setCurrentSone(toadletContext, sone);
174 // TEMPLATEPAGE METHODS
181 protected String getPageTitle(FreenetRequest request) {
182 if (pageTitleKey != null) {
183 return webInterface.getL10n().getString(pageTitleKey);
192 protected List<Map<String, String>> getAdditionalLinkNodes(FreenetRequest request) {
193 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();
200 protected Collection<String> getStyleSheets() {
201 return Arrays.asList("css/sone.css");
208 protected String getShortcutIcon() {
209 return "images/icon.png";
213 * Returns whether this page requires the user to log in.
215 * @return {@code true} if the user is required to be logged in to use this
216 * page, {@code false} otherwise
218 protected boolean requiresLogin() {
226 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
227 super.processTemplate(request, templateContext);
228 Optional<LocalSone> currentSone = getCurrentSone(request.getToadletContext(), false);
229 templateContext.set("core", webInterface.getCore());
230 templateContext.set("currentSone", currentSone.orNull());
231 templateContext.set("localSones", webInterface.getCore().getLocalSones());
232 templateContext.set("request", request);
233 templateContext.set("currentVersion", SonePlugin.VERSION);
234 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
235 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
236 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
237 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
238 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone.orNull());
239 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
240 templateContext.set("notifications", notifications);
241 templateContext.set("notificationHash", notifications.hashCode());
248 protected String getRedirectTarget(FreenetRequest request) {
249 if (requiresLogin() && !getCurrentSone(request.getToadletContext(), false).isPresent()) {
250 HTTPRequest httpRequest = request.getHttpRequest();
251 String originalUrl = httpRequest.getPath();
252 if (httpRequest.hasParameters()) {
253 StringBuilder requestParameters = new StringBuilder();
254 for (String parameterName : httpRequest.getParameterNames()) {
255 if (requestParameters.length() > 0) {
256 requestParameters.append("%26");
258 String[] parameterValues = httpRequest.getMultipleParam(parameterName);
259 for (String parameterValue : parameterValues) {
261 requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
262 } catch (UnsupportedEncodingException uee1) {
263 /* A JVM without UTF-8? I don’t think so. */
267 originalUrl += "?" + requestParameters.toString();
269 return "login.html?target=" + originalUrl;
278 protected boolean isFullAccessOnly() {
279 return webInterface.getCore().getPreferences().isRequireFullAccess();
286 public boolean isEnabled(ToadletContext toadletContext) {
287 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
290 if (requiresLogin()) {
291 return getCurrentSone(toadletContext, false).isPresent();