add smart http support

This commit is contained in:
Looly
2024-12-25 14:20:24 +08:00
parent 2af90ebaa5
commit 7f12d45b4e
15 changed files with 362 additions and 219 deletions

View File

@@ -43,6 +43,7 @@
<jetty.version>9.4.56.v20240826</jetty.version>
<!-- 固定 9.x支持到JDK8 -->
<tomcat.version>9.0.97</tomcat.version>
<smartboot.version>1.4.3</smartboot.version>
</properties>
<dependencies>
@@ -131,6 +132,12 @@
<version>${tomcat.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.smartboot.http</groupId>
<artifactId>smart-http-server</artifactId>
<version>${smartboot.version}</version>
<scope>provided</scope>
</dependency>
<!-- 仅用于测试 -->
<dependency>

View File

@@ -0,0 +1,83 @@
/*
* 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.engine.smart;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.http.server.handler.ServerRequest;
import org.smartboot.http.server.HttpRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
/**
* SmartHttp请求对象
*
* @author looly
* @since 6.0.0
*/
public class SmartHttpRequest implements ServerRequest {
private final HttpRequest request;
/**
* 构造
*
* @param request 请求对象
*/
public SmartHttpRequest(final HttpRequest request) {
this.request = request;
}
@Override
public String getMethod() {
return request.getMethod();
}
@Override
public String getPath() {
return request.getRequestURI();
}
@Override
public String getQuery() {
return request.getQueryString();
}
@Override
public String getHeader(final String name) {
return request.getHeader(name);
}
/**
* 获取所有Header名称
*
* @return 所有Header名称
*/
public Collection<String> getHeaderNames() {
return request.getHeaderNames();
}
@Override
public InputStream getBodyStream() {
try {
return request.getInputStream();
} catch (final IOException e) {
throw new IORuntimeException(e);
}
}
}

View File

@@ -0,0 +1,79 @@
/*
* 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.engine.smart;
import org.dromara.hutool.http.server.handler.ServerResponse;
import org.smartboot.http.common.enums.HttpStatus;
import org.smartboot.http.server.HttpResponse;
import java.io.OutputStream;
import java.nio.charset.Charset;
/**
* SmartHttp响应对象
*
* @author Looly
* @since 6.0.0
*/
public class SmartHttpResponse implements ServerResponse {
private final HttpResponse response;
private Charset charset;
/**
* 构造
*
* @param response 响应对象
*/
public SmartHttpResponse(final HttpResponse response) {
this.response = response;
}
@Override
public SmartHttpResponse setStatus(final int statusCode) {
response.setHttpStatus(HttpStatus.valueOf(statusCode));
return this;
}
@Override
public SmartHttpResponse setCharset(final Charset charset) {
this.charset = charset;
return this;
}
@Override
public Charset getCharset() {
return this.charset;
}
@Override
public SmartHttpResponse addHeader(final String header, final String value) {
this.response.addHeader(header, value);
return this;
}
@Override
public SmartHttpResponse setHeader(final String header, final String value) {
this.response.setHeader(header, value);
return this;
}
@Override
public OutputStream getOutputStream() {
return this.response.getOutputStream();
}
}

View File

@@ -0,0 +1,112 @@
/*
* 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.engine.smart;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.server.ServerConfig;
import org.dromara.hutool.http.server.engine.AbstractServerEngine;
import org.smartboot.http.server.*;
import org.smartboot.http.server.impl.Request;
import org.smartboot.socket.extension.plugins.SslPlugin;
import javax.net.ssl.SSLContext;
/**
* smart-http-server引擎
*
* @author looly
* @since 6.0.0
*/
public class SmartHttpServerEngine extends AbstractServerEngine {
private HttpBootstrap bootstrap;
/**
* 构造
*/
public SmartHttpServerEngine() {
// issue#IABWBL JDK8下在IDEA旗舰版加载Spring boot插件时启动应用不会检查字段类是否存在
// 此处构造时调用下这个类,以便触发类是否存在的检查
Assert.notNull(HttpBootstrap.class);
}
@Override
public void start() {
initEngine();
bootstrap.start();
}
@Override
public HttpBootstrap getRawEngine() {
return this.bootstrap;
}
@Override
protected void reset() {
if(null != this.bootstrap){
this.bootstrap.shutdown();
this.bootstrap = null;
}
}
@Override
protected void initEngine() {
if (null != this.bootstrap) {
return;
}
final HttpBootstrap bootstrap = new HttpBootstrap();
final HttpServerConfiguration configuration = bootstrap.configuration();
final ServerConfig config = this.config;
configuration.host(config.getHost());
// SSL
final SSLContext sslContext = config.getSslContext();
if(null != sslContext){
final SslPlugin<Request> sslPlugin;
try {
sslPlugin = new SslPlugin<>(() -> sslContext);
} catch (final Exception e) {
throw new HttpException(e);
}
configuration.addPlugin(sslPlugin);
}
// 选项
final int coreThreads = config.getCoreThreads();
if(coreThreads > 0){
configuration.threadNum(coreThreads);
}
final long idleTimeout = config.getIdleTimeout();
if(idleTimeout > 0){
configuration.setHttpIdleTimeout((int) idleTimeout);
}
bootstrap.httpHandler(new HttpServerHandler() {
@Override
public void handle(final HttpRequest request, final HttpResponse response) {
handler.handle(new SmartHttpRequest(request), new SmartHttpResponse(response));
}
});
bootstrap.setPort(config.getPort());
this.bootstrap = bootstrap;
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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.
*/
/**
* smart-http-server服务器引擎实现<br>
* 见https://smartboot.tech/smart-http/
*
* @author Looly
* @since 6.0.0
*/
package org.dromara.hutool.http.server.engine.smart;

View File

@@ -17,4 +17,5 @@
org.dromara.hutool.http.server.engine.undertow.UndertowEngine
org.dromara.hutool.http.server.engine.tomcat.TomcatEngine
org.dromara.hutool.http.server.engine.jetty.JettyEngine
org.dromara.hutool.http.server.engine.smart.SmartHttpServerEngine
org.dromara.hutool.http.server.engine.sun.SunHttpServerEngine

View File

@@ -0,0 +1,27 @@
package org.dromara.hutool.http.server.engine;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.net.ssl.SSLContextUtil;
import org.dromara.hutool.crypto.KeyStoreUtil;
import org.dromara.hutool.http.server.ServerConfig;
import javax.net.ssl.SSLContext;
import java.security.KeyStore;
public class SmartHttpServerTest {
public static void main(final String[] args) throws Exception {
final char[] pwd = "123456".toCharArray();
final KeyStore keyStore = KeyStoreUtil.readJKSKeyStore(FileUtil.file("d:/test/keystore.jks"), pwd);
// 初始化SSLContext
final SSLContext sslContext = SSLContextUtil.createSSLContext(keyStore, pwd);
final ServerEngine engine = ServerEngineFactory.createEngine("SmartHttpServer");
engine.init(ServerConfig.of().setSslContext(sslContext));
engine.setHandler((request, response) -> {
Console.log(request.getPath());
response.write("Hutool Smart-Http response test");
});
engine.start();
}
}