add ExceptionFilter

This commit is contained in:
Looly 2023-09-28 18:54:23 +08:00
parent 0c3f91bad3
commit 53419903c1
5 changed files with 139 additions and 9 deletions

View File

@ -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;

View File

@ -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 = "<!DOCTYPE html><html><head><title>Hutool - Error report</title><style>h1,h3 {color:white; background-color: gray;}</style></head><body><h1>HTTP Status {} - {}</h1><hr size=\"1\" noshade=\"noshade\" /><p>{}</p><hr size=\"1\" noshade=\"noshade\" /><h3>Hutool</h3></body></html>";
@Override
public void afterException(final HttpServerRequest req, final HttpServerResponse res, final Throwable e) {
String content = ExceptionUtil.stacktraceToString(e);
content = content.replace("\n", "<br/>\n");
content = StrUtil.format(TEMPLATE_ERROR, 500, req.getURI(), content);
res.sendError(500, content);
}
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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();
}
}