forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActErrorPageRender.java
More file actions
183 lines (167 loc) · 6.5 KB
/
ActErrorPageRender.java
File metadata and controls
183 lines (167 loc) · 6.5 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package act.util;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.Act;
import act.app.ActionContext;
import act.view.Template;
import act.view.ViewManager;
import com.alibaba.fastjson.JSON;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.mvc.ErrorPageRenderer;
import org.osgl.mvc.MvcConfig;
import org.osgl.mvc.result.ErrorResult;
import org.osgl.util.S;
import java.util.HashMap;
import java.util.Map;
import static org.osgl.http.H.Format.CSV;
import static org.osgl.http.H.Format.HTML;
import static org.osgl.http.H.Format.XML;
public class ActErrorPageRender extends ErrorPageRenderer {
public static final String ARG_ERROR = "_error";
private volatile Boolean i18n;
private Map<String, $.Var<Template>> templateCache = new HashMap<>();
@Override
protected String renderTemplate(ErrorResult error, H.Format format) {
ActionContext context = ActionContext.current();
if (null == context) {
return null;
}
Integer errorCode = error.errorCode();
int statusCode = error.statusCode();
fixRequestAcceptFormat(context);
Template t = getTemplate(statusCode, context);
if (null == t) {
String errorMsg = error.getMessage();
if (null == errorMsg) {
errorMsg = MvcConfig.errorMessage(error.status());
}
if (i18n()) {
String translated = context.i18n(true, errorMsg);
if (translated == errorMsg) {
translated = context.i18n(true, MvcConfig.class, errorMsg);
}
errorMsg = translated;
}
H.Format accept = context.accept();
if (H.Format.JSON == accept) {
return jsonContent(error, errorCode, errorMsg);
} else if (HTML == accept) {
String header = S.concat("HTTP/1.1 ", Integer.toString(statusCode), " ", errorMsg);
return S.concat("<!DOCTYPE html><html><head><meta charset='utf-8'><title>"
, header
, "</title></head><body><h1>"
, header, "</h1></body></html>");
} else if (H.Format.XML == accept) {
S.Buffer sb = context.strBuf();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>");
if (null != errorCode) {
sb.append("<code>").append(errorCode).append("</code");
}
sb.append("<message>").append(errorMsg).append("</message></error>");
return sb.toString();
} else if (CSV == accept) {
if (null == errorCode) {
return S.concat("message\n", errorMsg);
} else {
return S.concat("code,message\n", Integer.toString(errorCode), ",", errorMsg);
}
} else if (H.Format.TXT == accept) {
return null == errorCode ? errorMsg : S.concat(Integer.toString(errorCode), " ", errorMsg);
} else {
// Unknown accept format
return "";
}
}
if (HTML == context.accept()) {
String header = S.concat("HTTP/1.1 ", Integer.toString(statusCode), " ", MvcConfig.errorMessage(error.status()));
context.renderArg("header", header);
}
context.renderArg(ARG_ERROR, error);
return t.render(context);
}
private void fixRequestAcceptFormat(ActionContext context) {
H.Request req = context.req();
if (null != req && !isAcceptGoodForErrorPage(req.accept())) {
req.accept(H.Format.HTML);
}
}
private boolean isAcceptGoodForErrorPage(H.Format fmt) {
return (null == fmt ||
HTML == fmt
|| CSV == fmt
|| H.Format.JSON == fmt
|| XML == fmt);
}
private String jsonContent(ErrorResult error, Integer errorCode, String errorMsg) {
Object payload = error.attachment();
if (null != payload) {
return JSON.toJSONString(payload);
}
if (null == errorCode) {
return S.concat("{\"message\":\"", errorMsg, "\"}");
} else {
return S.concat("{\"code\":", S.string(errorCode), ",\"message\":\"", errorMsg, "\"}");
}
}
private String templatePath(int code, ActionContext context) {
ErrorTemplatePathResolver resolver = context.config().errorTemplatePathResolver();
if (null == resolver) {
resolver = new ErrorTemplatePathResolver.DefaultErrorTemplatePathResolver();
}
return resolver.resolve(code, context.accept());
}
private Template getTemplate(int statusCode, ActionContext context) {
H.Format format = context.accept();
String key = statusCode + "" + format;
$.Var<Template> templateBag = templateCache.get(key);
if (null == templateBag) {
ViewManager vm = Act.viewManager();
if (null == vm) {
// unit testing
return null;
}
context.templatePath(templatePath(statusCode, context));
Template t = vm.load(context);
if (null == t) {
// try default one
if (Act.isDev()) {
context.templatePath("/error/dev/errorPage." + context.accept().name());
} else {
context.templatePath("/error/errorPage." + context.accept().name());
}
t = vm.load(context);
}
templateBag = $.var(t);
templateCache.put(key, templateBag);
}
return templateBag.get();
}
private boolean i18n() {
if (null == i18n) {
synchronized (this) {
if (null == i18n) {
i18n = Act.appConfig().i18nEnabled();
}
}
}
return i18n;
}
}