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.LinkEnabledCallback;
26 import freenet.clients.http.PageMaker;
27 import freenet.clients.http.PageNode;
28 import freenet.clients.http.ToadletContext;
29 import freenet.l10n.BaseL10n;
32 * Base class for all {@link Page}s that are rendered with {@link Template}s.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class TemplatePage implements Page, LinkEnabledCallback {
38 /** The path of the page. */
39 private final String path;
41 /** The template to render. */
42 private final Template template;
44 /** The L10n handler. */
45 private final BaseL10n l10n;
47 /** The l10n key for the page title. */
48 private final String pageTitleKey;
51 * Creates a new template page.
54 * The path of the page
56 * The template to render
60 * The l10n key of the title page
62 public TemplatePage(String path, Template template, BaseL10n l10n, String pageTitleKey) {
64 this.template = template;
66 this.pageTitleKey = pageTitleKey;
73 public String getPath() {
81 public Response handleRequest(Request request) {
82 String redirectTarget = getRedirectTarget(request);
83 if (redirectTarget != null) {
84 return new RedirectResponse(redirectTarget);
87 ToadletContext toadletContext = request.getToadletContext();
88 PageMaker pageMaker = toadletContext.getPageMaker();
89 PageNode pageNode = pageMaker.getPageNode(l10n.getString(pageTitleKey), toadletContext);
90 for (String styleSheet : getStyleSheets()) {
91 pageNode.addCustomStyleSheet(styleSheet);
95 processTemplate(request, template);
96 } catch (RedirectException re1) {
97 return new RedirectResponse(re1.getTarget());
100 StringWriter stringWriter = new StringWriter();
101 template.render(stringWriter);
102 pageNode.content.addChild("%", stringWriter.toString());
104 return new Response(200, "OK", "text/html", pageNode.outer.generate());
108 * Can be overridden to return a custom set of style sheets that are to be
109 * included in the page’s header.
111 * @return Additional style sheets to load
113 protected Collection<String> getStyleSheets() {
114 return Collections.emptySet();
118 * Can be overridden when extending classes need to set variables in the
119 * template before it is rendered.
122 * The request that is rendered
124 * The template to set variables in
125 * @throws RedirectException
126 * if the processing page wants to redirect after processing
128 protected void processTemplate(Request request, Template template) throws RedirectException {
133 * Can be overridden to redirect the user to a different page, in case a log
134 * in is required, or something else is wrong.
137 * The request that is processed
138 * @return The URL to redirect to, or {@code null} to not redirect
140 protected String getRedirectTarget(Page.Request request) {
145 // INTERFACE LinkEnabledCallback
152 public boolean isEnabled(ToadletContext toadletContext) {
157 * Exception that can be thrown to signal that a subclassed {@link Page}
158 * wants to redirect the user during the
159 * {@link TemplatePage#processTemplate(net.pterodactylus.sone.web.page.Page.Request, Template)}
162 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
164 public class RedirectException extends Exception {
166 /** The target to redirect to. */
167 private final String target;
170 * Creates a new redirect exception.
173 * The target of the redirect
175 public RedirectException(String target) {
176 this.target = target;
180 * Returns the target to redirect to.
182 * @return The target to redirect to
184 public String getTarget() {