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;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
26 import net.pterodactylus.sone.web.page.Page.Request.Method;
27 import net.pterodactylus.util.logging.Logging;
28 import net.pterodactylus.util.template.Template;
29 import freenet.clients.http.LinkEnabledCallback;
30 import freenet.clients.http.PageMaker;
31 import freenet.clients.http.PageNode;
32 import freenet.clients.http.ToadletContext;
33 import freenet.l10n.BaseL10n;
36 * Base class for all {@link Page}s that are rendered with {@link Template}s.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class TemplatePage implements Page, LinkEnabledCallback {
43 private static final Logger logger = Logging.getLogger(TemplatePage.class);
45 /** The path of the page. */
46 private final String path;
48 /** The template to render. */
49 protected final Template template;
51 /** The L10n handler. */
52 private final BaseL10n l10n;
54 /** The l10n key for the page title. */
55 private final String pageTitleKey;
57 /** Where to redirect for invalid form passwords. */
58 private final String invalidFormPasswordRedirectTarget;
61 * Creates a new template page.
64 * The path of the page
66 * The template to render
70 * The l10n key of the title page
71 * @param invalidFormPasswordRedirectTarget
72 * The target to redirect to if a POST request does not contain
73 * the correct form password
75 public TemplatePage(String path, Template template, BaseL10n l10n, String pageTitleKey, String invalidFormPasswordRedirectTarget) {
77 this.template = template;
79 this.pageTitleKey = pageTitleKey;
80 this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget;
87 public String getPath() {
95 public Response handleRequest(Request request) {
96 String redirectTarget = getRedirectTarget(request);
97 if (redirectTarget != null) {
98 return new RedirectResponse(redirectTarget);
101 ToadletContext toadletContext = request.getToadletContext();
102 if (request.getMethod() == Method.POST) {
103 /* require form password. */
104 String formPassword = request.getHttpRequest().getPartAsStringFailsafe("formPassword", 32);
105 if (!formPassword.equals(toadletContext.getContainer().getFormPassword())) {
106 return new RedirectResponse(invalidFormPasswordRedirectTarget);
109 PageMaker pageMaker = toadletContext.getPageMaker();
110 PageNode pageNode = pageMaker.getPageNode(l10n.getString(pageTitleKey), toadletContext);
111 for (String styleSheet : getStyleSheets()) {
112 pageNode.addCustomStyleSheet(styleSheet);
114 String shortcutIcon = getShortcutIcon();
115 if (shortcutIcon != null) {
116 pageNode.addForwardLink("icon", shortcutIcon);
120 long start = System.nanoTime();
121 processTemplate(request, template);
122 long finish = System.nanoTime();
123 logger.log(Level.FINEST, "Template was rendered in " + ((finish - start) / 1000) / 1000.0 + "ms.");
124 } catch (RedirectException re1) {
125 return new RedirectResponse(re1.getTarget());
128 StringWriter stringWriter = new StringWriter();
129 template.render(stringWriter);
130 pageNode.content.addChild("%", stringWriter.toString());
132 postProcess(request, template);
134 return new Response(200, "OK", "text/html", pageNode.outer.generate());
138 * Can be overridden to return a custom set of style sheets that are to be
139 * included in the page’s header.
141 * @return Additional style sheets to load
143 protected Collection<String> getStyleSheets() {
144 return Collections.emptySet();
148 * Returns the name of the shortcut icon to include in the page’s header.
150 * @return The URL of the shortcut icon, or {@code null} for no icon
152 protected String getShortcutIcon() {
157 * Can be overridden when extending classes need to set variables in the
158 * template before it is rendered.
161 * The request that is rendered
163 * The template to set variables in
164 * @throws RedirectException
165 * if the processing page wants to redirect after processing
167 protected void processTemplate(Request request, Template template) throws RedirectException {
172 * This method will be called after
173 * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, Template)}
174 * has processed the template and the template was rendered. This method
175 * will not be called if
176 * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, Template)}
177 * throws a {@link RedirectException}!
180 * The request being processed
182 * The template that was rendered
184 protected void postProcess(Request request, Template template) {
189 * Can be overridden to redirect the user to a different page, in case a log
190 * in is required, or something else is wrong.
193 * The request that is processed
194 * @return The URL to redirect to, or {@code null} to not redirect
196 protected String getRedirectTarget(Page.Request request) {
201 // INTERFACE LinkEnabledCallback
208 public boolean isEnabled(ToadletContext toadletContext) {
213 * Exception that can be thrown to signal that a subclassed {@link Page}
214 * wants to redirect the user during the
215 * {@link TemplatePage#processTemplate(net.pterodactylus.sone.web.page.Page.Request, Template)}
218 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
220 public class RedirectException extends Exception {
222 /** The target to redirect to. */
223 private final String target;
226 * Creates a new redirect exception.
229 * The target of the redirect
231 public RedirectException(String target) {
232 this.target = target;
236 * Returns the target to redirect to.
238 * @return The target to redirect to
240 public String getTarget() {