2 * Sone - FreenetTemplatePage.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 if (isFullAccessOnly() && !request.getToadletContext().isAllowedFullAccess()) {
113 return new Response(401, "Not authorized", "text/html", "Not authorized");
115 ToadletContext toadletContext = request.getToadletContext();
116 if (request.getMethod() == Method.POST) {
117 /* require form password. */
118 String formPassword = request.getHttpRequest().getPartAsStringFailsafe("formPassword", 32);
119 if (!formPassword.equals(toadletContext.getContainer().getFormPassword())) {
120 return new RedirectResponse(invalidFormPasswordRedirectTarget);
123 PageMaker pageMaker = toadletContext.getPageMaker();
124 PageNode pageNode = pageMaker.getPageNode(getPageTitle(request), toadletContext);
125 for (String styleSheet : getStyleSheets()) {
126 pageNode.addCustomStyleSheet(styleSheet);
128 for (Map<String, String> linkNodeParameters : getAdditionalLinkNodes(request)) {
129 HTMLNode linkNode = pageNode.headNode.addChild("link");
130 for (Entry<String, String> parameter : linkNodeParameters.entrySet()) {
131 linkNode.addAttribute(parameter.getKey(), parameter.getValue());
134 String shortcutIcon = getShortcutIcon();
135 if (shortcutIcon != null) {
136 pageNode.addForwardLink("icon", shortcutIcon);
139 TemplateContext templateContext = templateContextFactory.createTemplateContext();
140 templateContext.mergeContext(template.getInitialContext());
142 long start = System.nanoTime();
143 processTemplate(request, templateContext);
144 long finish = System.nanoTime();
145 logger.log(Level.FINEST, "Template was rendered in " + ((finish - start) / 1000) / 1000.0 + "ms.");
146 } catch (RedirectException re1) {
147 return new RedirectResponse(re1.getTarget());
150 StringWriter stringWriter = new StringWriter();
151 template.render(templateContext, stringWriter);
152 pageNode.content.addChild("%", stringWriter.toString());
154 postProcess(request, templateContext);
156 return new Response(200, "OK", "text/html", pageNode.outer.generate());
160 * Can be overridden to return a custom set of style sheets that are to be
161 * included in the page’s header.
163 * @return Additional style sheets to load
165 protected Collection<String> getStyleSheets() {
166 return Collections.emptySet();
170 * Returns the name of the shortcut icon to include in the page’s header.
172 * @return The URL of the shortcut icon, or {@code null} for no icon
174 protected String getShortcutIcon() {
179 * Can be overridden when extending classes need to set variables in the
180 * template before it is rendered.
183 * The request that is rendered
184 * @param templateContext
185 * The template context to set variables in
186 * @throws RedirectException
187 * if the processing page wants to redirect after processing
189 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
194 * This method will be called after
195 * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
196 * has processed the template and the template was rendered. This method
197 * will not be called if
198 * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
199 * throws a {@link RedirectException}!
202 * The request being processed
203 * @param templateContext
204 * The template context that supplied the rendered data
206 protected void postProcess(Request request, TemplateContext templateContext) {
211 * Can be overridden to redirect the user to a different page, in case a log
212 * in is required, or something else is wrong.
215 * The request that is processed
216 * @return The URL to redirect to, or {@code null} to not redirect
218 protected String getRedirectTarget(Page.Request request) {
223 * Returns additional <link> nodes for the HTML’s <head> node.
226 * The request for which to return the link nodes
227 * @return All link nodes that should be added to the HTML head
229 protected List<Map<String, String>> getAdditionalLinkNodes(Request request) {
230 return Collections.emptyList();
234 * Returns whether this page should only be allowed for requests from hosts
237 * @return {@code true} if this page should only be allowed for hosts with
238 * full access, {@code false} to allow this page for any host
240 protected boolean isFullAccessOnly() {
245 // INTERFACE LinkEnabledCallback
252 public boolean isEnabled(ToadletContext toadletContext) {
253 return !isFullAccessOnly();
257 * Exception that can be thrown to signal that a subclassed {@link Page}
258 * wants to redirect the user during the
259 * {@link FreenetTemplatePage#processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
262 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
264 public static class RedirectException extends Exception {
266 /** The target to redirect to. */
267 private final String target;
270 * Creates a new redirect exception.
273 * The target of the redirect
275 public RedirectException(String target) {
276 this.target = target;
280 * Returns the target to redirect to.
282 * @return The target to redirect to
284 public String getTarget() {