This commit is contained in:
Looly 2024-11-18 14:11:59 +08:00
parent 28fff1fbac
commit c738d783c2
15 changed files with 86 additions and 29 deletions

View File

@ -0,0 +1,49 @@
/*
* 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.handler.HttpHandler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Jetty9版本使用的Handler
*
* @author Looly
*/
public class Jetty9Handler extends AbstractHandler {
private final HttpHandler handler;
/**
* 构造
*
* @param handler 处理器
*/
public Jetty9Handler(final HttpHandler handler) {
this.handler = handler;
}
@Override
public void handle(final String target, final Request baseRequest,
final HttpServletRequest request, final HttpServletResponse response) {
handler.handle(new Jetty9Request(request), new Jetty9Response(response));
}
}

View File

@ -16,7 +16,7 @@
package org.dromara.hutool.http.server.engine.jetty; package org.dromara.hutool.http.server.engine.jetty;
import org.dromara.hutool.http.server.ServerRequest; import org.dromara.hutool.http.server.handler.ServerRequest;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;

View File

@ -17,7 +17,7 @@
package org.dromara.hutool.http.server.engine.jetty; package org.dromara.hutool.http.server.engine.jetty;
import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.http.server.ServerResponse; import org.dromara.hutool.http.server.handler.ServerResponse;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;

View File

