2 * Sone - SoneTemplatePage.java - Copyright © 2010–2012 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.object.HashCode;
35 import net.pterodactylus.util.template.Template;
36 import net.pterodactylus.util.template.TemplateContext;
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.collect.ImmutableMap;
41 import freenet.clients.http.SessionManager.Session;
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 current session, creating a new session if there is no
138 * @param toadletContenxt
139 * The toadlet context
140 * @return The current session, or {@code null} if there is no current
143 protected Session getCurrentSession(ToadletContext toadletContenxt) {
144 return webInterface.getCurrentSession(toadletContenxt);
148 * Returns the current session, creating a new session if there is no
149 * current session and {@code create} is {@code true}.
151 * @param toadletContenxt
152 * The toadlet context
154 * {@code true} to create a new session if there is no current
155 * session, {@code false} otherwise
156 * @return The current session, or {@code null} if there is no current
159 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
160 return webInterface.getCurrentSession(toadletContenxt, create);
164 * Returns the currently logged in Sone.
166 * @param toadletContext
167 * The toadlet context
168 * @return The currently logged in Sone, or {@code null} if no Sone is
169 * currently logged in
171 protected Sone getCurrentSone(ToadletContext toadletContext) {
172 return webInterface.getCurrentSone(toadletContext);
176 * Returns the currently logged in Sone.
178 * @param toadletContext
179 * The toadlet context
181 * {@code true} to create a new session if no session exists,
182 * {@code false} to not create a new session
183 * @return The currently logged in Sone, or {@code null} if no Sone is
184 * currently logged in
186 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
187 return webInterface.getCurrentSone(toadletContext, create);
191 * Sets the currently logged in Sone.
193 * @param toadletContext
194 * The toadlet context
196 * The Sone to set as currently logged in
198 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
199 webInterface.setCurrentSone(toadletContext, sone);
203 // TEMPLATEPAGE METHODS
210 protected String getPageTitle(FreenetRequest request) {
211 if (pageTitleKey != null) {
212 return webInterface.getL10n().getString(pageTitleKey);
221 protected List<Map<String, String>> getAdditionalLinkNodes(FreenetRequest request) {
222 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();
229 protected Collection<String> getStyleSheets() {
230 return Arrays.asList("css/sone.css");
237 protected String getShortcutIcon() {
238 return "images/icon.png";
242 * Returns whether this page requires the user to log in.
244 * @return {@code true} if the user is required to be logged in to use this
245 * page, {@code false} otherwise
247 protected boolean requiresLogin() {
255 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
256 super.processTemplate(request, templateContext);
257 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
258 templateContext.set("core", webInterface.getCore());
259 templateContext.set("currentSone", currentSone);
260 templateContext.set("localSones", webInterface.getCore().getLocalSones());
261 templateContext.set("request", request);
262 templateContext.set("currentVersion", SonePlugin.VERSION);
263 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
264 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
265 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
266 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
267 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
268 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
269 templateContext.set("notifications", notifications);
270 templateContext.set("notificationHash", HashCode.hashCode(notifications));
277 protected String getRedirectTarget(FreenetRequest request) {
278 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
279 HTTPRequest httpRequest = request.getHttpRequest();
280 String originalUrl = httpRequest.getPath();
281 if (httpRequest.hasParameters()) {
282 StringBuilder requestParameters = new StringBuilder();
283 for (String parameterName : httpRequest.getParameterNames()) {
284 if (requestParameters.length() > 0) {
285 requestParameters.append("%26");
287 String[] parameterValues = httpRequest.getMultipleParam(parameterName);
288 for (String parameterValue : parameterValues) {
290 requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
291 } catch (UnsupportedEncodingException uee1) {
292 /* A JVM without UTF-8? I don’t think so. */
296 originalUrl += "?" + requestParameters.toString();
298 return "login.html?target=" + originalUrl;
307 protected boolean isFullAccessOnly() {
308 return webInterface.getCore().getPreferences().isRequireFullAccess();
315 public boolean isEnabled(ToadletContext toadletContext) {
316 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
319 if (requiresLogin()) {
320 return getCurrentSone(toadletContext, false) != null;