forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTags.java
More file actions
316 lines (280 loc) · 10.1 KB
/
Copy pathTags.java
File metadata and controls
316 lines (280 loc) · 10.1 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package act.view.rythm;
/*-
* #%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.Destroyable;
import act.act_messages;
import act.app.ActionContext;
import act.i18n.I18n;
import act.internal.util.ResourceChecksumManager;
import act.job.OnAppStart;
import act.route.Router;
import act.util.ActContext;
import act.util.DestroyableBase;
import org.osgl.$;
import org.osgl.exception.NotAppliedException;
import org.osgl.util.E;
import org.osgl.util.S;
import org.rythmengine.RythmEngine;
import org.rythmengine.template.JavaTagBase;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.validation.ConstraintViolation;
/**
* Defines fast tags for Act app
*/
@Singleton
public class Tags extends DestroyableBase {
@Inject
private List<JavaTagBase> fastTags;
@OnAppStart
@Override
protected void releaseResources() {
Destroyable.Util.tryDestroyAll(fastTags, ApplicationScoped.class);
}
public void register(RythmEngine engine) {
if (null == fastTags) {
// something wrong before Dependency Injector initialized
// register basic tags required by Error page template
engine.registerFastTag(new ActMessage());
return;
}
for (JavaTagBase tag : fastTags) {
engine.registerFastTag(tag);
}
}
/**
* Retrieve validation error message by field name
*
* Usage: `@_error("foo.name")` where`foo.name` is the field name
*/
public static class ValidationError extends JavaTagBase {
@Override
public String __getName() {
return "_error";
}
@Override
protected void call(__ParameterList params, __Body body) {
int paramSize = params.size();
E.illegalArgumentIf(paramSize < 1);
String field = params.get(0).value.toString();
ConstraintViolation violation = ActContext.Base.currentContext().violation(field);
if (null != violation) {
p(violation.getMessage());
}
}
}
/**
* Retrieve act framework defined i18n messages
*
* Usage: `@actMsg("msg-id")`
*/
public static class ActMessage extends JavaTagBase {
@Override
public String __getName() {
return "actMsg";
}
@Override
protected void call(__ParameterList params, __Body body) {
int paramSize = params.size();
E.illegalArgumentIf(paramSize < 1);
String msg = params.get(0).value.toString();
Object[] args;
if (paramSize > 1) {
args = new Object[paramSize - 1];
for (int i = 1; i < paramSize; ++i) {
args[i - 1] = params.get(i).value;
}
} else {
args = new Object[0];
}
p(I18n.i18n(act_messages.class, msg, args));
}
}
public static class Resource extends JavaTagBase {
@Inject
private ResourceChecksumManager checksumManager;
@Override
public String __getName() {
return "resource";
}
@Override
protected void call(__ParameterList params, __Body body) {
E.illegalArgumentIf(params.size() < 1);
String path = params.get(0).value.toString();
call(path);
}
protected void call(String path) {
if (path.startsWith("http:") || path.startsWith("//")) {
// we don't process absolute path
p(path);
return;
}
if (Act.isDev()) {
if (!path.startsWith("/")) {
path = '/' + path;
}
path = path + (path.contains("?") ? '&' : '?');
path = path + "ts=" + $.ms();
} else {
String checksum = checksumManager.checksumOf(path);
if (!path.startsWith("/")) {
path = '/' + path;
}
if (S.notBlank(checksum)) {
String sep = path.contains("?") ? "&" : "?";
path = S.concat(path, sep, "checksum=", checksum);
}
}
p(path);
}
}
public static class Asset extends Resource {
@Override
public String __getName() {
return "asset";
}
@Override
protected void call(__ParameterList params, __Body body) {
E.illegalArgumentIf(params.size() < 1);
String path = params.get(0).value.toString();
if (!path.startsWith("/asset/") && !path.startsWith("asset/")) {
path = org.osgl.util.S.pathConcat("asset", '/', path);
}
call(path);
}
}
/**
* Retrieve reverse routing URL path
*
* Usage: `@fullUrl()`
*/
public static class ReverseRouting extends JavaTagBase {
private boolean fullUrl = false;
public ReverseRouting() {
}
protected ReverseRouting(boolean fullUrl) {
this.fullUrl = fullUrl;
}
@Override
public String __getName() {
return "url";
}
// See https://github.com/actframework/actframework/issues/108#infer_reference
private static $.Func0<String> INFER_REFERENCE_PROVIDER = new $.Func0<String>() {
@Override
public String apply() throws NotAppliedException, $.Break {
ActContext context = ActContext.Base.currentContext();
E.illegalStateIf(null == context, "Cannot get full action path reference outside of act context");
/*
* try to determine if the template is free or bounded template
*/
if (context.templatePathIsImplicit()) {
return context.methodPath();
} else {
String path = context.templatePath();
path = org.osgl.util.S.beforeLast(path, ".");
if (!isTemplateBounded(path, context.methodPath())) {
return context.methodPath();
}
return path.replace('/', '.');
}
}
};
private static boolean isTemplateBounded(String templatePath, String methodPath) {
int pos = templatePath.indexOf('/');
if (pos < 0) {
// must be a free template without package hierarchies
return false;
}
int pos2 = methodPath.indexOf('.');
if (pos2 != pos) {
// must be a free template as the first package path doesn't match
return false;
}
String templatePathStartPkg = templatePath.substring(0, pos);
String methodPathStartPkg = methodPath.substring(0, pos2);
return S.eq(templatePathStartPkg, methodPathStartPkg);
}
// see https://github.com/actframework/actframework/issues/108
private String inferFullPath(String actionPath) {
E.illegalArgumentIf(S.empty(actionPath), "action path expected");
if (actionPath.contains("/") || (!actionPath.contains(".") && !actionPath.contains("("))) {
// this is a URL path, not action path
return actionPath;
}
if (actionPath.contains("(")) {
actionPath = org.osgl.util.S.beforeFirst(actionPath, "(");
}
return Router.inferFullActionPath(actionPath, INFER_REFERENCE_PROVIDER);
}
@Override
protected void call(__ParameterList parameterList, __Body body) {
Object o = parameterList.getByName("value");
if (null == o) {
o = parameterList.getDefault();
}
String value = inferFullPath(o.toString());
boolean fullUrl = this.fullUrl;
o = parameterList.getByName("fullUrl");
if (null != o) {
fullUrl = (Boolean) o;
}
ActionContext context = ActionContext.current();
Router router = null == context ? Act.app().router() : context.router();
if (value.contains("/") || !value.contains(".")) {
int n = parameterList.size();
Object[] args = new Object[n - 1];
for (int i = 0; i < n - 1; ++i) {
args[i] = parameterList.getByPosition(i + 1);
}
if (fullUrl) {
p(router._fullUrl(value, args));
} else {
p(String.format(value, args));
}
} else {
// value is an action path, need reverse route
Map<String, Object> args = new HashMap<>();
for (__Parameter param : parameterList) {
String name = param.name;
if (S.empty(name) || "value".equals(name) || "fullUrl".equals(name)) {
continue;
}
args.put(param.name, param.value);
}
p(router.reverseRoute(value, args, fullUrl));
}
}
}
public static class FullUrl extends ReverseRouting {
@Override
public String __getName() {
return "fullUrl";
}
public FullUrl() {
super(true);
}
}
}