Use traditional getter 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         /** Whether to require a login. */
42         private final boolean requireLogin;
43
44         /**
45          * Creates a new template page for Freetalk that does not require the user
46          * to be logged in.
47          *
48          * @param path
49          *            The path of the page
50          * @param template
51          *            The template to render
52          * @param pageTitleKey
53          *            The l10n key of the page title
54          * @param webInterface
55          *            The Sone web interface
56          */
57         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
58                 this(path, template, pageTitleKey, webInterface, false);
59         }
60
61         /**
62          * Creates a new template page for Freetalk.
63          *
64          * @param path
65          *            The path of the page
66          * @param template
67          *            The template to render
68          * @param pageTitleKey
69          *            The l10n key of the page title
70          * @param webInterface
71          *            The Sone web interface
72          * @param requireLogin
73          *            Whether this page requires a login
74          */
75         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
76                 super(path, template, webInterface.getL10n(), pageTitleKey, "noPermission.html");
77                 this.webInterface = webInterface;
78                 this.requireLogin = requireLogin;
79                 template.set("webInterface", webInterface);
80         }
81
82         //
83         // PROTECTED METHODS
84         //
85
86         /**
87          * Returns the current session, creating a new session if there is no
88          * current session.
89          *
90          * @param toadletContenxt
91          *            The toadlet context
92          * @return The current session, or {@code null} if there is no current
93          *         session
94          */
95         protected Session getCurrentSession(ToadletContext toadletContenxt) {
96                 return getCurrentSession(toadletContenxt, true);
97         }
98
99         /**
100          * Returns the current session, creating a new session if there is no
101          * current session and {@code create} is {@code true}.
102          *
103          * @param toadletContenxt
104          *            The toadlet context
105          * @param create
106          *            {@code true} to create a new session if there is no current
107          *            session, {@code false} otherwise
108          * @return The current session, or {@code null} if there is no current
109          *         session
110          */
111         protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
112                 try {
113                         Session session = webInterface.getSessionManager().useSession(toadletContenxt);
114                         if (create && (session == null)) {
115                                 session = webInterface.getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
116                         }
117                         return session;
118                 } catch (freenet.clients.http.RedirectException re1) {
119                         return null;
120                 }
121         }
122
123         /**
124          * Returns the currently logged in Sone.
125          *
126          * @param toadletContext
127          *            The toadlet context
128          * @return The currently logged in Sone, or {@code null} if no Sone is
129          *         currently logged in
130          */
131         protected Sone getCurrentSone(ToadletContext toadletContext) {
132                 Session session = getCurrentSession(toadletContext);
133                 if (session == null) {
134                         return null;
135                 }
136                 String soneId = (String) session.getAttribute("Sone.CurrentSone");
137                 if (soneId == null) {
138                         return null;
139                 }
140                 for (Sone sone : webInterface.getCore().getSones()) {
141                         if (sone.getId().equals(soneId)) {
142                                 return sone;
143                         }
144                 }
145                 return null;
146         }
147
148         /**
149          * Sets the currently logged in Sone.
150          *
151          * @param toadletContext
152          *            The toadlet context
153          * @param sone
154          *            The Sone to set as currently logged in
155          */
156         protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
157                 Session session = getCurrentSession(toadletContext);
158                 if (sone == null) {
159                         session.removeAttribute("Sone.CurrentSone");
160                 } else {
161                         session.setAttribute("Sone.CurrentSone", sone.getId());
162                 }
163         }
164
165         //
166         // TEMPLATEPAGE METHODS
167         //
168
169         /**
170          * {@inheritDoc}
171          */
172         @Override
173         protected Collection<String> getStyleSheets() {
174                 return Arrays.asList("css/sone.css");
175         }
176
177         /**
178          * {@inheritDoc}
179          */
180         @Override
181         protected String getShortcutIcon() {
182                 return "images/icon.png";
183         }
184
185         /**
186          * Returns whether this page requires the user to log in.
187          *
188          * @return {@code true} if the user is required to be logged in to use this
189          *         page, {@code false} otherwise
190          */
191         protected boolean requiresLogin() {
192                 return requireLogin;
193         }
194
195         /**
196          * {@inheritDoc}
197          */
198         @Override
199         protected void processTemplate(Request request, Template template) throws RedirectException {
200                 super.processTemplate(request, template);
201                 template.set("currentSone", getCurrentSone(request.getToadletContext()));
202                 template.set("request", request);
203         }
204
205         /**
206          * {@inheritDoc}
207          */
208         @Override
209         protected String getRedirectTarget(Page.Request request) {
210                 if (requiresLogin() && (getCurrentSone(request.getToadletContext()) == null)) {
211                         return "login.html";
212                 }
213                 return null;
214         }
215
216         /**
217          * {@inheritDoc}
218          */
219         @Override
220         public boolean isEnabled(ToadletContext toadletContext) {
221                 if (requiresLogin()) {
222                         return getCurrentSone(toadletContext) != null;
223                 }
224                 return true;
225         }
226
227 }