Add method to set the current Sone.
[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.RedirectException;
29 import freenet.clients.http.SessionManager.Session;
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 request
68          *            The request to extract the session information from
69          * @return The current session, or {@code null} if there is no current
70          *         session
71          */
72         protected Session getCurrentSession(Request request) {
73                 return getCurrentSession(request, 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 request
81          *            The request to extract the session information from
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(Request request, boolean create) {
89                 try {
90                         Session session = webInterface.sessionManager().useSession(request.getToadletContext());
91                         if (create && (session == null)) {
92                                 session = webInterface.sessionManager().createSession(UUID.randomUUID().toString(), request.getToadletContext());
93                         }
94                         return session;
95                 } catch (RedirectException re1) {
96                         return null;
97                 }
98         }
99
100         /**
101          * Returns the currently logged in Sone.
102          *
103          * @param request
104          *            The request to extract the session information from
105          * @return The currently logged in Sone, or {@code null} if no Sone is
106          *         currently logged in
107          */
108         protected Sone getCurrentSone(Request request) {
109                 Session session = getCurrentSession(request);
110                 if (session == null) {
111                         return null;
112                 }
113                 return (Sone) session.getAttribute("Sone.CurrentSone");
114         }
115
116         /**
117          * Sets the currently logged in Sone.
118          *
119          * @param request
120          *            The request
121          * @param sone
122          *            The Sone to set as currently logged in
123          */
124         protected void setCurrentSone(Request request, Sone sone) {
125                 Session session = getCurrentSession(request);
126                 session.setAttribute("Sone.CurrentSone", sone);
127         }
128
129         //
130         // TEMPLATEPAGE METHODS
131         //
132
133         /**
134          * {@inheritDoc}
135          */
136         @Override
137         protected Collection<String> getStyleSheets() {
138                 return Arrays.asList("css/sone.css");
139         }
140
141         /**
142          * Returns whether this page requires the user to log in.
143          *
144          * @return {@code true} if the user is required to be logged in to use this
145          *         page, {@code false} otherwise
146          */
147         protected boolean requiresLogin() {
148                 return false;
149         }
150
151         /**
152          * {@inheritDoc}
153          */
154         @Override
155         protected String getRedirectTarget(Page.Request request) {
156                 if (requiresLogin() && (getCurrentSone(request) == null)) {
157                         return "login.html";
158                 }
159                 return null;
160         }
161
162 }