-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathPropertySpec.java
More file actions
404 lines (358 loc) · 13.9 KB
/
Copy pathPropertySpec.java
File metadata and controls
404 lines (358 loc) · 13.9 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
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.app.ActionContext;
import act.cli.CliContext;
import act.cli.CliSession;
import act.controller.meta.HandlerMethodMetaInfo;
import org.osgl.$;
import org.osgl.Lang;
import org.osgl.util.C;
import org.osgl.util.S;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
/**
* Mark on a method (could be cli command, or controller action)
* to specify the fields to be exported.
* <p>This annotation is only effective when there is one and only one
* type of object returned, as a single instance or a collection of instances, e.g</p>
* <pre>
* {@literal @}PropertySpec({"firstName","lastName","email"})
* public List<Employee> getEmployees(String search) {
* List<Employee> retList = EmployeeDao.find(search).asList();
* return retList;
* }
* </pre>
* Suppose the request accept {@code application/json} type, then only the following
* field of the {@code Employee} instances will be exported in JSON output:
* <ul>
* <li>firstName</li>
* <li>lastName</li>
* <li>email</li>
* </ul>
* <p>
* When the result is to be presented on a {@link CliSession} and
* {@code PropertySpec} annotation is presented, either {@link act.cli.TableView}
* or {@link act.util.JsonView} can be used to define the presenting style.
* If both {@code TableView} and {@code JsonView} are found on the method
* then {@code JsonView} is the winner. If non of them is presented then
* {@code JsonView} will be used by default
* </p>
* <p>
* When the result is to be write to an {@link org.osgl.http.H.Response}, and
* {@code PropertySpec} annotation is presented on the controller action method,
* then the return value (if not of type {@link org.osgl.mvc.result.Result}) will
* be serialized into a JSON string and the filter will effect and impact the
* JSON string
* </p>
* @see act.cli.TableView
* @see act.util.JsonView
* @see FastJsonPropertyPreFilter
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PropertySpec {
/**
* Specify the object fields to be displayed in final result. E.g.
* <pre>
* {@literal @}PropertySpec({"firstName","lastName","email"})
* </pre>
* You can specify multiple fields in one string, with fields
* separated with one of the following character: {@code ,;|}
* <pre>
* {@literal @}PropertySpec("firstName,lastName,email")
* </pre>
* You can use {@code as} to specify the label, e.g.
* <pre>
* {@literal @}PropertySpec("fn as First name,ln as Last name,email as Email")
* </pre>
* If there are multiple levels of objects, use {@code .} or {@code /} to
* express the traverse path:
* <pre>
* {@literal @}PropertySpec("fn,ln,contact.address.street,contact.address.city,contact.email")
* </pre>
* Instead of specifying fields to be exported, it can also specify the fields to be
* excluded from output with the symbol {@code -}, e.g.
* <pre>
* {@literal @}PropertySpec("-password,-salary")
* </pre>
* when symbol {@code -} is used to specify the excluded fields, then all the fields
* without symbol {@code -} in the list will be ignored. However it can still use
* {@code as} notation to specify the label. E.g.
* <pre>
* {@literal @}PropertySpec("-password,-salary,fn as firstName,ln as lastName")
* </pre>
* @return the field specification
*/
String[] value() default {};
/**
* Specify the spec for command line interface output
* <p>
* If not specified, then it will use the spec specified in {@link #value()}
* when output to CLI
* </p>
* @return the field specification for CLI
* @see #value()
*/
String[] cli() default {};
/**
* Specify the spec for http response output
* <p>
* If not specified, then it will use the spec specified in {@link #value()}
* when output to http response
* </p>
* @return the field specification for http
* @see #value()
*/
String[] http() default {};
/**
* Capture the {@code PropertySpec} annotation meta info in bytecode scanning phase
*/
class MetaInfo {
// split "fn as firstName" into "fn" and "firstName"
private static Pattern p = Pattern.compile("\\s+as\\s+", Pattern.CASE_INSENSITIVE);
public static class Spec extends $.T3<List<String>, Set<String>, Map<String, String>> {
Spec() {
super(C.<String>newList(), C.<String>newSet(), C.<String, String>newMap());
}
List<String> outputs() {
return _1;
}
Set<String> excluded() {
return _2;
}
Map<String, String> labels() {
return _3;
}
boolean isEmpty() {
return _1.isEmpty() && _2.isEmpty() && _3.isEmpty();
}
public Lang._MappingStage applyTo(Lang._MappingStage stage) {
if (!outputs().isEmpty()) {
stage.filter(S.join(",", outputs()));
} else if (!excluded().isEmpty()) {
stage.filter(S.join(",", C.list(excluded()).map(S.F.prepend("-"))));
}
if (!labels().isEmpty()) {
stage.withHeadMapping(labels());
}
return stage;
}
}
private static Spec newSpec() {
return new Spec();
}
private Spec common = newSpec();
private Spec cli = newSpec();
private Spec http = newSpec();
private String commonRaw;
private String cliRaw;
private String httpRaw;
public void onValue(String value) {
commonRaw = value;
_on(value, common);
}
public void onCli(String value) {
cliRaw = value;
_on(value, cli);
}
public void onHttp(String value) {
httpRaw = value;
_on(value, http);
}
public void ensureValid() {
if (common.isEmpty() && http.isEmpty() && cli.isEmpty()) {
throw new IllegalStateException("no spec defined");
}
}
private void _on(String string, Spec spec) {
String[] sa = string.split("[,;]+");
for (String s: sa) {
s = s.trim();
if (s.startsWith("-")) {
spec.excluded().add(s.substring(1));
spec.outputs().clear();
} else {
String[] sa0 = p.split(s);
if (sa0.length > 1) {
String k = sa0[0].trim(), v = sa0[1].trim();
spec.labels().put(k, v);
if (spec.excluded().isEmpty()) {
spec.outputs().add(k);
}
} else if (spec.excluded().isEmpty()) {
spec.outputs().add(s.trim());
}
}
}
}
@Deprecated
public List<String> outputFields() {
return C.list(common.outputs());
}
public String raw(ActContext context) {
if (null == context) {
return commonRaw;
} else if (context instanceof ActionContext) {
return S.blank(httpRaw) ? commonRaw : httpRaw;
} else if (context instanceof CliContext) {
return S.blank(cliRaw) ? commonRaw : cliRaw;
} else {
// mail context is unlikely to happen
throw new IllegalStateException("context not applied: " + context);
}
}
public List<String> outputFields(ActContext context) {
Spec spec = spec(context);
return null == spec ? C.<String>list() : spec.outputs();
}
public List<S.Pair> outputFieldsAndLabel(ActContext context) {
Spec spec = spec(context);
if (null == spec) {
return C.list();
}
List<String> outputs = spec.outputs();
List<S.Pair> pairs = new ArrayList<>();
for (String f : outputs) {
S.Pair pair = S.pair(f, spec.labels().get(f));
pairs.add(pair);
}
return pairs;
}
public List<String> outputFieldsForHttp() {
Spec spec = httpSpec();
return null == spec ? C.<String>list() : spec.outputs();
}
public List<String> labels(List<String> outputs, ActContext context) {
List<String> retList = new ArrayList<>();
for (String f : outputs) {
retList.add(label(f, context));
}
return retList;
}
public List<String> labels2(List<S.Pair> outputs, ActContext context) {
List<String> retList = new ArrayList<>();
for (S.Pair pair : outputs) {
retList.add(label(pair, context));
}
return retList;
}
public Map<String, String> labelMapping() {
return C.Map(common.labels());
}
public Map<String, String> labelMapping(ActContext context) {
return null == context ? labelMapping() : C.Map(spec(context).labels());
}
public Set<String> excludedFields(ActContext context) {
Spec spec = spec(context);
return null == spec ? C.<String>Set() : C.Set(spec.excluded());
}
public Set<String> excludeFieldsForHttp() {
Spec spec = httpSpec();
return null == spec ? C.<String>Set() : C.Set(spec.excluded());
}
public String label(String field, ActContext context) {
String lbl = spec(context).labels().get(field);
return null == lbl ? field : lbl;
}
public String label(S.Pair field, ActContext context) {
String lbl = spec(context).labels().get(field._1);
String defLabel = field._2;
if (null == defLabel) {
defLabel = field._1;
}
return null == lbl ? defLabel : lbl;
}
public void setLabelIfNotFound(String field, String label, ActContext context) {
Map<String, String> map = spec(context).labels();
if (!map.containsKey(field)) {
map.put(field, label);
}
}
public Lang._MappingStage applyTo(Lang._MappingStage mappingStage, ActContext context) {
return spec(context).applyTo(mappingStage);
}
private Spec httpSpec() {
return null == http || http.isEmpty() ? common : http;
}
private Spec spec(ActContext context) {
if (null == context) {
return common;
}
if (context instanceof ActionContext) {
return null == http || http.isEmpty() ? common : http;
} else if (context instanceof CliContext) {
return null == cli || cli.isEmpty() ? common : cli;
} else {
// mail context is unlikely to happen
throw new IllegalStateException("context not applied: " + context);
}
}
public static MetaInfo withCurrentNoConsume(MetaInfo builtIn, ActContext context) {
return _withCurrent(builtIn, context, false);
}
public static MetaInfo withCurrent(MetaInfo builtIn, ActContext context) {
return _withCurrent(builtIn, context, true);
}
private static MetaInfo _withCurrent(MetaInfo builtIn, ActContext context, boolean consume) {
// see https://github.com/actframework/actframework/issues/1118
if (consume && null != context) {
if (context.isPropertySpecConsumed()) {
return null;
}
context.markPropertySpecConsumed();
}
MetaInfo retVal = builtIn;
MetaInfo spec = currentSpec.get();
if (null != spec) {
retVal = spec;
} else {
String s = PropertySpec.current.get();
if (S.notBlank(s)) {
spec = new PropertySpec.MetaInfo();
if (context instanceof CliContext) {
spec.onCli(s);
} else {
spec.onHttp(s);
}
retVal = spec;
}
}
if (context instanceof ActionContext) {
ActionContext actionContext = (ActionContext) context;
actionContext.propertySpec(retVal);
}
return retVal;
}
public static MetaInfo withCurrent(HandlerMethodMetaInfo methodMetaInfo, ActContext context) {
MetaInfo builtIn = null == methodMetaInfo ? null : methodMetaInfo.propertySpec();
return withCurrent(builtIn, context);
}
}
ThreadLocal<String> current = new ThreadLocal<>();
ThreadLocal<PropertySpec.MetaInfo> currentSpec = new ThreadLocal<>();
}