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.util.Arrays;
21 import java.util.Collection;
22 import java.util.UUID;
24 import net.pterodactylus.sone.data.Sone;
25 import net.pterodactylus.sone.web.page.Page;
26 import net.pterodactylus.sone.web.page.TemplatePage;
27 import net.pterodactylus.util.template.Template;
28 import freenet.clients.http.SessionManager.Session;
29 import freenet.clients.http.ToadletContext;
32 * Base page for the Freetalk web interface.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class SoneTemplatePage extends TemplatePage {
39 protected final WebInterface webInterface;
42 * Creates a new template page for Freetalk.
45 * The path of the page
47 * The template to render
49 * The l10n key of the page title
51 * The Sone web interface
53 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
54 super(path, template, webInterface.l10n(), pageTitleKey);
55 this.webInterface = webInterface;
56 template.set("webInterface", webInterface);
64 * Returns the current session, creating a new session if there is no
67 * @param toadletContenxt
69 * @return The current session, or {@code null} if there is no current
72 protected Session getCurrentSession(ToadletContext toadletContenxt) {
73 return getCurrentSession(toadletContenxt, true);
77 * Returns the current session, creating a new session if there is no
78 * current session and {@code create} is {@code true}.
80 * @param toadletContenxt
83 * {@code true} to create a new session if there is no current
84 * session, {@code false} otherwise
85 * @return The current session, or {@code null} if there is no current
88 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
90 Session session = webInterface.sessionManager().useSession(toadletContenxt);
91 if (create && (session == null)) {
92 session = webInterface.sessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
95 } catch (freenet.clients.http.RedirectException re1) {
101 * Returns the currently logged in Sone.
103 * @param toadletContext
104 * The toadlet context
105 * @return The currently logged in Sone, or {@code null} if no Sone is
106 * currently logged in
108 protected Sone getCurrentSone(ToadletContext toadletContext) {
109 Session session = getCurrentSession(toadletContext);
110 if (session == null) {
113 String soneId = (String) session.getAttribute("Sone.CurrentSone");
114 if (soneId == null) {
117 for (Sone sone : webInterface.core().getSones()) {
118 if (sone.getId().equals(soneId)) {
126 * Sets the currently logged in Sone.
128 * @param toadletContext
129 * The toadlet context
131 * The Sone to set as currently logged in
133 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
134 Session session = getCurrentSession(toadletContext);
136 session.removeAttribute("Sone.CurrentSone");
138 session.setAttribute("Sone.CurrentSone", sone.getId());
143 // TEMPLATEPAGE METHODS
150 protected Collection<String> getStyleSheets() {
151 return Arrays.asList("css/sone.css");
155 * Returns whether this page requires the user to log in.
157 * @return {@code true} if the user is required to be logged in to use this
158 * page, {@code false} otherwise
160 protected boolean requiresLogin() {
168 protected void processTemplate(Request request, Template template) throws RedirectException {
169 super.processTemplate(request, template);
170 template.set("currentSone", getCurrentSone(request.getToadletContext()));
177 protected String getRedirectTarget(Page.Request request) {
178 if (requiresLogin() && (getCurrentSone(request.getToadletContext()) == null)) {
188 public boolean isEnabled(ToadletContext toadletContext) {
189 if (requiresLogin()) {
190 return getCurrentSone(toadletContext) != null;