Skip to content

Commit

Permalink
优化拦截器
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuo894 committed Sep 25, 2024
1 parent d41d633 commit 5e231b8
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/main/java/com/emby/util/ServerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ public static SimpleServer create(String... args) {
}
Object action = ReflectUtil.newInstanceIfPossible(aClass);
String urlPath = "/api" + path.value();
server.addFilter(new HttpFilter() {
@Override
public void doFilter(HttpServerRequest req, HttpServerResponse res, Filter.Chain chain) throws IOException {
Config config = ConfigUtil.CONFIG;
Boolean isInnerIP = config.getIsInnerIP();
server.addFilter((req, res, chain) -> {
REQUEST.set(req);
RESPONSE.set(res);
Config config = ConfigUtil.CONFIG;
Boolean isInnerIP = config.getIsInnerIP();
try {
String ip = getIp();
if (isInnerIP) {
if (!PatternPool.IPV4.matcher(ip).matches()) {
Expand All @@ -71,6 +72,9 @@ public void doFilter(HttpServerRequest req, HttpServerResponse res, Filter.Chain
}
}
chain.doFilter(req.getHttpExchange());
} finally {
REQUEST.remove();
RESPONSE.remove();
}
});
server.addAction(urlPath, new BaseAction() {
Expand All @@ -79,8 +83,6 @@ public void doFilter(HttpServerRequest req, HttpServerResponse res, Filter.Chain
@Override
public void doAction(HttpServerRequest req, HttpServerResponse res) {
try {
REQUEST.set(req);
RESPONSE.set(res);
BaseAction baseAction = (BaseAction) action;
baseAction.doAction(req, res);
} catch (Exception e) {
Expand All @@ -91,9 +93,6 @@ public void doAction(HttpServerRequest req, HttpServerResponse res) {
log.error("{} {}", urlPath, e.getMessage());
log.debug(e);
}
} finally {
REQUEST.remove();
RESPONSE.remove();
}
}
});
Expand Down

0 comments on commit 5e231b8

Please sign in to comment.