Fix parameter name.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SoneTemplatePage.java
1 /*
2  * Freetalk - FreetalkTemplatePage.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.UUID;
23
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;
30
31 /**
32  * Base page for the Freetalk web interface.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class SoneTemplatePage extends TemplatePage {
37
38         /** The Sone core. */
39         protected final WebInterface webInterface;
40
41         /**
42          * Creates a new template page for Freetalk.
43          *
44          * @param path
45          *            The path of the page
46          * @param template
47          *            The template to render
48          * @param pageTitleKey
49          *            The l10n key of the page title
50          * @param webInterface
51          *            The Sone web interface
52          */
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);
57         }
58
59         //
60         // PROTECTED METHODS
61         //
62
63         /**
64          * Returns the current session, creating a new session if there is no
65          * current session.
66          *
67          * @param toadletContenxt
68          *            The toadlet context
69          * @return The current session, or {@code null} if there is no current
70          *         session
71          */
72         protected Session getCurrentSession(ToadletContext toadletContenxt) {
73                 return getCurrentSession(toadletContenxt, true);
74         }
75
76         /**
77          * Returns the current session, creating a new session if there is no
78          * current session and {@code create} is {@code true}.
79          *
80          * @param toadletContenxt
81          *            The toadlet context
82          * @param create
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
86          *         session
87          */
88         protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
89                 try {
90                         Session session = webInterface.sessionManager().useSession(toadletContenxt);
91                         if (create && (session == null)) {
92                                 session = webInterface.sessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
93                         }
94                         return session;
95                 } catch (freenet.clients.http.RedirectException re1) {
96                         return null;
97                 }
98         }
99
100         /**
101          * Returns the currently logged in Sone.
102          *
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
107          */
108         protected Sone getCurrentSone(ToadletContext toadletContext) {
109                 Session session = getCurrentSession(toadletContext);
110                 if (session == null) {
111                         return null;
112                 }
113                 String soneId = (String) session.getAttribute("Sone.CurrentSone");
114                 for (Sone sone : webInterface.core().getSones()) {
115                         if (sone.getId().equals(soneId)) {
116                                 return sone;
117                         }
118                 }
119                 return null;
120         }
121
122         /**
123          * Sets the currently logged in Sone.
124          *
125          * @param toadletContext
126          *            The toadlet context
127          * @param sone
128          *            The Sone to set as currently logged in
129          */
130         protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
131                 Session session = getCurrentSession(toadletContext);
132                 if (sone == null) {
133                         session.removeAttribute("Sone.CurrentSone");
134                 } else {
135                         session.setAttribute("Sone.CurrentSone", sone.getId());
136                 }
137         }
138
139         //
140         // TEMPLATEPAGE METHODS
141         //
142
143         /**
144          * {@inheritDoc}
145          */
146         @Override
147         protected Collection<String> getStyleSheets() {
148                 return Arrays.asList("css/sone.css");
149         }
150
151         /**
152          * Returns whether this page requires the user to log in.
153          *
154          * @return {@code true} if the user is required to be logged in to use this
155          *         page, {@code false} otherwise
156          */
157         protected boolean requiresLogin() {
158                 return false;
159         }
160
161         /**
162          * {@inheritDoc}
163          */
164         @Override
165         protected void processTemplate(Request request, Template template) throws RedirectException {
166                 super.processTemplate(request, template);
167                 template.set("currentSone", getCurrentSone(request.getToadletContext()));
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         @Override
174         protected String getRedirectTarget(Page.Request request) {
175                 if (requiresLogin() && (getCurrentSone(request.getToadletContext()) == null)) {
176                         return "login.html";
177                 }
178                 return null;
179         }
180
181         /**
182          * {@inheritDoc}
183          */
184         @Override
185         public boolean isEnabled(ToadletContext toadletContext) {
186                 if (requiresLogin()) {
187                         return getCurrentSone(toadletContext) != null;
188                 }
189                 return true;
190         }
191
192 }