diff --git a/hutool-http/pom.xml b/hutool-http/pom.xml index 911e1364b..8b73a1176 100755 --- a/hutool-http/pom.xml +++ b/hutool-http/pom.xml @@ -39,7 +39,8 @@ 4.12.0 2.2.36.Final - 12.0.15 + + 9.4.56.v20240826 11.0.1 diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/Jetty9Engine.java b/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/Jetty9Engine.java new file mode 100644 index 000000000..f160c0570 --- /dev/null +++ b/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/Jetty9Engine.java @@ -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; + } +} diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/Jetty9Request.java b/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/Jetty9Request.java new file mode 100644 index 000000000..7add8af91 --- /dev/null +++ b/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/Jetty9Request.java @@ -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); + } + } +} diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/Jetty9Response.java b/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/Jetty9Response.java new file mode 100644 index 000000000..293902e16 --- /dev/null +++ b/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/Jetty9Response.java @@ -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); + } + } +} diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/package-info.java b/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/package-info.java index 8675fed66..da119eea2 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/package-info.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/server/engine/jetty/package-info.java @@ -15,7 +15,7 @@ */ /** - * Jetty引擎实现 + * Jetty9引擎实现 * * @author Looly */ diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/server/engine/JettyTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/server/engine/JettyTest.java new file mode 100644 index 000000000..6445182df --- /dev/null +++ b/hutool-http/src/test/java/org/dromara/hutool/http/server/engine/JettyTest.java @@ -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(); + } +} diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/server/engine/UndertowTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/server/engine/UndertowTest.java index 0e5c34207..d9e82d043 100644 --- a/hutool-http/src/test/java/org/dromara/hutool/http/server/engine/UndertowTest.java +++ b/hutool-http/src/test/java/org/dromara/hutool/http/server/engine/UndertowTest.java @@ -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(); } }