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.IOException;
21 import java.io.StringWriter;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.util.logging.Logging;
32 import net.pterodactylus.util.template.Template;
33 import net.pterodactylus.util.template.TemplateContext;
34 import net.pterodactylus.util.template.TemplateContextFactory;
35 import net.pterodactylus.util.web.Method;
36 import net.pterodactylus.util.web.Page;
37 import net.pterodactylus.util.web.RedirectResponse;
38 import net.pterodactylus.util.web.Response;
39 import freenet.clients.http.LinkEnabledCallback;
40 import freenet.clients.http.PageMaker;
41 import freenet.clients.http.PageNode;
42 import freenet.clients.http.ToadletContext;
43 import freenet.support.HTMLNode;
46 * Base class for all {@link Page}s that are rendered with {@link Template}s and
47 * fit into Freenet’s web interface.
49 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51 public class FreenetTemplatePage implements FreenetPage, LinkEnabledCallback {
54 private static final Logger logger = Logging.getLogger(FreenetTemplatePage.class);
56 /** The path of the page. */
57 private final String path;
59 /** The template context factory. */
60 private final TemplateContextFactory templateContextFactory;
62 /** The template to render. */
63 private final Template template;
65 /** Where to redirect for invalid form passwords. */
66 private final String invalidFormPasswordRedirectTarget;
69 * Creates a new template page.
72 * The path of the page
73 * @param templateContextFactory
74 * The template context factory
76 * The template to render
77 * @param invalidFormPasswordRedirectTarget
78 * The target to redirect to if a POST request does not contain
79 * the correct form password
81 public FreenetTemplatePage(String path, TemplateContextFactory templateContextFactory, Template template, String invalidFormPasswordRedirectTarget) {
83 this.templateContextFactory = templateContextFactory;
84 this.template = template;
85 this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget;
92 public String getPath() {
97 * Returns the title of the page.
100 * The request to serve
101 * @return The title of the page
103 protected String getPageTitle(FreenetRequest request) {
111 public boolean isPrefixPage() {
119 public Response handleRequest(FreenetRequest request, Response response) throws IOException {
120 String redirectTarget = getRedirectTarget(request);
121 if (redirectTarget != null) {
122 return new RedirectResponse(redirectTarget);
125 if (isFullAccessOnly() && !request.getToadletContext().isAllowedFullAccess()) {
126 return response.setStatusCode(401).setStatusText("Not authorized").setContentType("text/html");
128 ToadletContext toadletContext = request.getToadletContext();
129 if (request.getMethod() == Method.POST) {
130 /* require form password. */
131 String formPassword = request.getHttpRequest().getPartAsStringFailsafe("formPassword", 32);
132 if (!formPassword.equals(toadletContext.getContainer().getFormPassword())) {
133 return new RedirectResponse(invalidFormPasswordRedirectTarget);
136 PageMaker pageMaker = toadletContext.getPageMaker();
137 PageNode pageNode = pageMaker.getPageNode(getPageTitle(request), toadletContext);
138 for (String styleSheet : getStyleSheets()) {
139 pageNode.addCustomStyleSheet(styleSheet);
141 for (Map<String, String> linkNodeParameters : getAdditionalLinkNodes(request)) {
142 HTMLNode linkNode = pageNode.headNode.addChild("link");
143 for (Entry<String, String> parameter : linkNodeParameters.entrySet()) {
144 linkNode.addAttribute(parameter.getKey(), parameter.getValue());
147 String shortcutIcon = getShortcutIcon();
148 if (shortcutIcon != null) {
149 pageNode.addForwardLink("icon", shortcutIcon);
152 TemplateContext templateContext = templateContextFactory.createTemplateContext();
153 templateContext.mergeContext(template.getInitialContext());
155 long start = System.nanoTime();
156 processTemplate(request, templateContext);
157 long finish = System.nanoTime();
158 logger.log(Level.FINEST, "Template was rendered in " + ((finish - start) / 1000) / 1000.0 + "ms.");
159 } catch (RedirectException re1) {
160 return new RedirectResponse(re1.getTarget());
163 StringWriter stringWriter = new StringWriter();
164 template.render(templateContext, stringWriter);
165 pageNode.content.addChild("%", stringWriter.toString());
167 postProcess(request, templateContext);
169 return response.setStatusCode(200).setStatusText("OK").setContentType("text/html").write(pageNode.outer.generate());
173 * Can be overridden to return a custom set of style sheets that are to be
174 * included in the page’s header.
176 * @return Additional style sheets to load
178 protected Collection<String> getStyleSheets() {
179 return Collections.emptySet();
183 * Returns the name of the shortcut icon to include in the page’s header.
185 * @return The URL of the shortcut icon, or {@code null} for no icon
187 protected String getShortcutIcon() {
192 * Can be overridden when extending classes need to set variables in the
193 * template before it is rendered.
196 * The request that is rendered
197 * @param templateContext
198 * The template context to set variables in
199 * @throws RedirectException
200 * if the processing page wants to redirect after processing
202 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
207 * This method will be called after
208 * {@link #processTemplate(FreenetRequest, TemplateContext)} has processed
209 * the template and the template was rendered. This method will not be
210 * called if {@link #processTemplate(FreenetRequest, TemplateContext)}
211 * throws a {@link RedirectException}!
214 * The request being processed
215 * @param templateContext
216 * The template context that supplied the rendered data
218 protected void postProcess(FreenetRequest request, TemplateContext templateContext) {
223 * Can be overridden to redirect the user to a different page, in case a log
224 * in is required, or something else is wrong.
227 * The request that is processed
228 * @return The URL to redirect to, or {@code null} to not redirect
230 protected String getRedirectTarget(FreenetRequest request) {
235 * Returns additional <link> nodes for the HTML’s <head> node.
238 * The request for which to return the link nodes
239 * @return All link nodes that should be added to the HTML head
241 protected List<Map<String, String>> getAdditionalLinkNodes(FreenetRequest request) {
242 return Collections.emptyList();
246 * Returns whether this page should only be allowed for requests from hosts
249 * @return {@code true} if this page should only be allowed for hosts with
250 * full access, {@code false} to allow this page for any host
252 protected boolean isFullAccessOnly() {
260 public boolean isLinkExcepted(URI link) {
265 // INTERFACE LinkEnabledCallback
272 public boolean isEnabled(ToadletContext toadletContext) {
273 return !isFullAccessOnly();
277 * Exception that can be thrown to signal that a subclassed {@link Page}
278 * wants to redirect the user during the
279 * {@link FreenetTemplatePage#processTemplate(FreenetRequest, TemplateContext)}
282 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
284 public static class RedirectException extends Exception {
286 /** The target to redirect to. */
287 private final String target;
290 * Creates a new redirect exception.
293 * The target of the redirect
295 public RedirectException(String target) {
296 this.target = target;
300 * Returns the target to redirect to.
302 * @return The target to redirect to
304 public String getTarget() {