2 * Sone - SoneTemplatePage.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.List;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.main.SonePlugin;
29 import net.pterodactylus.sone.web.page.FreenetTemplatePage;
30 import net.pterodactylus.sone.web.page.Page;
31 import net.pterodactylus.util.collection.ListBuilder;
32 import net.pterodactylus.util.collection.MapBuilder;
33 import net.pterodactylus.util.template.Template;
34 import net.pterodactylus.util.template.TemplateContext;
35 import freenet.clients.http.SessionManager.Session;
36 import freenet.clients.http.ToadletContext;
37 import freenet.support.api.HTTPRequest;
40 * Base page for the Sone web interface.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class SoneTemplatePage extends FreenetTemplatePage {
47 protected final WebInterface webInterface;
49 /** The page title l10n key. */
50 private final String pageTitleKey;
52 /** Whether to require a login. */
53 private final boolean requireLogin;
56 * Creates a new template page for Sone that does not require the user to be
60 * The path of the page
62 * The template to render
64 * The Sone web interface
66 public SoneTemplatePage(String path, Template template, WebInterface webInterface) {
67 this(path, template, null, webInterface, false);
71 * Creates a new template page for Sone that does not require the user to be
75 * The path of the page
77 * The template to render
79 * The l10n key of the page title
81 * The Sone web interface
83 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface) {
84 this(path, template, pageTitleKey, webInterface, false);
88 * Creates a new template page for Sone.
91 * The path of the page
93 * The template to render
95 * The Sone web interface
97 * Whether this page requires a login
99 public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) {
100 this(path, template, null, webInterface, requireLogin);
104 * Creates a new template page for Sone.
107 * The path of the page
109 * The template to render
110 * @param pageTitleKey
111 * The l10n key of the page title
112 * @param webInterface
113 * The Sone web interface
114 * @param requireLogin
115 * Whether this page requires a login
117 public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) {
118 super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html");
119 this.pageTitleKey = pageTitleKey;
120 this.webInterface = webInterface;
121 this.requireLogin = requireLogin;
122 template.getInitialContext().set("webInterface", webInterface);
130 * Returns the current session, creating a new session if there is no
133 * @param toadletContenxt
134 * The toadlet context
135 * @return The current session, or {@code null} if there is no current
138 protected Session getCurrentSession(ToadletContext toadletContenxt) {
139 return webInterface.getCurrentSession(toadletContenxt);
143 * Returns the current session, creating a new session if there is no
144 * current session and {@code create} is {@code true}.
146 * @param toadletContenxt
147 * The toadlet context
149 * {@code true} to create a new session if there is no current
150 * session, {@code false} otherwise
151 * @return The current session, or {@code null} if there is no current
154 protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
155 return webInterface.getCurrentSession(toadletContenxt, create);
159 * Returns the currently logged in Sone.
161 * @param toadletContext
162 * The toadlet context
163 * @return The currently logged in Sone, or {@code null} if no Sone is
164 * currently logged in
166 protected Sone getCurrentSone(ToadletContext toadletContext) {
167 return webInterface.getCurrentSone(toadletContext);
171 * Returns the currently logged in Sone.
173 * @param toadletContext
174 * The toadlet context
176 * {@code true} to create a new session if no session exists,
177 * {@code false} to not create a new session
178 * @return The currently logged in Sone, or {@code null} if no Sone is
179 * currently logged in
181 protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
182 return webInterface.getCurrentSone(toadletContext, create);
186 * Sets the currently logged in Sone.
188 * @param toadletContext
189 * The toadlet context
191 * The Sone to set as currently logged in
193 protected void setCurrentSone(ToadletContext toadletContext, Sone sone) {
194 webInterface.setCurrentSone(toadletContext, sone);
198 // TEMPLATEPAGE METHODS
205 protected String getPageTitle(Request request) {
206 if (pageTitleKey != null) {
207 return webInterface.getL10n().getString(pageTitleKey);
216 protected List<Map<String, String>> getAdditionalLinkNodes(Request request) {
217 return new ListBuilder<Map<String, String>>().add(new MapBuilder<String, String>().put("rel", "search").put("type", "application/opensearchdescription+xml").put("title", "Sone").put("href", "http://" + request.getHttpRequest().getHeader("host") + "/Sone/OpenSearch.xml").get()).get();
224 protected Collection<String> getStyleSheets() {
225 return Arrays.asList("css/sone.css");
232 protected String getShortcutIcon() {
233 return "images/icon.png";
237 * Returns whether this page requires the user to log in.
239 * @return {@code true} if the user is required to be logged in to use this
240 * page, {@code false} otherwise
242 protected boolean requiresLogin() {
250 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
251 super.processTemplate(request, templateContext);
252 templateContext.set("currentSone", getCurrentSone(request.getToadletContext(), false));
253 templateContext.set("localSones", webInterface.getCore().getLocalSones());
254 templateContext.set("request", request);
255 templateContext.set("currentVersion", SonePlugin.VERSION);
256 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
257 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
258 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
259 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
266 protected String getRedirectTarget(Page.Request request) {
267 if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) {
268 HTTPRequest httpRequest = request.getHttpRequest();
269 String originalUrl = httpRequest.getPath();
270 if (httpRequest.hasParameters()) {
271 StringBuilder requestParameters = new StringBuilder();
272 for (String parameterName : httpRequest.getParameterNames()) {
273 if (requestParameters.length() > 0) {
274 requestParameters.append("%26");
276 String[] parameterValues = httpRequest.getMultipleParam(parameterName);
277 for (String parameterValue : parameterValues) {
279 requestParameters.append(URLEncoder.encode(parameterName, "UTF-8")).append("%3d").append(URLEncoder.encode(parameterValue, "UTF-8"));
280 } catch (UnsupportedEncodingException uee1) {
281 /* A JVM without UTF-8? I don’t think so. */
285 originalUrl += "?" + requestParameters.toString();
287 return "login.html?target=" + originalUrl;
296 public boolean isEnabled(ToadletContext toadletContext) {
297 if (requiresLogin()) {
298 return getCurrentSone(toadletContext, false) != null;