From f2b44605d0ef14e75c3289b089687af064033a00 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 2 Apr 2020 18:21:22 +0800 Subject: [PATCH] add code --- .../cn/hutool/http/server/HttpServerBase.java | 32 +++++++ ...ttpRequest.java => HttpServerRequest.java} | 8 +- .../http/server/HttpServerResponse.java | 89 +++++++++++++++++++ .../cn/hutool/http/server/SimpleServer.java | 30 ++++++- .../cn/hutool/http/server/action/Action.java | 22 +++++ .../http/server/handler/ActionHandler.java | 32 +++++++ 6 files changed, 205 insertions(+), 8 deletions(-) create mode 100644 hutool-http/src/main/java/cn/hutool/http/server/HttpServerBase.java rename hutool-http/src/main/java/cn/hutool/http/server/{HttpRequest.java => HttpServerRequest.java} (93%) create mode 100644 hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java create mode 100644 hutool-http/src/main/java/cn/hutool/http/server/action/Action.java create mode 100644 hutool-http/src/main/java/cn/hutool/http/server/handler/ActionHandler.java diff --git a/hutool-http/src/main/java/cn/hutool/http/server/HttpServerBase.java b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerBase.java new file mode 100644 index 000000000..5cf8ecb81 --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerBase.java @@ -0,0 +1,32 @@ +package cn.hutool.http.server; + +import com.sun.net.httpserver.HttpExchange; + +/** + * HttpServer公用对象,提供HttpExchange包装和公用方法 + * + * @author looly + * @since 5.2.6 + */ +public class HttpServerBase { + + final HttpExchange httpExchange; + + /** + * 构造 + * + * @param httpExchange {@link HttpExchange} + */ + public HttpServerBase(HttpExchange httpExchange) { + this.httpExchange = httpExchange; + } + + /** + * 获取{@link HttpExchange}对象 + * + * @return {@link HttpExchange}对象 + */ + public HttpExchange getHttpExchange() { + return this.httpExchange; + } +} diff --git a/hutool-http/src/main/java/cn/hutool/http/server/HttpRequest.java b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java similarity index 93% rename from hutool-http/src/main/java/cn/hutool/http/server/HttpRequest.java rename to hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java index 116b83560..58062c6aa 100644 --- a/hutool-http/src/main/java/cn/hutool/http/server/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerRequest.java @@ -28,9 +28,7 @@ import java.util.Map; * @author looly * @since 5.2.6 */ -public class HttpRequest { - - private final HttpExchange httpExchange; +public class HttpServerRequest extends HttpServerBase{ private Map cookieCache; @@ -39,8 +37,8 @@ public class HttpRequest { * * @param httpExchange {@link HttpExchange} */ - public HttpRequest(HttpExchange httpExchange) { - this.httpExchange = httpExchange; + public HttpServerRequest(HttpExchange httpExchange) { + super(httpExchange); } /** diff --git a/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java new file mode 100644 index 000000000..23fb29c58 --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/server/HttpServerResponse.java @@ -0,0 +1,89 @@ +package cn.hutool.http.server; + +import cn.hutool.core.io.IORuntimeException; +import cn.hutool.core.io.IoUtil; +import com.sun.net.httpserver.HttpExchange; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Http响应对象,用于写出数据到客户端 + */ +public class HttpServerResponse extends HttpServerBase{ + + /** + * 构造 + * + * @param httpExchange {@link HttpExchange} + */ + public HttpServerResponse(HttpExchange httpExchange) { + super(httpExchange); + } + + /** + * 发送HTTP状态码 + * + * @param httpStatusCode HTTP状态码,见HttpStatus + * @return this + */ + public HttpServerResponse send(int httpStatusCode) { + return send(httpStatusCode, 0); + } + + /** + * 发送HTTP状态码 + * + * @param httpStatusCode HTTP状态码,见HttpStatus + * @param bodyLength 响应体长度,默认0 + * @return this + */ + public HttpServerResponse send(int httpStatusCode, long bodyLength) { + try { + this.httpExchange.sendResponseHeaders(httpStatusCode, bodyLength); + } catch (IOException e) { + throw new IORuntimeException(e); + } + return this; + } + + /** + * 获取响应数据流 + * + * @return 响应数据流 + */ + public OutputStream getOut() { + return this.httpExchange.getResponseBody(); + } + + /** + * 写出数据到客户端 + * + * @param data 数据 + * @return this + */ + public HttpServerResponse write(byte[] data) { + write(new ByteArrayInputStream(data)); + return this; + } + + /** + * 写出数据到客户端 + * + * @param in 数据流 + * @return this + */ + public HttpServerResponse write(InputStream in) { + OutputStream out = null; + try { + out = getOut(); + IoUtil.copy(in, out); + } finally { + IoUtil.close(out); + IoUtil.close(in); + } + return this; + } +} diff --git a/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java b/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java index 90c587a69..21d62fd1f 100644 --- a/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java +++ b/hutool-http/src/main/java/cn/hutool/http/server/SimpleServer.java @@ -1,6 +1,9 @@ package cn.hutool.http.server; import cn.hutool.core.io.IORuntimeException; +import cn.hutool.http.server.action.Action; +import cn.hutool.http.server.handler.ActionHandler; +import cn.hutool.http.server.handler.RootHandler; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; @@ -53,7 +56,7 @@ public class SimpleServer { /** * 增加请求处理规则 * - * @param path 路径 + * @param path 路径 * @param handler 处理器 * @return this */ @@ -62,6 +65,27 @@ public class SimpleServer { return this; } + /** + * 设置根目录,默认的页面从root目录中读取解析返回 + * + * @param root 路径 + * @return this + */ + public SimpleServer setRoot(String root) { + return addHandler("/", new RootHandler(root)); + } + + /** + * 增加请求处理规则 + * + * @param path 路径 + * @param action 处理器 + * @return this + */ + public SimpleServer addAction(String path, Action action) { + return addHandler(path, new ActionHandler(action)); + } + /** * 设置自定义线程池 * @@ -78,7 +102,7 @@ public class SimpleServer { * * @return {@link HttpServer} */ - public HttpServer getRawServer(){ + public HttpServer getRawServer() { return this.server; } @@ -87,7 +111,7 @@ public class SimpleServer { * * @return {@link InetSocketAddress} */ - public InetSocketAddress getAddress(){ + public InetSocketAddress getAddress() { return this.server.getAddress(); } diff --git a/hutool-http/src/main/java/cn/hutool/http/server/action/Action.java b/hutool-http/src/main/java/cn/hutool/http/server/action/Action.java new file mode 100644 index 000000000..2ef304eef --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/server/action/Action.java @@ -0,0 +1,22 @@ +package cn.hutool.http.server.action; + +import cn.hutool.http.server.HttpServerRequest; +import cn.hutool.http.server.HttpServerResponse; + +/** + * 请求处理接口
+ * 当用户请求某个Path,则调用相应Action的doAction方法 + * + * @author Looly + * @since 5.2.6 + */ +public interface Action { + + /** + * 处理请求 + * + * @param request 请求对象 + * @param response 响应对象 + */ + void doAction(HttpServerRequest request, HttpServerResponse response); +} diff --git a/hutool-http/src/main/java/cn/hutool/http/server/handler/ActionHandler.java b/hutool-http/src/main/java/cn/hutool/http/server/handler/ActionHandler.java new file mode 100644 index 000000000..94a51d004 --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/server/handler/ActionHandler.java @@ -0,0 +1,32 @@ +package cn.hutool.http.server.handler; + +import cn.hutool.http.server.HttpServerRequest; +import cn.hutool.http.server.HttpServerResponse; +import cn.hutool.http.server.action.Action; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; + +/** + * Action处理器,用于将HttpHandler转换为Action形式 + * + * @author looly + * @since 5.2.6 + */ +public class ActionHandler implements HttpHandler { + + private final Action action; + + /** + * 构造 + * + * @param action Action + */ + public ActionHandler(Action action) { + this.action = action; + } + + @Override + public void handle(HttpExchange httpExchange) { + action.doAction(new HttpServerRequest(httpExchange), new HttpServerResponse(httpExchange)); + } +}