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 net.pterodactylus.util.template.TemplateContext;
30 import net.pterodactylus.util.template.TemplateContextFactory;
31 import freenet.clients.http.LinkEnabledCallback;
32 import freenet.clients.http.PageMaker;
33 import freenet.clients.http.PageNode;
34 import freenet.clients.http.ToadletContext;
37 * Base class for all {@link Page}s that are rendered with {@link Template}s.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class TemplatePage implements Page, LinkEnabledCallback {
44 private static final Logger logger = Logging.getLogger(TemplatePage.class);
46 /** The path of the page. */
47 private final String path;
49 /** The template context factory. */
50 private final TemplateContextFactory templateContextFactory;
52 /** The template to render. */
53 private final Template template;
55 /** Where to redirect for invalid form passwords. */
56 private final String invalidFormPasswordRedirectTarget;
59 * Creates a new template page.
62 * The path of the page
63 * @param templateContextFactory
64 * The template context factory
66 * The template to render
67 * @param invalidFormPasswordRedirectTarget
68 * The target to redirect to if a POST request does not contain
69 * the correct form password
71 public TemplatePage(String path, TemplateContextFactory templateContextFactory, Template template, String invalidFormPasswordRedirectTarget) {
73 this.templateContextFactory = templateContextFactory;
74 this.template = template;
75 this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget;
82 public String getPath() {
87 * Returns the title of the page.
90 * The request to serve
91 * @return The title of the page
93 protected String getPageTitle(Request request) {
101 public Response handleRequest(Request request) {
102 String redirectTarget = getRedirectTarget(request);
103 if (redirectTarget != null) {
104 return new RedirectResponse(redirectTarget);
107 ToadletContext toadletContext = request.getToadletContext();
108 if (request.getMethod() == Method.POST) {
109 /* require form password. */
110 String formPassword = request.getHttpRequest().getPartAsStringFailsafe("formPassword", 32);
111 if (!formPassword.equals(toadletContext.getContainer().getFormPassword())) {
112 return new RedirectResponse(invalidFormPasswordRedirectTarget);
115 PageMaker pageMaker = toadletContext.getPageMaker();
116 PageNode pageNode = pageMaker.getPageNode(getPageTitle(request), toadletContext);
117 for (String styleSheet : getStyleSheets()) {
118 pageNode.addCustomStyleSheet(styleSheet);
120 String shortcutIcon = getShortcutIcon();
121 if (shortcutIcon != null) {
122 pageNode.addForwardLink("icon", shortcutIcon);
125 TemplateContext templateContext = templateContextFactory.createTemplateContext();
126 templateContext.mergeContext(template.getInitialContext());
128 long start = System.nanoTime();
129 processTemplate(request, templateContext);
130 long finish = System.nanoTime();
131 logger.log(Level.FINEST, "Template was rendered in " + ((finish - start) / 1000) / 1000.0 + "ms.");
132 } catch (RedirectException re1) {
133 return new RedirectResponse(re1.getTarget());
136 StringWriter stringWriter = new StringWriter();
137 template.render(templateContext, stringWriter);
138 pageNode.content.addChild("%", stringWriter.toString());
140 postProcess(request, templateContext);
142 return new Response(200, "OK", "text/html", pageNode.outer.generate());
146 * Can be overridden to return a custom set of style sheets that are to be
147 * included in the page’s header.
149 * @return Additional style sheets to load
151 protected Collection<String> getStyleSheets() {
152 return Collections.emptySet();
156 * Returns the name of the shortcut icon to include in the page’s header.
158 * @return The URL of the shortcut icon, or {@code null} for no icon
160 protected String getShortcutIcon() {
165 * Can be overridden when extending classes need to set variables in the
166 * template before it is rendered.
169 * The request that is rendered
170 * @param templateContext
171 * The template context to set variables in
172 * @throws RedirectException
173 * if the processing page wants to redirect after processing
175 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
180 * This method will be called after
181 * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
182 * has processed the template and the template was rendered. This method
183 * will not be called if
184 * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
185 * throws a {@link RedirectException}!
188 * The request being processed
189 * @param templateContext
190 * The template context that supplied the rendered data
192 protected void postProcess(Request request, TemplateContext templateContext) {
197 * Can be overridden to redirect the user to a different page, in case a log
198 * in is required, or something else is wrong.
201 * The request that is processed
202 * @return The URL to redirect to, or {@code null} to not redirect
204 protected String getRedirectTarget(Page.Request request) {
209 // INTERFACE LinkEnabledCallback
216 public boolean isEnabled(ToadletContext toadletContext) {
221 * Exception that can be thrown to signal that a subclassed {@link Page}
222 * wants to redirect the user during the
223 * {@link TemplatePage#processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
226 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
228 public static class RedirectException extends Exception {
230 /** The target to redirect to. */
231 private final String target;
234 * Creates a new redirect exception.
237 * The target of the redirect
239 public RedirectException(String target) {
240 this.target = target;
244 * Returns the target to redirect to.
246 * @return The target to redirect to
248 public String getTarget() {