import java.io.StringWriter;
import java.util.Collection;
import java.util.Collections;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import net.pterodactylus.sone.web.page.Page.Request.Method;
+import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.template.Template;
import freenet.clients.http.LinkEnabledCallback;
import freenet.clients.http.PageMaker;
*/
public class TemplatePage implements Page, LinkEnabledCallback {
+ /** The logger. */
+ private static final Logger logger = Logging.getLogger(TemplatePage.class);
+
/** The path of the page. */
private final String path;
/** The template to render. */
- private final Template template;
+ protected final Template template;
/** The L10n handler. */
private final BaseL10n l10n;
/** The l10n key for the page title. */
private final String pageTitleKey;
+ /** Where to redirect for invalid form passwords. */
+ private final String invalidFormPasswordRedirectTarget;
+
/**
* Creates a new template page.
*
* The L10n handler
* @param pageTitleKey
* The l10n key of the title page
+ * @param invalidFormPasswordRedirectTarget
+ * The target to redirect to if a POST request does not contain
+ * the correct form password
*/
- public TemplatePage(String path, Template template, BaseL10n l10n, String pageTitleKey) {
+ public TemplatePage(String path, Template template, BaseL10n l10n, String pageTitleKey, String invalidFormPasswordRedirectTarget) {
this.path = path;
this.template = template;
this.l10n = l10n;
this.pageTitleKey = pageTitleKey;
+ this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget;
}
/**
}
ToadletContext toadletContext = request.getToadletContext();
+ if (request.getMethod() == Method.POST) {
+ /* require form password. */
+ String formPassword = request.getHttpRequest().getPartAsStringFailsafe("formPassword", 32);
+ if (!formPassword.equals(toadletContext.getContainer().getFormPassword())) {
+ return new RedirectResponse(invalidFormPasswordRedirectTarget);
+ }
+ }
PageMaker pageMaker = toadletContext.getPageMaker();
PageNode pageNode = pageMaker.getPageNode(l10n.getString(pageTitleKey), toadletContext);
for (String styleSheet : getStyleSheets()) {
pageNode.addCustomStyleSheet(styleSheet);
}
+ String shortcutIcon = getShortcutIcon();
+ if (shortcutIcon != null) {
+ pageNode.addForwardLink("icon", shortcutIcon);
+ }
try {
+ long start = System.nanoTime();
processTemplate(request, template);
+ long finish = System.nanoTime();
+ logger.log(Level.FINEST, "Template was rendered in " + ((finish - start) / 1000) / 1000.0 + "ms.");
} catch (RedirectException re1) {
return new RedirectResponse(re1.getTarget());
}
template.render(stringWriter);
pageNode.content.addChild("%", stringWriter.toString());
+ postProcess(request, template);
+
return new Response(200, "OK", "text/html", pageNode.outer.generate());
}
}
/**
+ * Returns the name of the shortcut icon to include in the page’s header.
+ *
+ * @return The URL of the shortcut icon, or {@code null} for no icon
+ */
+ protected String getShortcutIcon() {
+ return null;
+ }
+
+ /**
* Can be overridden when extending classes need to set variables in the
* template before it is rendered.
*
}
/**
+ * This method will be called after
+ * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, Template)}
+ * has processed the template and the template was rendered. This method
+ * will not be called if
+ * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, Template)}
+ * throws a {@link RedirectException}!
+ *
+ * @param request
+ * The request being processed
+ * @param template
+ * The template that was rendered
+ */
+ protected void postProcess(Request request, Template template) {
+ /* do nothing. */
+ }
+
+ /**
* Can be overridden to redirect the user to a different page, in case a log
* in is required, or something else is wrong.
*