2 * Sone - SoneTemplatePage.java - Copyright © 2010 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.List;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.main.SonePlugin;
29 import net.pterodactylus.sone.notify.ListNotificationFilters;
30 import net.pterodactylus.sone.web.page.FreenetRequest;
31 import net.pterodactylus.sone.web.page.FreenetTemplatePage;
32 import net.pterodactylus.util.collection.ListBuilder;
33 import net.pterodactylus.util.collection.MapBuilder;
34 import net.pterodactylus.util.notify.Notification;
35 import net.pterodactylus.util.object.HashCode;
36 import net.pterodactylus.util.template.Template;
37 import net.pterodactylus.util.template.TemplateContext;
38 import freenet.clients.http.SessionManager.Session;
39 import freenet.clients.http.ToadletContext;
40 import freenet.support.api.HTTPRequest;
43 * Base page for the Sone web interface.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class SoneTemplatePage extends FreenetTemplatePage {
50 protected final WebInterface webInterface;
52 /** The page title l10n key. */
53 private final String pageTitleKey;
55 /** Whether to require a login. */
56 private final boolean requireLogin;
59 * Creates a new template page for Sone that does not require the user to be
63 * The path of the page
65 * The template to render
67 * The Sone web interface
69 public SoneTemplatePage(String path, Template template, WebInterface webInterface) {
70 this(path, template, null, webInterface, false);
74 * Creates a new template page for Sone that does not require the user to be
78 * The path of the page
80 * The template to render
82 * The l10n key of the page title
84 * The Sone web interface
86 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
87 this(path, template, pageTitleKey, webInterface, false);
91 * Creates a new template page for Sone.
94 * The path of the page
96 * The template to render
98 * The Sone web interface
100 * Whether this page requires a login
102 public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
103 this(path, template, null, webInterface, requireLogin);
107 * Creates a new template page for Sone.
110 * The path of the page
112 * The template to render
113 * @param pageTitleKey
114 * The l10n key of the page title
115 * @param webInterface
116 * The Sone web interface
117 * @param requireLogin
118 * Whether this page requires a login
120 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
121 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
122 this.pageTitleKey = pageTitleKey;
123 this.webInterface = webInterface;
124 this.requireLogin = requireLogin;
132 * Returns the current session, creating a new session if there is no
135 * @param toadletContenxt
136 * The toadlet context
137 * @return The current session, or {@code null} if there is no current
140 protected Session getCurrentSession(ToadletContext toadletContenxt) {
141 return webInterface.getCurrentSession(toadletContenxt);
145 * Returns the current session, creating a new session if there is no
146 * current session and {@code create} is {@code true}.
148 * @param toadletContenxt
149 * The toadlet context
151 * {@code true} to create a new session if there is no current
152 * session, {@code false} otherwise
153 * @return The current session, or {@code null} if there is no current
156 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
157 return webInterface.getCurrentSession(toadletContenxt, create);
161 * Returns the currently logged in Sone.
163 * @param toadletContext
164 * The toadlet context
165 * @return The currently logged in Sone, or {@code null} if no Sone is
166 * currently logged in
168 protected Sone getCurrentSone(ToadletContext toadletContext) {
169 return webInterface.getCurrentSone(toadletContext);
173 * Returns the currently logged in Sone.
175 * @param toadletContext
176 * The toadlet context
178 * {@code true} to create a new session if no session exists,
179 * {@code false} to not create a new session
180 * @return The currently logged in Sone, or {@code null} if no Sone is
181 * currently logged in
183 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
184 return webInterface.getCurrentSone(toadletContext, create);
188 * Sets the currently logged in Sone.
190 * @param toadletContext
191 * The toadlet context
193 * The Sone to set as currently logged in
195 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
196 webInterface.setCurrentSone(toadletContext, sone);
200 // TEMPLATEPAGE METHODS
207 protected String getPageTitle(FreenetRequest request) {
208 if (pageTitleKey != null) {
209 return webInterface.getL10n().getString(pageTitleKey);
218 protected List<Map<String, String>> getAdditionalLinkNodes(FreenetRequest request) {
219 return new ListBuilder<Map<String, String>>().add(new MapBuilder<String, String>().put("rel", "search").put("type", "application/opensearchdescription+xml").put("title", "Sone").put("href", "http://" + request.getHttpRequest().getHeader("host") + "/Sone/OpenSearch.xml").get()).get();
226 protected Collection<String> getStyleSheets() {
227 return Arrays.asList("css/sone.css");
234 protected String getShortcutIcon() {
235 return "images/icon.png";
239 * Returns whether this page requires the user to log in.
241 * @return {@code true} if the user is required to be logged in to use this
242 * page, {@code false} otherwise
244 protected boolean requiresLogin() {
252 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
253 super.processTemplate(request, templateContext);
254 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
255 templateContext.set("core", webInterface.getCore());
256 templateContext.set("currentSone", currentSone);
257 templateContext.set("localSones", webInterface.getCore().getLocalSones());
258 templateContext.set("request", request);
259 templateContext.set("currentVersion", SonePlugin.VERSION);
260 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
261 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
262 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
263 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
264 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
265 templateContext.set("notifications", notifications);
266 templateContext.set("notificationHash", HashCode.hashCode(notifications));
273 protected String getRedirectTarget(FreenetRequest request) {
274 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
275 HTTPRequest httpRequest = request.getHttpRequest();
276 String originalUrl = httpRequest.getPath();
277 if (httpRequest.hasParameters()) {
278 StringBuilder requestParameters = new StringBuilder();
279 for (String parameterName : httpRequest.getParameterNames()) {
280 if (requestParameters.length() > 0) {
281 requestParameters.append("%26");
283 String[] parameterValues = httpRequest.getMultipleParam(parameterName);
284 for (String parameterValue : parameterValues) {
286 requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
287 } catch (UnsupportedEncodingException uee1) {
288 /* A JVM without UTF-8? I don’t think so. */
292 originalUrl += "?" + requestParameters.toString();
294 return "login.html?target=" + originalUrl;
303 protected boolean isFullAccessOnly() {
304 return webInterface.getCore().getPreferences().isRequireFullAccess();
311 public boolean isEnabled(ToadletContext toadletContext) {
312 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
315 if (requiresLogin()) {
316 return getCurrentSone(toadletContext, false) != null;