- 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());
- }
-
- postProcess(request);
-
- StringWriter stringWriter = new StringWriter();
- template.render(stringWriter);
- pageNode.content.addChild("%", stringWriter.toString());
-
- return new Response(200, "OK", "text/html", pageNode.outer.generate());
- }
-
- /**
- * Can be overridden to return a custom set of style sheets that are to be
- * included in the page’s header.
- *
- * @return Additional style sheets to load
- */
- protected Collection<String> getStyleSheets() {
- return Collections.emptySet();
- }
-
- /**
- * 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.
- *
- * @param request
- * The request that is rendered
- * @param template
- * The template to set variables in
- * @throws RedirectException
- * if the processing page wants to redirect after processing
- */
- protected void processTemplate(Request request, Template template) throws RedirectException {
- /* do nothing. */
- }
-
- /**
- * 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
- */
- protected void postProcess(Request request) {
- /* 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.
- *
- * @param request
- * The request that is processed
- * @return The URL to redirect to, or {@code null} to not redirect
- */
- protected String getRedirectTarget(Page.Request request) {
- return null;
- }
-
- //
- // INTERFACE LinkEnabledCallback
- //
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean isEnabled(ToadletContext toadletContext) {
- return true;
- }
-
- /**
- * 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, Template)}
- * method call.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
- public class RedirectException extends Exception {
-
- /** The target to redirect to. */
- private final String target;
-
- /**
- * Creates a new redirect exception.
- *
- * @param target
- * The target of the redirect
- */
- public RedirectException(String target) {
- this.target = target;
+ responseWriter = new OutputStreamWriter(responseOutputStream, "UTF-8");
+ TemplateContext templateContext = templateContextFactory.createTemplateContext();
+ templateContext.set("request", request);
+ template.render(templateContext, responseWriter);
+ } catch (IOException ioe1) {
+ logger.log(Level.WARNING, "Could not render template for path “" + path + "”!", ioe1);
+ } finally {
+ Closer.close(responseWriter);
+ Closer.close(responseOutputStream);