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 /** The page title l10n key. */
46 private final String pageTitleKey;
48 /** Whether to require a login. */
49 private final boolean requireLogin;
52 * Creates a new template page for Freetalk that does not require the user
56 * The path of the page
58 * The template to render
60 * The Sone web interface
62 public SoneTemplatePage(String path, Template template, WebInterface webInterface) {
63 this(path, template, null, webInterface, false);
67 * Creates a new template page for Freetalk that does not require the user
71 * The path of the page
73 * The template to render
75 * The l10n key of the page title
77 * The Sone web interface
79 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
80 this(path, template, pageTitleKey, webInterface, false);
84 * Creates a new template page for Freetalk.
87 * The path of the page
89 * The template to render
91 * The Sone web interface
93 * Whether this page requires a login
95 public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
96 this(path, template, null, webInterface, requireLogin);
100 * Creates a new template page for Freetalk.
103 * The path of the page
105 * The template to render
106 * @param pageTitleKey
107 * The l10n key of the page title
108 * @param webInterface
109 * The Sone web interface
110 * @param requireLogin
111 * Whether this page requires a login
113 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
114 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
115 this.pageTitleKey = pageTitleKey;
116 this.webInterface = webInterface;
117 this.requireLogin = requireLogin;
118 template.getInitialContext().set("webInterface", webInterface);
126 * Returns the current session, creating a new session if there is no
129 * @param toadletContenxt
130 * The toadlet context
131 * @return The current session, or {@code null} if there is no current
134 protected Session getCurrentSession(ToadletContext toadletContenxt) {
135 return webInterface.getCurrentSession(toadletContenxt);
139 * Returns the current session, creating a new session if there is no
140 * current session and {@code create} is {@code true}.
142 * @param toadletContenxt
143 * The toadlet context
145 * {@code true} to create a new session if there is no current
146 * session, {@code false} otherwise
147 * @return The current session, or {@code null} if there is no current
150 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
151 return webInterface.getCurrentSession(toadletContenxt, create);
155 * Returns the currently logged in Sone.
157 * @param toadletContext
158 * The toadlet context
159 * @return The currently logged in Sone, or {@code null} if no Sone is
160 * currently logged in
162 protected Sone getCurrentSone(ToadletContext toadletContext) {
163 return webInterface.getCurrentSone(toadletContext);
167 * Returns the currently logged in Sone.
169 * @param toadletContext
170 * The toadlet context
172 * {@code true} to create a new session if no session exists,
173 * {@code false} to not create a new session
174 * @return The currently logged in Sone, or {@code null} if no Sone is
175 * currently logged in
177 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
178 return webInterface.getCurrentSone(toadletContext, create);
182 * Sets the currently logged in Sone.
184 * @param toadletContext
185 * The toadlet context
187 * The Sone to set as currently logged in
189 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
190 webInterface.setCurrentSone(toadletContext, sone);
194 // TEMPLATEPAGE METHODS
201 protected String getPageTitle(Request request) {
202 if (pageTitleKey != null) {
203 return webInterface.getL10n().getString(pageTitleKey);
212 protected Collection<String> getStyleSheets() {
213 return Arrays.asList("css/sone.css");
220 protected String getShortcutIcon() {
221 return "images/icon.png";
225 * Returns whether this page requires the user to log in.
227 * @return {@code true} if the user is required to be logged in to use this
228 * page, {@code false} otherwise
230 protected boolean requiresLogin() {
238 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
239 super.processTemplate(request, templateContext);
240 templateContext.set("currentSone", getCurrentSone(request.getToadletContext(), false));
241 templateContext.set("localSones", webInterface.getCore().getLocalSones());
242 templateContext.set("request", request);
243 templateContext.set("currentVersion", SonePlugin.VERSION);
244 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
245 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
246 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
247 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
254 protected String getRedirectTarget(Page.Request request) {
255 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
256 HTTPRequest httpRequest = request.getHttpRequest();
257 String originalUrl = httpRequest.getPath();
258 if (httpRequest.hasParameters()) {
259 StringBuilder requestParameters = new StringBuilder();
260 for (String parameterName : httpRequest.getParameterNames()) {
261 if (requestParameters.length() > 0) {
262 requestParameters.append("%26");
264 String[] parameterValues = httpRequest.getMultipleParam(parameterName);
265 for (String parameterValue : parameterValues) {
267 requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
268 } catch (UnsupportedEncodingException uee1) {
269 /* A JVM without UTF-8? I don’t think so. */
273 originalUrl += "?" + requestParameters.toString();
275 return "login.html?target=" + originalUrl;
284 public boolean isEnabled(ToadletContext toadletContext) {
285 if (requiresLogin()) {
286 return getCurrentSone(toadletContext, false) != null;