2 * Freetalk - FreetalkTemplatePage.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;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.main.SonePlugin;
27 import net.pterodactylus.sone.web.page.Page;
28 import net.pterodactylus.sone.web.page.TemplatePage;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContext;
31 import freenet.clients.http.SessionManager.Session;
32 import freenet.clients.http.ToadletContext;
33 import freenet.support.api.HTTPRequest;
36 * Base page for the Freetalk web interface.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class SoneTemplatePage extends TemplatePage {
43 protected final WebInterface webInterface;
45 /** Whether to require a login. */
46 private final boolean requireLogin;
49 * Creates a new template page for Freetalk that does not require the user
53 * The path of the page
55 * The template to render
57 * The l10n key of the page title
59 * The Sone web interface
61 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
62 this(path, template, pageTitleKey, webInterface, false);
66 * Creates a new template page for Freetalk.
69 * The path of the page
71 * The template to render
73 * The l10n key of the page title
75 * The Sone web interface
77 * Whether this page requires a login
79 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
80 super(path, webInterface.getTemplateContextFactory(), template, webInterface.getL10n(), pageTitleKey, "noPermission.html");
81 this.webInterface = webInterface;
82 this.requireLogin = requireLogin;
83 template.getInitialContext().set("webInterface", webInterface);
91 * Returns the current session, creating a new session if there is no
94 * @param toadletContenxt
96 * @return The current session, or {@code null} if there is no current
99 protected Session getCurrentSession(ToadletContext toadletContenxt) {
100 return webInterface.getCurrentSession(toadletContenxt);
104 * Returns the current session, creating a new session if there is no
105 * current session and {@code create} is {@code true}.
107 * @param toadletContenxt
108 * The toadlet context
110 * {@code true} to create a new session if there is no current
111 * session, {@code false} otherwise
112 * @return The current session, or {@code null} if there is no current
115 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
116 return webInterface.getCurrentSession(toadletContenxt, create);
120 * Returns the currently logged in Sone.
122 * @param toadletContext
123 * The toadlet context
124 * @return The currently logged in Sone, or {@code null} if no Sone is
125 * currently logged in
127 protected Sone getCurrentSone(ToadletContext toadletContext) {
128 return webInterface.getCurrentSone(toadletContext);
132 * Returns the currently logged in Sone.
134 * @param toadletContext
135 * The toadlet context
137 * {@code true} to create a new session if no session exists,
138 * {@code false} to not create a new session
139 * @return The currently logged in Sone, or {@code null} if no Sone is
140 * currently logged in
142 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
143 return webInterface.getCurrentSone(toadletContext, create);
147 * Sets the currently logged in Sone.
149 * @param toadletContext
150 * The toadlet context
152 * The Sone to set as currently logged in
154 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
155 webInterface.setCurrentSone(toadletContext, sone);
159 // TEMPLATEPAGE METHODS
166 protected Collection<String> getStyleSheets() {
167 return Arrays.asList("css/sone.css");
174 protected String getShortcutIcon() {
175 return "images/icon.png";
179 * Returns whether this page requires the user to log in.
181 * @return {@code true} if the user is required to be logged in to use this
182 * page, {@code false} otherwise
184 protected boolean requiresLogin() {
192 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
193 super.processTemplate(request, templateContext);
194 templateContext.set("currentSone", getCurrentSone(request.getToadletContext(), false));
195 templateContext.set("localSones", webInterface.getCore().getLocalSones());
196 templateContext.set("request", request);
197 templateContext.set("currentVersion", SonePlugin.VERSION);
198 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
199 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
200 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
201 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
208 protected String getRedirectTarget(Page.Request request) {
209 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
210 HTTPRequest httpRequest = request.getHttpRequest();
211 String originalUrl = httpRequest.getPath();
212 if (httpRequest.hasParameters()) {
213 StringBuilder requestParameters = new StringBuilder();
214 for (String parameterName : httpRequest.getParameterNames()) {
215 if (requestParameters.length() > 0) {
216 requestParameters.append("%26");
218 String[] parameterValues = httpRequest.getMultipleParam(parameterName);
219 for (String parameterValue : parameterValues) {
221 requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
222 } catch (UnsupportedEncodingException uee1) {
223 /* A JVM without UTF-8? I don’t think so. */
227 originalUrl += "?" + requestParameters.toString();
229 return "login.html?target=" + originalUrl;
238 public boolean isEnabled(ToadletContext toadletContext) {
239 if (requiresLogin()) {
240 return getCurrentSone(toadletContext, false) != null;