blob: 81abe74bb3d48c377d8490a100015250be7a8c69 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/*
* Copyright 2013 Jesse Morgan
*/
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;
import com.p4square.fmfacade.FreeMarkerPageResource;
/**
* ErrorPage wraps a String or Template Representation and displays the given
* error message.
*
* @author Jesse Morgan <jesse@jesterpm.net>
*/
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.");
public static final ErrorPage BACKEND_ERROR =
new ErrorPage("Error communicating with backend.");
public static final ErrorPage NOT_FOUND =
new ErrorPage("The requested URL could not be found.");
private static Template cTemplate = null;
private static Map<String, Object> cRoot = 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, Map<String, Object> root) {
cTemplate = template;
cRoot = root;
}
protected Representation getRepresentation() {
if (cTemplate == null) {
return new StringRepresentation(mMessage);
} else {
Map<String, Object> root = new HashMap<String, Object>(cRoot);
root.put("errorMessage", mMessage);
return new TemplateRepresentation(cTemplate, root, MediaType.TEXT_HTML);
}
}
@Override
public void write(Writer writer) throws IOException {
getRepresentation().write(writer);
}
}
|