import net.pterodactylus.sone.web.page.Page.Request.Method;
import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.template.DataProvider;
import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
import freenet.clients.http.LinkEnabledCallback;
import freenet.clients.http.PageMaker;
import freenet.clients.http.PageNode;
import freenet.clients.http.ToadletContext;
-import freenet.l10n.BaseL10n;
/**
* Base class for all {@link Page}s that are rendered with {@link Template}s.
/** The path of the page. */
private final String path;
+ /** The template context factory. */
+ private final TemplateContextFactory templateContextFactory;
+
/** The template to render. */
private 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;
*
* @param path
* The path of the page
+ * @param templateContextFactory
+ * The template context factory
* @param template
* The template to render
- * @param l10n
- * 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, String invalidFormPasswordRedirectTarget) {
+ public TemplatePage(String path, TemplateContextFactory templateContextFactory, Template template, String invalidFormPasswordRedirectTarget) {
this.path = path;
+ this.templateContextFactory = templateContextFactory;
this.template = template;
- this.l10n = l10n;
- this.pageTitleKey = pageTitleKey;
this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget;
}
}
/**
+ * Returns the title of the page.
+ *
+ * @param request
+ * The request to serve
+ * @return The title of the page
+ */
+ protected String getPageTitle(Request request) {
+ return null;
+ }
+
+ /**
* {@inheritDoc}
*/
@Override
}
}
PageMaker pageMaker = toadletContext.getPageMaker();
- PageNode pageNode = pageMaker.getPageNode(l10n.getString(pageTitleKey), toadletContext);
+ PageNode pageNode = pageMaker.getPageNode(getPageTitle(request), toadletContext);
for (String styleSheet : getStyleSheets()) {
pageNode.addCustomStyleSheet(styleSheet);
}
pageNode.addForwardLink("icon", shortcutIcon);
}
- DataProvider dataProvider = template.createDataProvider();
+ TemplateContext templateContext = templateContextFactory.createTemplateContext();
+ templateContext.mergeContext(template.getInitialContext());
try {
long start = System.nanoTime();
- processTemplate(request, dataProvider);
+ processTemplate(request, templateContext);
long finish = System.nanoTime();
logger.log(Level.FINEST, "Template was rendered in " + ((finish - start) / 1000) / 1000.0 + "ms.");
} catch (RedirectException re1) {
}
StringWriter stringWriter = new StringWriter();
- template.render(dataProvider, stringWriter);
+ template.render(templateContext, stringWriter);
pageNode.content.addChild("%", stringWriter.toString());
- postProcess(request, dataProvider);
+ postProcess(request, templateContext);
return new Response(200, "OK", "text/html", pageNode.outer.generate());
}
*
* @param request
* The request that is rendered
- * @param dataProvider
- * The data provider to set variables in
+ * @param templateContext
+ * The template context to set variables in
* @throws RedirectException
* if the processing page wants to redirect after processing
*/
- protected void processTemplate(Request request, DataProvider dataProvider) throws RedirectException {
+ protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
/* do nothing. */
}
/**
* This method will be called after
- * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, DataProvider)}
+ * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
* 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, DataProvider)}
+ * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
* throws a {@link RedirectException}!
*
* @param request
* The request being processed
- * @param dataProvider
- * The data provider that supplied the rendered data
+ * @param templateContext
+ * The template context that supplied the rendered data
*/
- protected void postProcess(Request request, DataProvider dataProvider) {
+ protected void postProcess(Request request, TemplateContext templateContext) {
/* do nothing. */
}
/**
* Exception that can be thrown to signal that a subclassed {@link Page}
* wants to redirect the user during the
- * {@link TemplatePage#processTemplate(net.pterodactylus.sone.web.page.Page.Request, DataProvider)}
+ * {@link TemplatePage#processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)}
* method call.
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
- public class RedirectException extends Exception {
+ public static class RedirectException extends Exception {
/** The target to redirect to. */
private final String target;