From 53419903c1ad373c6cb08219ed7b58c419fbcf08 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 28 Sep 2023 18:54:23 +0800 Subject: [PATCH] add ExceptionFilter --- .../hutool/http/server/SimpleServer.java | 11 +---- .../server/filter/DefaultExceptionFilter.java | 37 ++++++++++++++++ .../http/server/filter/ExceptionFilter.java | 43 +++++++++++++++++++ .../org/dromara/hutool/http/Issue444Test.java | 28 ++++++++++++ .../http/server/ExceptionServerTest.java | 29 +++++++++++++ 5 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 hutool-http/src/main/java/org/dromara/hutool/http/server/filter/DefaultExceptionFilter.java create mode 100644 hutool-http/src/main/java/org/dromara/hutool/http/server/filter/ExceptionFilter.java create mode 100644 hutool-http/src/test/java/org/dromara/hutool/http/Issue444Test.java create mode 100644 hutool-http/src/test/java/org/dromara/hutool/http/server/ExceptionServerTest.java diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/server/SimpleServer.java b/hutool-http/src/main/java/org/dromara/hutool/http/server/SimpleServer.java index e21bf95bb..019788b42 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/server/SimpleServer.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/server/SimpleServer.java @@ -12,24 +12,17 @@ package org.dromara.hutool.http.server; +import com.sun.net.httpserver.*; import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.core.lang.Console; -import org.dromara.hutool.core.thread.GlobalThreadPool; import org.dromara.hutool.core.text.StrUtil; +import org.dromara.hutool.core.thread.GlobalThreadPool; import org.dromara.hutool.http.server.action.Action; import org.dromara.hutool.http.server.action.RootAction; import org.dromara.hutool.http.server.filter.HttpFilter; import org.dromara.hutool.http.server.filter.SimpleFilter; import org.dromara.hutool.http.server.handler.ActionHandler; -import com.sun.net.httpserver.Filter; -import com.sun.net.httpserver.HttpContext; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpServer; -import com.sun.net.httpserver.HttpsConfigurator; -import com.sun.net.httpserver.HttpsServer; - import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/server/filter/DefaultExceptionFilter.java b/hutool-http/src/main/java/org/dromara/hutool/http/server/filter/DefaultExceptionFilter.java new file mode 100644 index 000000000..0724a0be9 --- /dev/null +++ b/hutool-http/src/main/java/org/dromara/hutool/http/server/filter/DefaultExceptionFilter.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023. looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.http.server.filter; + +import org.dromara.hutool.core.exception.ExceptionUtil; +import org.dromara.hutool.core.text.StrUtil; +import org.dromara.hutool.http.server.HttpServerRequest; +import org.dromara.hutool.http.server.HttpServerResponse; + +/** + * 默认异常处理拦截器 + * + * @author looly + */ +public class DefaultExceptionFilter extends ExceptionFilter{ + + private final static String TEMPLATE_ERROR = "Hutool - Error report

HTTP Status {} - {}


{}


Hutool

"; + + @Override + public void afterException(final HttpServerRequest req, final HttpServerResponse res, final Throwable e) { + String content = ExceptionUtil.stacktraceToString(e); + content = content.replace("\n", "
\n"); + content = StrUtil.format(TEMPLATE_ERROR, 500, req.getURI(), content); + + res.sendError(500, content); + } +} diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/server/filter/ExceptionFilter.java b/hutool-http/src/main/java/org/dromara/hutool/http/server/filter/ExceptionFilter.java new file mode 100644 index 000000000..e3d215ba1 --- /dev/null +++ b/hutool-http/src/main/java/org/dromara/hutool/http/server/filter/ExceptionFilter.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023. looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.http.server.filter; + +import com.sun.net.httpserver.Filter; +import org.dromara.hutool.http.server.HttpServerRequest; +import org.dromara.hutool.http.server.HttpServerResponse; + +/** + * 异常处理过滤器 + * + * @author looly + */ +public abstract class ExceptionFilter implements HttpFilter { + + @Override + public void doFilter(final HttpServerRequest req, final HttpServerResponse res, final Filter.Chain chain) { + try { + chain.doFilter(req.getHttpExchange()); + } catch (final Throwable e) { + afterException(req, res, e); + } + } + + /** + * 异常之后的处理逻辑 + * + * @param req {@link HttpServerRequest} + * @param res {@link HttpServerResponse} + * @param e 异常 + */ + public abstract void afterException(final HttpServerRequest req, final HttpServerResponse res, final Throwable e); +} diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/Issue444Test.java b/hutool-http/src/test/java/org/dromara/hutool/http/Issue444Test.java new file mode 100644 index 000000000..96c85a9e2 --- /dev/null +++ b/hutool-http/src/test/java/org/dromara/hutool/http/Issue444Test.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023. looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.http; + +import org.dromara.hutool.core.lang.Console; +import org.dromara.hutool.http.client.Request; +import org.dromara.hutool.http.client.engine.jdk.JdkClientEngine; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +public class Issue444Test { + @Test + @Disabled + void getTest() { + final String s = Request.of("http://localhost:8888").send(new JdkClientEngine()).bodyStr(); + Console.log(s); + } +} diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/server/ExceptionServerTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/server/ExceptionServerTest.java new file mode 100644 index 000000000..ad6cd6ff6 --- /dev/null +++ b/hutool-http/src/test/java/org/dromara/hutool/http/server/ExceptionServerTest.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.http.server; + +import org.dromara.hutool.http.HttpUtil; +import org.dromara.hutool.http.server.filter.DefaultExceptionFilter; + +import java.io.IOException; + +public class ExceptionServerTest { + public static void main(final String[] args) { + HttpUtil.createServer(8888) + .addFilter(new DefaultExceptionFilter()) + .addAction("/", (req, res) -> { + throw new IOException("Test Exception"); + }) + .start(); + } +}