@ -17,36 +17,49 @@
package org.dromara.hutool.http.server.engine.jetty; package org.dromara.hutool.http.server.engine.jetty;
import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.HttpException; import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.server.ServerConfig; import org.dromara.hutool.http.server.ServerConfig;
import org.dromara.hutool.http.server.engine.AbstractServerEngine; import org.dromara.hutool.http.server.engine.AbstractServerEngine;
import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.*; import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** /**
* Jetty引擎实现 * Jetty引擎实现
* *
* @author Looly * @author Looly
*/ */
public class Jetty9Engine extends AbstractServerEngine { public class JettyEngine extends AbstractServerEngine {
private Server server; private Server server;
/**
* 由于Jetty9和以上版本中接口实现不同此处根据不同版本做兼容自定义
*/
private Handler jettyHandler;
/** /**
* 构造 * 构造
*/ */
public Jetty9Engine() { public JettyEngine() {
// issue#IABWBL JDK8下在IDEA旗舰版加载Spring boot插件时启动应用不会检查字段类是否存在 // issue#IABWBL JDK8下在IDEA旗舰版加载Spring boot插件时启动应用不会检查字段类是否存在
// 此处构造时调用下这个类以便触发类是否存在的检查 // 此处构造时调用下这个类以便触发类是否存在的检查
Assert.notNull(Server.class); Assert.notNull(Server.class);
} }
/**
* 设置Jetty处理器用于处理请求
*
* @param jettyHandler 处理器
* @return this
*/
public JettyEngine setJettyHandler(final Handler jettyHandler) {
this.jettyHandler = jettyHandler;
return this;
}
@Override @Override
public void start() { public void start() {
initEngine(); initEngine();
@ -80,13 +93,8 @@ public class Jetty9Engine extends AbstractServerEngine {
final ServerConfig config = this.config; final ServerConfig config = this.config;
final Server server = new Server(); final Server server = new Server();
server.addConnector(createConnector(server, config)); server.addConnector(createConnector(server, config));
server.setHandler(new AbstractHandler() { server.setHandler(ObjUtil.defaultIfNull(this.jettyHandler,
@Override () -> new Jetty9Handler(this.handler)));
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; this.server = server;
} }

View File

@ -33,7 +33,7 @@ import org.dromara.hutool.http.meta.ContentTypeUtil;
import org.dromara.hutool.http.meta.HeaderName; import org.dromara.hutool.http.meta.HeaderName;
import org.dromara.hutool.http.multipart.MultipartFormData; import org.dromara.hutool.http.multipart.MultipartFormData;
import org.dromara.hutool.http.multipart.UploadSetting; import org.dromara.hutool.http.multipart.UploadSetting;
import org.dromara.hutool.http.server.ServerRequest; import org.dromara.hutool.http.server.handler.ServerRequest;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpCookie; import java.net.HttpCookie;

View File

@ -22,7 +22,7 @@ import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.http.meta.ContentType; import org.dromara.hutool.http.meta.ContentType;
import org.dromara.hutool.http.meta.HttpStatus; import org.dromara.hutool.http.meta.HttpStatus;
import org.dromara.hutool.http.server.ServerResponse; import org.dromara.hutool.http.server.handler.ServerResponse;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;

View File

@ -19,7 +19,7 @@ package org.dromara.hutool.http.server.engine.undertow;
import io.undertow.server.HttpServerExchange; import io.undertow.server.HttpServerExchange;
import io.undertow.util.HeaderMap; import io.undertow.util.HeaderMap;
import org.dromara.hutool.core.util.CharsetUtil; import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.http.server.ServerRequest; import org.dromara.hutool.http.server.handler.ServerRequest;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;

View File

@ -19,7 +19,7 @@ package org.dromara.hutool.http.server.engine.undertow;
import io.undertow.io.Sender; import io.undertow.io.Sender;
import io.undertow.server.HttpServerExchange; import io.undertow.server.HttpServerExchange;
import io.undertow.util.HttpString; import io.undertow.util.HttpString;
import org.dromara.hutool.http.server.ServerResponse; import org.dromara.hutool.http.server.handler.ServerResponse;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;

View File

@ -16,11 +16,9 @@
package org.dromara.hutool.http.server.handler; package org.dromara.hutool.http.server.handler;
import org.dromara.hutool.http.server.ServerRequest;
import org.dromara.hutool.http.server.ServerResponse;
/** /**
* HTTP请求处理器 * HTTP请求处理器<br>
* 抽象请求处理对于不同的HTTP服务器将这个处理器封装成对应的处理器例如Jetty的JettyHandlerUndertow的UndertowHandler等
* *
* @author Looly * @author Looly
*/ */

View File

@ -19,8 +19,6 @@ package org.dromara.hutool.http.server.handler;
import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.http.meta.HttpStatus; import org.dromara.hutool.http.meta.HttpStatus;
import org.dromara.hutool.http.server.ServerRequest;
import org.dromara.hutool.http.server.ServerResponse;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.dromara.hutool.http.server; package org.dromara.hutool.http.server.handler;
import org.dromara.hutool.core.io.IORuntimeException; import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.io.IoUtil;

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.dromara.hutool.http.server; package org.dromara.hutool.http.server.handler;
import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.io.file.FileUtil;

View File

@ -15,6 +15,8 @@
*/ */
/** /**
* {@link com.sun.net.httpserver.HttpHandler} 实现包装 * HTTP服务器请求和响应处理器的统一封装
*
* @author Looly
*/ */
package org.dromara.hutool.http.server.handler; package org.dromara.hutool.http.server.handler;

View File

@ -21,6 +21,8 @@ import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.map.multi.ListValueMap; import org.dromara.hutool.core.map.multi.ListValueMap;
import org.dromara.hutool.http.HttpUtil; import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.server.engine.sun.SimpleServer; import org.dromara.hutool.http.server.engine.sun.SimpleServer;
import org.dromara.hutool.http.server.handler.ServerRequest;
import org.dromara.hutool.http.server.handler.ServerResponse;
/** /**
* http://localhost:8888/?name=hutool * http://localhost:8888/?name=hutool

View File

@ -2,11 +2,11 @@ package org.dromara.hutool.http.server.engine;
import org.dromara.hutool.core.lang.Console; import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.http.server.ServerConfig; import org.dromara.hutool.http.server.ServerConfig;
import org.dromara.hutool.http.server.engine.jetty.Jetty9Engine; import org.dromara.hutool.http.server.engine.jetty.JettyEngine;
public class JettyTest { public class JettyTest {
public static void main(String[] args) { public static void main(String[] args) {
final Jetty9Engine engine = new Jetty9Engine(); final JettyEngine engine = new JettyEngine();
engine.init(ServerConfig.of()); engine.init(ServerConfig.of());
engine.setHandler((request, response) -> { engine.setHandler((request, response) -> {
Console.log(request.getPath()); Console.log(request.getPath());