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.FreenetTemplatePage;
31 import net.pterodactylus.sone.web.page.Page;
32 import net.pterodactylus.util.collection.ListBuilder;
33 import net.pterodactylus.util.collection.MapBuilder;
34 import net.pterodactylus.util.template.Template;
35 import net.pterodactylus.util.template.TemplateContext;
36 import freenet.clients.http.SessionManager.Session;
37 import freenet.clients.http.ToadletContext;
38 import freenet.support.api.HTTPRequest;
41 * Base page for the Sone web interface.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class SoneTemplatePage extends FreenetTemplatePage {
48 protected final WebInterface webInterface;
50 /** The page title l10n key. */
51 private final String pageTitleKey;
53 /** Whether to require a login. */
54 private final boolean requireLogin;
57 * Creates a new template page for Sone that does not require the user to be
61 * The path of the page
63 * The template to render
65 * The Sone web interface
67 public SoneTemplatePage(String path, Template template, WebInterface webInterface) {
68 this(path, template, null, webInterface, false);
72 * Creates a new template page for Sone that does not require the user to be
76 * The path of the page
78 * The template to render
80 * The l10n key of the page title
82 * The Sone web interface
84 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
85 this(path, template, pageTitleKey, webInterface, false);
89 * Creates a new template page for Sone.
92 * The path of the page
94 * The template to render
96 * The Sone web interface
98 * Whether this page requires a login
100 public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
101 this(path, template, null, webInterface, requireLogin);
105 * Creates a new template page for Sone.
108 * The path of the page
110 * The template to render
111 * @param pageTitleKey
112 * The l10n key of the page title
113 * @param webInterface
114 * The Sone web interface
115 * @param requireLogin
116 * Whether this page requires a login
118 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
119 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
120 this.pageTitleKey = pageTitleKey;
121 this.webInterface = webInterface;
122 this.requireLogin = requireLogin;
123 template.getInitialContext().set("webInterface", webInterface);
131 * Returns the current session, creating a new session if there is no
134 * @param toadletContenxt
135 * The toadlet context
136 * @return The current session, or {@code null} if there is no current
139 protected Session getCurrentSession(ToadletContext toadletContenxt) {
140 return webInterface.getCurrentSession(toadletContenxt);
144 * Returns the current session, creating a new session if there is no
145 * current session and {@code create} is {@code true}.
147 * @param toadletContenxt
148 * The toadlet context
150 * {@code true} to create a new session if there is no current
151 * session, {@code false} otherwise
152 * @return The current session, or {@code null} if there is no current
155 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
156 return webInterface.getCurrentSession(toadletContenxt, create);
160 * Returns the currently logged in Sone.
162 * @param toadletContext
163 * The toadlet context
164 * @return The currently logged in Sone, or {@code null} if no Sone is
165 * currently logged in
167 protected Sone getCurrentSone(ToadletContext toadletContext) {
168 return webInterface.getCurrentSone(toadletContext);
172 * Returns the currently logged in Sone.
174 * @param toadletContext
175 * The toadlet context
177 * {@code true} to create a new session if no session exists,
178 * {@code false} to not create a new session
179 * @return The currently logged in Sone, or {@code null} if no Sone is
180 * currently logged in
182 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
183 return webInterface.getCurrentSone(toadletContext, create);
187 * Sets the currently logged in Sone.
189 * @param toadletContext
190 * The toadlet context
192 * The Sone to set as currently logged in
194 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
195 webInterface.setCurrentSone(toadletContext, sone);
199 // TEMPLATEPAGE METHODS
206 protected String getPageTitle(Request request) {
207 if (pageTitleKey != null) {
208 return webInterface.getL10n().getString(pageTitleKey);
217 protected List<Map<String, String>> getAdditionalLinkNodes(Request request) {
218 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();
225 protected Collection<String> getStyleSheets() {
226 return Arrays.asList("css/sone.css");
233 protected String getShortcutIcon() {
234 return "images/icon.png";
238 * Returns whether this page requires the user to log in.
240 * @return {@code true} if the user is required to be logged in to use this
241 * page, {@code false} otherwise
243 protected boolean requiresLogin() {
251 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
252 super.processTemplate(request, templateContext);
253 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
254 templateContext.set("currentSone", currentSone);
255 templateContext.set("localSones", webInterface.getCore().getLocalSones());
256 templateContext.set("request", request);
257 templateContext.set("currentVersion", SonePlugin.VERSION);
258 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
259 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
260 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
261 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
262 templateContext.set("notifications", ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone));
269 protected String getRedirectTarget(Page.Request request) {
270 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
271 HTTPRequest httpRequest = request.getHttpRequest();
272 String originalUrl = httpRequest.getPath();
273 if (httpRequest.hasParameters()) {
274 StringBuilder requestParameters = new StringBuilder();
275 for (String parameterName : httpRequest.getParameterNames()) {
276 if (requestParameters.length() > 0) {
277 requestParameters.append("%26");
279 String[] parameterValues = httpRequest.getMultipleParam(parameterName);
280 for (String parameterValue : parameterValues) {
282 requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
283 } catch (UnsupportedEncodingException uee1) {
284 /* A JVM without UTF-8? I don’t think so. */
288 originalUrl += "?" + requestParameters.toString();
290 return "login.html?target=" + originalUrl;
299 public boolean isEnabled(ToadletContext toadletContext) {
300 if (requiresLogin()) {
301 return getCurrentSone(toadletContext, false) != null;