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.List;
25 import java.util.Map.Entry;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import net.pterodactylus.sone.web.page.Page.Request.Method;
30 import net.pterodactylus.util.logging.Logging;
31 import net.pterodactylus.util.template.Template;
32 import net.pterodactylus.util.template.TemplateContext;
33 import net.pterodactylus.util.template.TemplateContextFactory;
34 import freenet.clients.http.LinkEnabledCallback;
35 import freenet.clients.http.PageMaker;
36 import freenet.clients.http.PageNode;
37 import freenet.clients.http.ToadletContext;
38 import freenet.support.HTMLNode;
41 * Base class for all {@link Page}s that are rendered with {@link Template}s and
42 * fit into Freenet’s web interface.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class FreenetTemplatePage implements Page, LinkEnabledCallback {
49 private static final Logger logger = Logging.getLogger(FreenetTemplatePage.class);
51 /** The path of the page. */
52 private final String path;
54 /** The template context factory. */
55 private final TemplateContextFactory templateContextFactory;
57 /** The template to render. */
58 private final Template template;
60 /** Where to redirect for invalid form passwords. */
61 private final String invalidFormPasswordRedirectTarget;
64 * Creates a new template page.
67 * The path of the page
68 * @param templateContextFactory
69 * The template context factory
71 * The template to render
72 * @param invalidFormPasswordRedirectTarget
73 * The target to redirect to if a POST request does not contain
74 * the correct form password
76 public FreenetTemplatePage(String path, TemplateContextFactory templateContextFactory, Template template, String invalidFormPasswordRedirectTarget) {
78 this.templateContextFactory = templateContextFactory;
79 this.template = template;
80 this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget;
87 public String getPath() {
92 * Returns the title of the page.
95 * The request to serve
96 * @return The title of the page
98 protected String getPageTitle(Request request) {
106 public Response handleRequest(Request request) {
107 String redirectTarget = getRedirectTarget(request);
108 if (redirectTarget != null) {
109 return new RedirectResponse(redirectTarget);
112 ToadletContext toadletContext = request.getToadletContext();
113 if (request.getMethod() == Method.POST) {
114 /* require form password. */
115 String formPassword = request.getHttpRequest().getPartAsStringFailsafe("formPassword", 32);
116 if (!formPassword.equals(toadletContext.getContainer().getFormPassword())) {
117 return new RedirectResponse(invalidFormPasswordRedirectTarget);
120 PageMaker pageMaker = toadletContext.getPageMaker();
121 PageNode pageNode = pageMaker.getPageNode(getPageTitle(request), toadletContext);
122 for (String styleSheet : getStyleSheets()) {
123 pageNode.addCustomStyleSheet(styleSheet);
125 for (Map<String, String> linkNodeParameters : getAdditionalLinkNodes(request)) {
126 HTMLNode linkNode = pageNode.headNode.addChild("link");
127 for (Entry<String, String> parameter : linkNodeParameters.entrySet()) {
128 linkNode.addAttribute(parameter.getKey(), parameter.getValue());
131 String shortcutIcon = getShortcutIcon();
132 if (shortcutIcon != null) {
133 pageNode.addForwardLink("icon", shortcutIcon);
136 TemplateContext templateContext = templateContextFactory.createTemplateContext();
137 templateContext.mergeContext(template.getInitialContext());
139 long start = System.nanoTime();
140 processTemplate(request, templateContext);
141 long finish = System.nanoTime();
142 logger.log(Level.FINEST, "Template was rendered in " + ((finish - start) / 1000) / 1000.0 + "ms.");
143 } catch (RedirectException re1) {
144 return new RedirectResponse(re1.getTarget());
147 StringWriter stringWriter = new StringWriter();
148 template.render(templateContext, stringWriter);
149 pageNode.content.addChild("%", stringWriter.toString());
151 postProcess(request, templateContext);
153 return new Response(200, "OK", "text/html", pageNode.outer.generate());
157 * Can be overridden to return a custom set of style sheets that are to be
158 * included in the page’s header.
160 * @return Additional style sheets to load
162 protected Collection<String> getStyleSheets() {
163 return Collections.emptySet();
167 * Returns the name of the shortcut icon to include in the page’s header.
169 * @return The URL of the shortcut icon, or {@code null} for no icon
171 protected String getShortcutIcon() {
176 * Can be overridden when extending classes need to set variables in the
177 * template before it is rendered.
180 * The request that is rendered
181 * @param templateContext
182 * The template context to set variables in
183 * @throws RedirectException
184 * if the processing page wants to redirect after processing
186 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
191 * This method will be called after
192 * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
193 * has processed the template and the template was rendered. This method
194 * will not be called if
195 * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
196 * throws a {@link RedirectException}!
199 * The request being processed
200 * @param templateContext
201 * The template context that supplied the rendered data
203 protected void postProcess(Request request, TemplateContext templateContext) {
208 * Can be overridden to redirect the user to a different page, in case a log
209 * in is required, or something else is wrong.
212 * The request that is processed
213 * @return The URL to redirect to, or {@code null} to not redirect
215 protected String getRedirectTarget(Page.Request request) {
220 * Returns additional <link> nodes for the HTML’s <head> node.
223 * The request for which to return the link nodes
224 * @return All link nodes that should be added to the HTML head
226 protected List<Map<String, String>> getAdditionalLinkNodes(Request request) {
227 return Collections.emptyList();
231 // INTERFACE LinkEnabledCallback
238 public boolean isEnabled(ToadletContext toadletContext) {
243 * Exception that can be thrown to signal that a subclassed {@link Page}
244 * wants to redirect the user during the
245 * {@link FreenetTemplatePage#processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
248 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
250 public static class RedirectException extends Exception {
252 /** The target to redirect to. */
253 private final String target;
256 * Creates a new redirect exception.
259 * The target of the redirect
261 public RedirectException(String target) {
262 this.target = target;
266 * Returns the target to redirect to.
268 * @return The target to redirect to
270 public String getTarget() {