Merge branch 'new-web-of-trust' into next
[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.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.util.Arrays;
23 import java.util.Collection;
24
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.main.SonePlugin;
27 import net.pterodactylus.sone.web.page.Page;
28 import net.pterodactylus.sone.web.page.TemplatePage;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContext;
31 import freenet.clients.http.SessionManager.Session;
32 import freenet.clients.http.ToadletContext;
33 import freenet.support.api.HTTPRequest;
34
35 /**
36  * Base page for the Freetalk web interface.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class SoneTemplatePage extends TemplatePage {
41
42         /** The Sone core. */
43         protected final WebInterface webInterface;
44
45         /** Whether to require a login. */
46         private final boolean requireLogin;
47
48         /**
49          * Creates a new template page for Freetalk that does not require the user
50          * to be logged in.
51          *
52          * @param path
53          *            The path of the page
54          * @param template
55          *            The template to render
56          * @param pageTitleKey
57          *            The l10n key of the page title
58          * @param webInterface
59          *            The Sone web interface
60          */
61         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
62                 this(path, template, pageTitleKey, webInterface, false);
63         }
64
65         /**
66          * Creates a new template page for Freetalk.
67          *
68          * @param path
69          *            The path of the page
70          * @param template
71          *            The template to render
72          * @param pageTitleKey
73          *            The l10n key of the page title
74          * @param webInterface
75          *            The Sone web interface
76          * @param requireLogin
77          *            Whether this page requires a login
78          */
79         public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
80                 super(path, webInterface.getTemplateContextFactory(), template, webInterface.getL10n(), pageTitleKey, "noPermission.html");
81                 this.webInterface = webInterface;
82                 this.requireLogin = requireLogin;
83                 template.getInitialContext().set("webInterface", webInterface);
84         }
85
86         //
87         // PROTECTED METHODS
88         //
89
90         /**
91          * Returns the current session, creating a new session if there is no
92          * current session.
93          *
94          * @param toadletContenxt
95          *            The toadlet context
96          * @return The current session, or {@code null} if there is no current
97          *         session
98          */
99         protected Session getCurrentSession(ToadletContext toadletContenxt) {
100                 return webInterface.getCurrentSession(toadletContenxt);
101         }
102
103         /**
104          * Returns the current session, creating a new session if there is no
105          * current session and {@code create} is {@code true}.
106          *
107          * @param toadletContenxt
108          *            The toadlet context
109          * @param create
110          *            {@code true} to create a new session if there is no current
111          *            session, {@code false} otherwise
112          * @return The current session, or {@code null} if there is no current
113          *         session
114          */
115         protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
116                 return webInterface.getCurrentSession(toadletContenxt, create);
117         }
118
119         /**
120          * Returns the currently logged in Sone.
121          *
122          * @param toadletContext
123          *            The toadlet context
124          * @return The currently logged in Sone, or {@code null} if no Sone is
125          *         currently logged in
126          */
127         protected Sone getCurrentSone(ToadletContext toadletContext) {
128                 return webInterface.getCurrentSone(toadletContext);
129         }
130
131         /**
132          * Returns the currently logged in Sone.
133          *
134          * @param toadletContext
135          *            The toadlet context
136          * @param create
137          *            {@code true} to create a new session if no session exists,
138          *            {@code false} to not create a new session
139          * @return The currently logged in Sone, or {@code null} if no Sone is
140          *         currently logged in
141          */
142         protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
143                 return webInterface.getCurrentSone(toadletContext, create);
144         }
145
146         /**
147          * Sets the currently logged in Sone.
148          *
149          * @param toadletContext
150          *            The toadlet context
151          * @param sone
152          *            The Sone to set as currently logged in
153          */
154         protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
155                 webInterface.setCurrentSone(toadletContext, sone);
156         }
157
158         //
159         // TEMPLATEPAGE METHODS
160         //
161
162         /**
163          * {@inheritDoc}
164          */
165         @Override
166         protected Collection<String> getStyleSheets() {
167                 return Arrays.asList("css/sone.css");
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         @Override
174         protected String getShortcutIcon() {
175                 return "images/icon.png";
176         }
177
178         /**
179          * Returns whether this page requires the user to log in.
180          *
181          * @return {@code true} if the user is required to be logged in to use this
182          *         page, {@code false} otherwise
183          */
184         protected boolean requiresLogin() {
185                 return requireLogin;
186         }
187
188         /**
189          * {@inheritDoc}
190          */
191         @Override
192         protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
193                 super.processTemplate(request, templateContext);
194                 templateContext.set("currentSone", getCurrentSone(request.getToadletContext(), false));
195                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
196                 templateContext.set("request", request);
197                 templateContext.set("currentVersion", SonePlugin.VERSION);
198                 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
199                 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
200                 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
201                 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
202         }
203
204         /**
205          * {@inheritDoc}
206          */
207         @Override
208         protected String getRedirectTarget(Page.Request request) {
209                 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
210                         HTTPRequest httpRequest = request.getHttpRequest();
211                         String originalUrl = httpRequest.getPath();
212                         if (httpRequest.hasParameters()) {
213                                 StringBuilder requestParameters = new StringBuilder();
214                                 for (String parameterName : httpRequest.getParameterNames()) {
215                                         if (requestParameters.length() > 0) {
216                                                 requestParameters.append("%26");
217                                         }
218                                         String[] parameterValues = httpRequest.getMultipleParam(parameterName);
219                                         for (String parameterValue : parameterValues) {
220                                                 try {
221                                                         requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
222                                                 } catch (UnsupportedEncodingException uee1) {
223                                                         /* A JVM without UTF-8? I don’t think so. */
224                                                 }
225                                         }
226                                 }
227                                 originalUrl += "?" + requestParameters.toString();
228                         }
229                         return "login.html?target=" + originalUrl;
230                 }
231                 return null;
232         }
233
234         /**
235          * {@inheritDoc}
236          */
237         @Override
238         public boolean isEnabled(ToadletContext toadletContext) {
239                 if (requiresLogin()) {
240                         return getCurrentSone(toadletContext, false) != null;
241                 }
242                 return true;
243         }
244
245 }