2 * shortener - TemplatePage.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.page;
20 import java.io.StringWriter;
21 import java.util.Collection;
22 import java.util.Collections;
24 import net.pterodactylus.util.template.Template;
25 import freenet.clients.http.PageMaker;
26 import freenet.clients.http.PageNode;
27 import freenet.clients.http.ToadletContext;
28 import freenet.l10n.BaseL10n;
31 * Base class for all {@link Page}s that are rendered with {@link Template}s.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class TemplatePage implements Page {
37 /** The path of the page. */
38 private final String path;
40 /** The template to render. */
41 private final Template template;
43 /** The L10n handler. */
44 private final BaseL10n l10n;
46 /** The l10n key for the page title. */
47 private final String pageTitleKey;
50 * Creates a new template page.
53 * The path of the page
55 * The template to render
59 * The l10n key of the title page
61 public TemplatePage(String path, Template template, BaseL10n l10n, String pageTitleKey) {
63 this.template = template;
65 this.pageTitleKey = pageTitleKey;
72 public String getPath() {
80 public Response handleRequest(Request request) {
81 String redirectTarget = getRedirectTarget(request);
82 if (redirectTarget != null) {
83 return new RedirectResponse(redirectTarget);
86 ToadletContext toadletContext = request.getToadletContext();
87 PageMaker pageMaker = toadletContext.getPageMaker();
88 PageNode pageNode = pageMaker.getPageNode(l10n.getString(pageTitleKey), toadletContext);
89 for (String styleSheet : getStyleSheets()) {
90 pageNode.addCustomStyleSheet(styleSheet);
93 processTemplate(request, template);
94 StringWriter stringWriter = new StringWriter();
95 template.render(stringWriter);
96 pageNode.content.addChild("%", stringWriter.toString());
98 return new Response(200, "OK", "text/html", pageNode.outer.generate());
102 * Can be overridden to return a custom set of style sheets that are to be
103 * included in the page’s header.
105 * @return Additional style sheets to load
107 protected Collection<String> getStyleSheets() {
108 return Collections.emptySet();
112 * Can be overridden when extending classes need to set variables in the
113 * template before it is rendered.
116 * The request that is rendered
118 * The template to set variables in
120 protected void processTemplate(Request request, Template template) {
125 * Can be overridden to redirect the user to a different page, in case a log
126 * in is required, or something else is wrong.
129 * The request that is processed
130 * @return The URL to redirect to, or {@code null} to not redirect
132 protected String getRedirectTarget(Page.Request request) {