forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppInterceptorManager.java
More file actions
98 lines (81 loc) · 3.7 KB
/
AppInterceptorManager.java
File metadata and controls
98 lines (81 loc) · 3.7 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
package act.app;
/*-
* #%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.handler.builtin.controller.AfterInterceptor;
import act.handler.builtin.controller.BeforeInterceptor;
import act.handler.builtin.controller.ExceptionInterceptor;
import act.handler.builtin.controller.FinallyInterceptor;
import act.handler.builtin.controller.RequestHandlerProxy.GroupAfterInterceptor;
import act.handler.builtin.controller.RequestHandlerProxy.GroupExceptionInterceptor;
import act.handler.builtin.controller.RequestHandlerProxy.GroupFinallyInterceptor;
import act.handler.builtin.controller.RequestHandlerProxy.GroupInterceptorWithResult;
import org.osgl.mvc.result.Result;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static act.handler.builtin.controller.RequestHandlerProxy.insertInterceptor;
/**
* Manage interceptors at App level
*/
public class AppInterceptorManager extends AppServiceBase<AppInterceptorManager> {
private List<BeforeInterceptor> beforeInterceptors = new ArrayList<>();
private List<AfterInterceptor> afterInterceptors = new ArrayList<>();
private List<ExceptionInterceptor> exceptionInterceptors = new ArrayList<>();
private List<FinallyInterceptor> finallyInterceptors = new ArrayList<>();
final GroupInterceptorWithResult BEFORE_INTERCEPTOR = new GroupInterceptorWithResult(beforeInterceptors);
final GroupAfterInterceptor AFTER_INTERCEPTOR = new GroupAfterInterceptor(afterInterceptors);
final GroupFinallyInterceptor FINALLY_INTERCEPTOR = new GroupFinallyInterceptor(finallyInterceptors);
final GroupExceptionInterceptor EXCEPTION_INTERCEPTOR = new GroupExceptionInterceptor(exceptionInterceptors);
AppInterceptorManager(App app) {
super(app);
}
public Result handleBefore(ActionContext actionContext) throws Exception {
return BEFORE_INTERCEPTOR.apply(actionContext);
}
public Result handleAfter(Result result, ActionContext actionContext) throws Exception {
return AFTER_INTERCEPTOR.apply(result, actionContext);
}
public void handleFinally(ActionContext actionContext) throws Exception {
FINALLY_INTERCEPTOR.apply(actionContext);
}
public Result handleException(Exception ex, ActionContext actionContext) throws Exception {
return EXCEPTION_INTERCEPTOR.apply(ex, actionContext);
}
public void registerInterceptor(BeforeInterceptor interceptor) {
insertInterceptor(beforeInterceptors, interceptor);
}
public void registerInterceptor(AfterInterceptor interceptor) {
insertInterceptor(afterInterceptors, interceptor);
}
public void registerInterceptor(FinallyInterceptor interceptor) {
insertInterceptor(finallyInterceptors, interceptor);
}
public void registerInterceptor(ExceptionInterceptor interceptor) {
insertInterceptor(exceptionInterceptors, interceptor);
Collections.sort(exceptionInterceptors);
}
@Override
protected void releaseResources() {
beforeInterceptors.clear();
afterInterceptors.clear();
exceptionInterceptors.clear();
finallyInterceptors.clear();
}
}