Add method to get the current session.
[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
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.sone.web.page.Page;
25 import net.pterodactylus.sone.web.page.TemplatePage;
26 import net.pterodactylus.util.template.Template;
27 import freenet.clients.http.RedirectException;
28 import freenet.clients.http.SessionManager.Session;
29
30 /**
31  * Base page for the Freetalk web interface.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class SoneTemplatePage extends TemplatePage {
36
37         /** The Sone core. */
38         protected final WebInterface webInterface;
39
40         /**
41          * Creates a new template page for Freetalk.
42          *
43          * @param path
44          *            The path of the page
45          * @param template
46          *            The template to render
47          * @param pageTitleKey
48          *            The l10n key of the page title
49          * @param webInterface
50          *            The Sone web interface
51          */
52         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
53                 super(path, template, webInterface.l10n(), pageTitleKey);
54                 this.webInterface = webInterface;
55                 template.set("webInterface", webInterface);
56         }
57
58         //
59         // PROTECTED METHODS
60         //
61
62         /**
63          * Returns the current session.
64          *
65          * @param request
66          *            The request to extract the session information from
67          * @return The current session, or {@code null} if there is no current
68          *         session
69          */
70         protected Session getCurrentSession(Request request) {
71                 try {
72                         return webInterface.sessionManager().useSession(request.getToadletContext());
73                 } catch (RedirectException re1) {
74                         return null;
75                 }
76         }
77
78         /**
79          * Returns the currently logged in Sone.
80          *
81          * @param request
82          *            The request to extract the session information from
83          * @return The currently logged in Sone, or {@code null} if no Sone is
84          *         currently logged in
85          */
86         protected Sone getCurrentSone(Request request) {
87                 Session session = getCurrentSession(request);
88                 if (session == null) {
89                         return null;
90                 }
91                 return (Sone) session.getAttribute("Sone.CurrentSone");
92         }
93
94         //
95         // TEMPLATEPAGE METHODS
96         //
97
98         /**
99          * {@inheritDoc}
100          */
101         @Override
102         protected Collection<String> getStyleSheets() {
103                 return Arrays.asList("css/sone.css");
104         }
105
106         /**
107          * Returns whether this page requires the user to log in.
108          *
109          * @return {@code true} if the user is required to be logged in to use this
110          *         page, {@code false} otherwise
111          */
112         protected boolean requiresLogin() {
113                 return false;
114         }
115
116         /**
117          * {@inheritDoc}
118          */
119         @Override
120         protected String getRedirectTarget(Page.Request request) {
121                 if (requiresLogin() && (getCurrentSone(request) == null)) {
122                         return "login.html";
123                 }
124                 return null;
125         }
126
127 }