This commit is contained in:
Looly 2024-11-18 13:52:58 +08:00
parent 53e36eff3e
commit 28fff1fbac
7 changed files with 302 additions and 6 deletions

View File

@ -39,7 +39,8 @@
<okhttp.version>4.12.0</okhttp.version>
<!-- 固定 2.2.x支持到JDK8 -->
<undertow.version>2.2.36.Final</undertow.version>
<jetty.version>12.0.15</jetty.version>
<!-- 9.x支持到JDK8 -->
<jetty.version>9.4.56.v20240826</jetty.version>
<tomcat.version>11.0.1</tomcat.version>
</properties>

View File

@ -0,0 +1,124 @@
/*
* 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.jetty;
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.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import javax.net.ssl.SSLContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Jetty引擎实现
*
* @author Looly
*/
public class Jetty9Engine extends AbstractServerEngine {
private Server server;
/**
* 构造
*/
public Jetty9Engine() {
// issue#IABWBL JDK8下在IDEA旗舰版加载Spring boot插件时启动应用不会检查字段类是否存在
// 此处构造时调用下这个类以便触发类是否存在的检查
Assert.notNull(Server.class);
}
@Override
public void start() {
initEngine();
try {
this.server.start();
this.server.join();
} catch (final Exception e) {
throw new HttpException(e);
}
}
@Override
public Server getRawEngine() {
return this.server;
}
@Override
protected void reset() {
if (null != this.server) {
this.server.destroy();
this.server = null;
}
}
@Override
protected void initEngine() {
if (null != this.server) {
return;
}
final ServerConfig config = this.config;
final Server server = new Server();
server.addConnector(createConnector(server, config));
server.setHandler(new AbstractHandler() {
@Override
public void handle(final String target, final Request baseRequest,
final HttpServletRequest request, final HttpServletResponse response) {
handler.handle(new Jetty9Request(request), new Jetty9Response(response));
}
});
this.server = server;
}
/**
* 创建连接器
*
* @param server 服务器
* @param config 配置
* @return 连接器
*/
private ServerConnector createConnector(final Server server, final ServerConfig config) {
final ServerConnector connector;
// 配置
final HttpConfiguration configuration = new HttpConfiguration();
final HttpConnectionFactory httpFactory = new HttpConnectionFactory(configuration);
final SSLContext sslContext = config.getSslContext();
if (null != sslContext) {
// 创建HTTPS连接器
final SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setSslContext(sslContext);
final SslConnectionFactory connectionFactory = new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString());
connector = new ServerConnector(server, connectionFactory, httpFactory);
} else {
// 创建HTTP连接器
connector = new ServerConnector(server, httpFactory);
}
connector.setHost(config.getHost());
connector.setPort(config.getPort());
return connector;
}
}

View File

@ -0,0 +1,71 @@
/*
* 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.jetty;
import org.dromara.hutool.http.server.ServerRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
/**
* Jetty请求对象包装
*
* @author Looly
*/
public class Jetty9Request implements ServerRequest {
private final HttpServletRequest request;
/**
* 构造
*
* @param request Jetty请求对象
*/
public Jetty9Request(final HttpServletRequest request) {
this.request = request;
}
@Override
public String getMethod() {
return this.request.getMethod();
}
@Override
public String getPath() {
return this.request.getContextPath();
}
@Override
public String getQuery() {
return this.request.getQueryString();
}
@Override
public String getHeader(final String name) {
return this.request.getHeader(name);
}
@Override
public InputStream getBodyStream() {
try {
return this.request.getInputStream();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}

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.jetty;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.http.server.ServerResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
/**
* Jetty响应对象包装
*
* @author Looly
*/
public class Jetty9Response implements ServerResponse {
private final HttpServletResponse response;
private Charset charset;
/**
* 构造
*
* @param response Jetty响应对象
*/
public Jetty9Response(final HttpServletResponse response) {
this.response = response;
}
@Override
public Jetty9Response setStatus(final int statusCode) {
this.response.setStatus(statusCode);
return this;
}
@Override
public Jetty9Response setCharset(final Charset charset) {
this.charset = charset;
return this;
}
@Override
public Charset getCharset() {
return this.charset;
}
@Override
public Jetty9Response addHeader(final String header, final String value) {
this.response.addHeader(header, value);
return this;
}
@Override
public Jetty9Response setHeader(final String header, final String value) {
this.response.setHeader(header, value);
return this;
}
@Override
public OutputStream getOutputStream() {
try {
return this.response.getOutputStream();
} catch (final IOException e) {
throw new IORuntimeException(e);
}
}
}

View File

@ -15,7 +15,7 @@
*/
/**
* Jetty引擎实现
* Jetty9引擎实现
*
* @author Looly
*/

View File

@ -0,0 +1,17 @@
package org.dromara.hutool.http.server.engine;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.http.server.ServerConfig;
import org.dromara.hutool.http.server.engine.jetty.Jetty9Engine;
public class JettyTest {
public static void main(String[] args) {
final Jetty9Engine engine = new Jetty9Engine();
engine.init(ServerConfig.of());
engine.setHandler((request, response) -> {
Console.log(request.getPath());
response.write("Hutool Jetty response test");
});
engine.start();
}
}

View File

@ -22,12 +22,12 @@ import org.dromara.hutool.http.server.engine.undertow.UndertowEngine;
public class UndertowTest {
public static void main(String[] args) {
final UndertowEngine undertowEngine = new UndertowEngine();
undertowEngine.init(ServerConfig.of());
undertowEngine.setHandler((request, response) -> {
final UndertowEngine engine = new UndertowEngine();
engine.init(ServerConfig.of());
engine.setHandler((request, response) -> {
Console.log(request.getPath());
response.write("Hutool Undertow response test");
});
undertowEngine.start();
engine.start();
}
}