Repository navigation
Expand file tree
/
Copy pathNetworkHandler.java
More file actions
140 lines (129 loc) · 4.55 KB
/
Copy pathNetworkHandler.java
File metadata and controls
140 lines (129 loc) · 4.55 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
package act.xio;
import act.Act;
import act.app.ActionContext;
import act.app.App;
import act.app.RequestRefreshClassLoader;
import act.app.RequestServerRestart;
import act.app.util.NamedPort;
import act.handler.RequestHandler;
import act.handler.builtin.controller.RequestHandlerProxy;
import act.handler.event.BeforeCommit;
import act.metric.Metric;
import act.metric.MetricInfo;
import act.metric.Timer;
import act.route.Router;
import act.util.DestroyableBase;
import act.view.ActServerError;
import org.osgl.$;
import org.osgl.exception.NotAppliedException;
import org.osgl.http.H;
import org.osgl.logging.LogManager;
import org.osgl.logging.Logger;
import org.osgl.mvc.result.Result;
import org.osgl.util.E;
import org.osgl.util.S;
/**
* A `NetworkHandler` can be registered to an {@link Network} and get invoked when
* there are network event (e.g. an HTTP request) incoming
*/
public class NetworkHandler extends DestroyableBase implements $.Func1<ActionContext, Void> {
private static Logger logger = LogManager.get(NetworkHandler.class);
final private App app;
private NamedPort port;
private Metric metric;
public NetworkHandler(App app) {
E.NPE(app);
this.app = app;
this.metric = Act.metricPlugin().metric("act.http");
}
public NetworkHandler(App app, NamedPort port) {
this(app);
this.port = port;
this.metric = Act.metricPlugin().metric("act.http");
}
public App app() {
return app;
}
public synchronized void handle(ActionContext ctx) {
if (isDestroyed()) {
return;
}
H.Request req = ctx.req();
String url = req.url();
H.Method method = req.method();
Timer timer = null;
try {
if (Act.isDev()) {
synchronized (app) {
try {
app.detectChanges();
} catch (RequestRefreshClassLoader refreshRequest) {
app.refresh();
} catch (RequestServerRestart requestServerRestart) {
app.refresh();
}
}
}
if (app.config().contentSuffixAware()) {
if (url.endsWith("/json") || url.endsWith(".json")) {
url = url.substring(0, url.length() - 5);
req.accept(H.Format.JSON);
} else if (url.endsWith("/xml") || url.endsWith(".xml")) {
url = url.substring(0, url.length() - 4);
req.accept(H.Format.XML);
} else if (url.endsWith("/csv") || url.endsWith(".csv")) {
url = url.substring(0, url.length() - 4);
req.accept(H.Format.CSV);
}
}
timer = metric.startTimer(MetricInfo.ROUTING);
RequestHandler rh = router().getInvoker(method, url, ctx);
timer.stop();
ctx.handler(rh);
timer = metric.startTimer(S.builder(MetricInfo.HTTP_HANDLER).append(":").append(rh).toString());
rh.handle(ctx);
} catch (Result r) {
try {
r = RequestHandlerProxy.GLOBAL_AFTER_INTERCEPTOR.apply(r, ctx);
} catch (Exception e) {
logger.error(e, "Error calling global after interceptor");
r = ActServerError.of(e);
}
app.eventBus().emit(new BeforeCommit(r, ctx));
r.apply(req, ctx.resp());
} catch (Exception t) {
logger.error(t, "Error handling network request");
Result r;
try {
r = RequestHandlerProxy.GLOBAL_EXCEPTION_INTERCEPTOR.apply(t, ctx);
} catch (Exception e) {
logger.error(e, "Error calling global exception interceptor");
r = ActServerError.of(e);
}
if (null == r) {
r = ActServerError.of(t);
}
app.eventBus().emit(new BeforeCommit(r, ctx));
r.apply(req, ctx.resp());
} finally {
// we don't destroy ctx here in case it's been passed to
// another thread
ActionContext.clearCurrent();
if (null != timer) {
timer.stop();
}
}
}
@Override
public Void apply(ActionContext ctx) throws NotAppliedException, $.Break {
handle(ctx);
return null;
}
@Override
public String toString() {
return app().name();
}
private Router router() {
return app.router(port);
}
}