mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
RouteHttpHandler
This commit is contained in:
parent
ae7e3b76cb
commit
0ead90d433
@ -20,7 +20,10 @@ import org.dromara.hutool.http.server.ServerConfig;
|
|||||||
import org.dromara.hutool.http.server.handler.HttpHandler;
|
import org.dromara.hutool.http.server.handler.HttpHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP服务器引擎
|
* HTTP服务器引擎,执行流程为:
|
||||||
|
* <pre>{@code
|
||||||
|
* init -> setHandler -> start
|
||||||
|
* }</pre>
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
|
@ -82,6 +82,7 @@ public class SimpleServer {
|
|||||||
.setPort(address.getPort())
|
.setPort(address.getPort())
|
||||||
.setSslContext(sslContext);
|
.setSslContext(sslContext);
|
||||||
this.engine.init(serverConfig);
|
this.engine.init(serverConfig);
|
||||||
|
this.engine.initEngine();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -140,35 +140,45 @@ public class SunHttpServerEngine extends AbstractServerEngine {
|
|||||||
this.server.stop(0);
|
this.server.stop(0);
|
||||||
this.server = null;
|
this.server = null;
|
||||||
}
|
}
|
||||||
|
this.server = createServer(this.config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initEngine() {
|
protected void initEngine() {
|
||||||
final ServerConfig config = this.config;
|
// 请求处理器
|
||||||
|
createContext("/", exchange -> handler.handle(
|
||||||
|
new SunServerRequest(exchange),
|
||||||
|
new SunServerResponse(exchange)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建{@link HttpServer}
|
||||||
|
*
|
||||||
|
* @param config {@link ServerConfig}
|
||||||
|
* @return {@link HttpServer}
|
||||||
|
*/
|
||||||
|
private static HttpServer createServer(final ServerConfig config){
|
||||||
|
final HttpServer server;
|
||||||
// SSL
|
// SSL
|
||||||
final InetSocketAddress address = new InetSocketAddress(config.getHost(), config.getPort());
|
final InetSocketAddress address = new InetSocketAddress(config.getHost(), config.getPort());
|
||||||
final SSLContext sslContext = config.getSslContext();
|
final SSLContext sslContext = config.getSslContext();
|
||||||
try {
|
try {
|
||||||
if (null != sslContext) {
|
if (null != sslContext) {
|
||||||
final HttpsServer server = HttpsServer.create(address, 0);
|
final HttpsServer httpsServer = HttpsServer.create(address, 0);
|
||||||
server.setHttpsConfigurator(new HttpsConfigurator(sslContext));
|
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
|
||||||
this.server = server;
|
server = httpsServer;
|
||||||
} else {
|
} else {
|
||||||
this.server = HttpServer.create(address, 0);
|
server = HttpServer.create(address, 0);
|
||||||
}
|
}
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 线程池和连接配置
|
// 线程池和连接配置
|
||||||
setExecutor(createExecutor(config));
|
server.setExecutor(createExecutor(config));
|
||||||
|
|
||||||
// 请求处理器
|
return server;
|
||||||
createContext("/", exchange -> handler.handle(
|
|
||||||
new SunServerRequest(exchange),
|
|
||||||
new SunServerResponse(exchange)
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -0,0 +1,117 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024 Hutool Team and hutool.cn
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dromara.hutool.http.server.handler;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.text.StrUtil;
|
||||||
|
import org.dromara.hutool.core.text.split.SplitUtil;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 路由处理器<br>
|
||||||
|
* 根据请求的路径精确匹配路由,并调用对应的处理器
|
||||||
|
*
|
||||||
|
* @author Looly
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public class PathTrie {
|
||||||
|
|
||||||
|
private final Node root;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*/
|
||||||
|
public PathTrie() {
|
||||||
|
root = new Node();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加路由
|
||||||
|
*
|
||||||
|
* @param path 路径
|
||||||
|
* @param handler 处理器
|
||||||
|
*/
|
||||||
|
public void add(final String path, final HttpHandler handler) {
|
||||||
|
Node node = root;
|
||||||
|
final List<String> parts = SplitUtil.splitTrim(path, StrUtil.SLASH);
|
||||||
|
for (final String part : parts) {
|
||||||
|
node = node.getOrCreateChildren(part);
|
||||||
|
}
|
||||||
|
node.isEndOfPath = true;
|
||||||
|
node.handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找匹配的处理器,采用最长匹配模式,即:<br>
|
||||||
|
* 传入"a/b/c",存在"a/b/c",则直接匹配,否则匹配"a/b",否则匹配"a"
|
||||||
|
*
|
||||||
|
* @param path 路径
|
||||||
|
* @return 处理器
|
||||||
|
*/
|
||||||
|
public HttpHandler match(final String path) {
|
||||||
|
Node matchedNode = null;
|
||||||
|
Node node = root;
|
||||||
|
final List<String> parts = SplitUtil.splitTrim(path, StrUtil.SLASH);
|
||||||
|
for (final String part : parts) {
|
||||||
|
node = node.getChildren(part);
|
||||||
|
if (node == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(node.isEndOfPath){
|
||||||
|
matchedNode = node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null == matchedNode ? null : matchedNode.handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Node {
|
||||||
|
Map<String, Node> children;
|
||||||
|
boolean isEndOfPath;
|
||||||
|
HttpHandler handler;
|
||||||
|
|
||||||
|
public Node() {
|
||||||
|
isEndOfPath = false;
|
||||||
|
handler = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取子节点
|
||||||
|
*
|
||||||
|
* @param part 节点标识
|
||||||
|
* @return 子节点
|
||||||
|
*/
|
||||||
|
public Node getChildren(final String part) {
|
||||||
|
return null == children ? null : children.get(part);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取或创建子节点
|
||||||
|
*
|
||||||
|
* @param part 节点标识
|
||||||
|
* @return 子节点
|
||||||
|
*/
|
||||||
|
public Node getOrCreateChildren(final String part) {
|
||||||
|
if(null == children){
|
||||||
|
children = new HashMap<>();
|
||||||
|
}
|
||||||
|
return children.computeIfAbsent(part, c -> new Node());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024 Hutool Team and hutool.cn
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dromara.hutool.http.server.handler;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.lang.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 路由处理器<br>
|
||||||
|
* 根据请求的路径精确匹配路由,并调用对应的处理器,如果没有定义处理器,使用默认处理器
|
||||||
|
*
|
||||||
|
* @author Looly
|
||||||
|
* @since 6.0.0
|
||||||
|
*/
|
||||||
|
public class RouteHttpHandler implements HttpHandler {
|
||||||
|
|
||||||
|
private final PathTrie pathTrie;
|
||||||
|
private final HttpHandler defaultHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param defaultHandler 默认处理器
|
||||||
|
*/
|
||||||
|
public RouteHttpHandler(final HttpHandler defaultHandler) {
|
||||||
|
this.pathTrie = new PathTrie();
|
||||||
|
this.defaultHandler = Assert.notNull(defaultHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加路由
|
||||||
|
*
|
||||||
|
* @param path 路径
|
||||||
|
* @param handler 处理器
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public RouteHttpHandler route(final String path, final HttpHandler handler) {
|
||||||
|
if (null != handler) {
|
||||||
|
pathTrie.add(path, handler);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(final ServerRequest request, final ServerResponse response) {
|
||||||
|
final String path = request.getPath();
|
||||||
|
final HttpHandler handler = pathTrie.match(path);
|
||||||
|
if (null != handler) {
|
||||||
|
handler.handle(request, response);
|
||||||
|
} else {
|
||||||
|
// 没有path匹配,使用默认处理器
|
||||||
|
defaultHandler.handle(request, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,7 +23,6 @@ import org.dromara.hutool.http.HttpUtil;
|
|||||||
import org.dromara.hutool.http.meta.ContentType;
|
import org.dromara.hutool.http.meta.ContentType;
|
||||||
import org.dromara.hutool.http.meta.HeaderName;
|
import org.dromara.hutool.http.meta.HeaderName;
|
||||||
import org.dromara.hutool.http.multipart.UploadFile;
|
import org.dromara.hutool.http.multipart.UploadFile;
|
||||||
import org.dromara.hutool.http.server.engine.sun.SunServerRequest;
|
|
||||||
import org.dromara.hutool.json.JSONUtil;
|
import org.dromara.hutool.json.JSONUtil;
|
||||||
|
|
||||||
import java.net.HttpCookie;
|
import java.net.HttpCookie;
|
||||||
@ -40,7 +39,7 @@ public class SimpleServerTest {
|
|||||||
.setRoot(FileUtil.file("html"))
|
.setRoot(FileUtil.file("html"))
|
||||||
// get数据测试,返回请求的PATH
|
// get数据测试,返回请求的PATH
|
||||||
.addAction("/get", (request, response) ->
|
.addAction("/get", (request, response) ->
|
||||||
response.write(((SunServerRequest)request).getURI().toString(), ContentType.TEXT_PLAIN.toString())
|
response.write(request.getPath(), ContentType.TEXT_PLAIN.toString())
|
||||||
)
|
)
|
||||||
// 返回JSON数据测试
|
// 返回JSON数据测试
|
||||||
.addAction("/restTest", (request, response) -> {
|
.addAction("/restTest", (request, response) -> {
|
||||||
|
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024 Hutool Team and hutool.cn
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.dromara.hutool.http.server.handler;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.lang.Console;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class PathTrieTest {
|
||||||
|
@Test
|
||||||
|
public void testPathTrie() {
|
||||||
|
final PathTrie trie = new PathTrie();
|
||||||
|
|
||||||
|
// 添加一些路径和对应的处理器
|
||||||
|
trie.add("/user", (req, res) -> Console.log("User handler"));
|
||||||
|
trie.add("/user/profile", (req, res) -> Console.log("Profile handler"));
|
||||||
|
|
||||||
|
// 测试精确匹配
|
||||||
|
assertNotNull(trie.match("/user"));
|
||||||
|
// 匹配父路径
|
||||||
|
assertNotNull(trie.match("/user/test1"));
|
||||||
|
// 匹配最近的上级路径
|
||||||
|
assertNotNull(trie.match("/user/test1/test2"));
|
||||||
|
|
||||||
|
// 自动忽略空路径,尾部的/也忽略
|
||||||
|
assertNotNull(trie.match("/user/profile"));
|
||||||
|
assertNotNull(trie.match("/user/profile/"));
|
||||||
|
assertNotNull(trie.match("/user////profile/"));
|
||||||
|
|
||||||
|
// 测试不存在的路径
|
||||||
|
assertNull(trie.match("/nonexistent"));
|
||||||
|
assertNull(trie.match("/"));
|
||||||
|
assertNull(trie.match(""));
|
||||||
|
assertNull(trie.match(null));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user