diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2013-09-01 15:56:37 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2013-09-01 15:56:37 -0700 |
commit | ba05f378d217316ebb3c92b69fdf48585be46825 (patch) | |
tree | bff4dbf0d74f4cddd7e31cb737af64e852a099ef | |
parent | 6b51ea6cf80f562b5c00b17129b8d8d1ec980269 (diff) |
Adding ErrorPage representation for error messages
-rw-r--r-- | src/com/p4square/grow/frontend/ErrorPage.java | 56 | ||||
-rw-r--r-- | src/com/p4square/grow/frontend/GrowFrontend.java | 8 | ||||
-rw-r--r-- | src/templates/templates/error.ftl | 17 | ||||
-rw-r--r-- | src/templates/templates/nav.ftl | 2 |
4 files changed, 75 insertions, 8 deletions
diff --git a/src/com/p4square/grow/frontend/ErrorPage.java b/src/com/p4square/grow/frontend/ErrorPage.java index 4a9380f..b57cc49 100644 --- a/src/com/p4square/grow/frontend/ErrorPage.java +++ b/src/com/p4square/grow/frontend/ErrorPage.java @@ -4,22 +4,64 @@ package com.p4square.grow.frontend; +import java.util.HashMap; +import java.util.Map; + +import java.io.IOException; +import java.io.Writer; + +import freemarker.template.Template; + +import org.restlet.data.MediaType; +import org.restlet.ext.freemarker.TemplateRepresentation; +import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; +import org.restlet.representation.WriterRepresentation; /** + * ErrorPage wraps a String or Template Representation and displays the given + * error message. * * @author Jesse Morgan <jesse@jesterpm.net> */ -public class ErrorPage extends StringRepresentation { - public static final ErrorPage TEMPLATE_NOT_FOUND = new ErrorPage(); - public static final ErrorPage RENDER_ERROR = new ErrorPage(); +public class ErrorPage extends WriterRepresentation { + public static final ErrorPage TEMPLATE_NOT_FOUND = + new ErrorPage("Could not find the requested page template."); + + public static final ErrorPage RENDER_ERROR = + new ErrorPage("Error rendering page."); + + private static Template cTemplate = null; + + private final String mMessage; + + public ErrorPage(String msg) { + this(msg, MediaType.TEXT_HTML); + } + + public ErrorPage(String msg, MediaType mediaType) { + super(mediaType); + + mMessage = msg; + } + + public static synchronized void setTemplate(Template template) { + cTemplate = template; + } + protected Representation getRepresentation() { + if (cTemplate == null) { + return new StringRepresentation(mMessage); - public ErrorPage() { - super("TODO"); + } else { + Map<String, Object> root = new HashMap<String, Object>(); + root.put("errorMessage", mMessage); + return new TemplateRepresentation(cTemplate, root, MediaType.TEXT_HTML); + } } - public ErrorPage(String s) { - super(s); + @Override + public void write(Writer writer) throws IOException { + getRepresentation().write(writer); } } diff --git a/src/com/p4square/grow/frontend/GrowFrontend.java b/src/com/p4square/grow/frontend/GrowFrontend.java index 37f0dea..7556c20 100644 --- a/src/com/p4square/grow/frontend/GrowFrontend.java +++ b/src/com/p4square/grow/frontend/GrowFrontend.java @@ -10,6 +10,8 @@ import java.io.IOException; import java.util.Arrays; import java.util.UUID; +import freemarker.template.Template; + import org.restlet.Application; import org.restlet.Component; import org.restlet.Client; @@ -74,6 +76,11 @@ public class GrowFrontend extends FMFacade { mConfig.updateConfig(configFilename); } + Template errorTemplate = getTemplate("templates/error.ftl"); + if (errorTemplate != null) { + ErrorPage.setTemplate(errorTemplate); + } + super.start(); } @@ -97,6 +104,7 @@ public class GrowFrontend extends FMFacade { defaultGuard.setNext(FreeMarkerPageResource.class); router.attachDefault(defaultGuard); router.attach("/login.html", LoginPageResource.class); + router.attach("/newaccount.html", NewAccountResource.class); final Router accountRouter = new Router(getContext()); accountRouter.attach("/authenticate", AuthenticatedResource.class); diff --git a/src/templates/templates/error.ftl b/src/templates/templates/error.ftl new file mode 100644 index 0000000..2494907 --- /dev/null +++ b/src/templates/templates/error.ftl @@ -0,0 +1,17 @@ +<#include "/macros/common.ftl"> +<#include "/macros/common-page.ftl"> + +<@commonpage> + <@noticebox> + </@noticebox> + + <@content> + <h1>An Error has Occurred</h1> + + <p>An error has occurred. If you continue to see this message, please contact us.</p> + <p>Error: ${errorMessage}</p> + </@content> +</@commonpage> + + + diff --git a/src/templates/templates/nav.ftl b/src/templates/templates/nav.ftl index cef283e..54074d5 100644 --- a/src/templates/templates/nav.ftl +++ b/src/templates/templates/nav.ftl @@ -1,6 +1,6 @@ <#macro navLink href> <li><a - <#if currentPage == href> + <#if currentPage!"" == href> class="current" </#if> href="${href}"><#nested></a></li> |