From b2201873ea1aab6cd32e88512396217f893fe5e4 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 2 Apr 2020 10:21:21 +0800 Subject: [PATCH] add Handler --- .../main/java/cn/hutool/core/io/FileUtil.java | 9 +- .../cn/hutool/core/io/file/FileReader.java | 27 +++--- .../main/java/cn/hutool/http/HttpUtil.java | 2 +- .../cn/hutool/http/server/SimpleServer.java | 37 +++++++- .../http/server/handler/HandlerUtil.java | 91 +++++++++++++++++++ .../http/server/handler/RootHandler.java | 45 +++++++++ .../hutool/http/server/SimpleServerTest.java | 13 +++ .../cn/hutool/http/test/SimpleServerTest.java | 21 ----- 8 files changed, 201 insertions(+), 44 deletions(-) create mode 100644 hutool-http/src/main/java/cn/hutool/http/server/handler/HandlerUtil.java create mode 100644 hutool-http/src/main/java/cn/hutool/http/server/handler/RootHandler.java create mode 100644 hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java delete mode 100644 hutool-http/src/test/java/cn/hutool/http/test/SimpleServerTest.java diff --git a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java index 852ee8aa2..9882ec1d4 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java @@ -3254,10 +3254,10 @@ public class FileUtil { * * @param file 文件 * @param out 流 - * @return 目标文件 + * @return 写出的流byte数 * @throws IORuntimeException IO异常 */ - public static File writeToStream(File file, OutputStream out) throws IORuntimeException { + public static long writeToStream(File file, OutputStream out) throws IORuntimeException { return FileReader.create(file).writeToStream(out); } @@ -3266,10 +3266,11 @@ public class FileUtil { * * @param fullFilePath 文件绝对路径 * @param out 输出流 + * @return 写出的流byte数 * @throws IORuntimeException IO异常 */ - public static void writeToStream(String fullFilePath, OutputStream out) throws IORuntimeException { - writeToStream(touch(fullFilePath), out); + public static long writeToStream(String fullFilePath, OutputStream out) throws IORuntimeException { + return writeToStream(touch(fullFilePath), out); } /** diff --git a/hutool-core/src/main/java/cn/hutool/core/io/file/FileReader.java b/hutool-core/src/main/java/cn/hutool/core/io/file/FileReader.java index b0a684241..93808ce89 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/file/FileReader.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/file/FileReader.java @@ -1,5 +1,12 @@ package cn.hutool.core.io.file; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IORuntimeException; +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.io.LineHandler; +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.StrUtil; + import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; @@ -11,13 +18,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -import cn.hutool.core.io.FileUtil; -import cn.hutool.core.io.IORuntimeException; -import cn.hutool.core.io.IoUtil; -import cn.hutool.core.io.LineHandler; -import cn.hutool.core.util.CharsetUtil; -import cn.hutool.core.util.StrUtil; - /** * 文件读取器 * @@ -249,20 +249,15 @@ public class FileReader extends FileWrapper { * 将文件写入流中 * * @param out 流 - * @return File + * @return 写出的流byte数 * @throws IORuntimeException IO异常 */ - public File writeToStream(OutputStream out) throws IORuntimeException { - FileInputStream in = null; - try { - in = new FileInputStream(file); - IoUtil.copy(in, out); + public long writeToStream(OutputStream out) throws IORuntimeException { + try (FileInputStream in = new FileInputStream(this.file)){ + return IoUtil.copy(in, out); }catch (IOException e) { throw new IORuntimeException(e); - } finally { - IoUtil.close(in); } - return this.file; } // -------------------------------------------------------------------------- Interface start diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java index 1d244b7fa..dca6c0daa 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java @@ -783,7 +783,7 @@ public class HttpUtil { * @return {@link SimpleServer} * @since 5.2.6 */ - public static SimpleServer createSimpleServer(int port){ + public static SimpleServer createServer(int port){ return new SimpleServer(port); } // ----------------------------------------------------------------------------------------- Private method start 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 f1fcc1f6e..90c587a69 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 @@ -50,18 +50,51 @@ public class SimpleServer { } } + /** + * 增加请求处理规则 + * + * @param path 路径 + * @param handler 处理器 + * @return this + */ public SimpleServer addHandler(String path, HttpHandler handler) { this.server.createContext(path, handler); return this; } + /** + * 设置自定义线程池 + * + * @param executor {@link Executor} + * @return this + */ public SimpleServer setExecutor(Executor executor) { this.server.setExecutor(executor); return this; } - public SimpleServer start() { + /** + * 获得原始HttpServer对象 + * + * @return {@link HttpServer} + */ + public HttpServer getRawServer(){ + return this.server; + } + + /** + * 获取服务器地址信息 + * + * @return {@link InetSocketAddress} + */ + public InetSocketAddress getAddress(){ + return this.server.getAddress(); + } + + /** + * 启动Http服务器,启动后会阻塞当前线程 + */ + public void start() { this.server.start(); - return this; } } diff --git a/hutool-http/src/main/java/cn/hutool/http/server/handler/HandlerUtil.java b/hutool-http/src/main/java/cn/hutool/http/server/handler/HandlerUtil.java new file mode 100644 index 000000000..a3fe4d0b8 --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/server/handler/HandlerUtil.java @@ -0,0 +1,91 @@ +package cn.hutool.http.server.handler; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.http.Header; +import cn.hutool.http.HttpStatus; +import cn.hutool.http.HttpUtil; +import com.sun.net.httpserver.HttpExchange; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; + +/** + * 请求处理器相关工具类 + * + * @since 5.2.6 + */ +public class HandlerUtil { + + /** + * 返回404页面 + * + * @param httpExchange HttpExchange + * @param content 要发送的404页面内容 + * @throws IOException IO异常 + */ + public static void send404(HttpExchange httpExchange, String content) throws IOException { + if (null == httpExchange) { + return; + } + + if (null == content) { + content = "404 Not Found !"; + } + + httpExchange.sendResponseHeaders(HttpStatus.HTTP_NOT_FOUND, 0); + try (OutputStream out = httpExchange.getResponseBody()) { + IoUtil.writeUtf8(out, false, content); + } + } + + /** + * 返回文件 + * + * @param httpExchange HttpExchange + * @param file 要发送的文件 + * @throws IOException IO异常 + */ + public static void sendFile(HttpExchange httpExchange, File file) throws IOException { + if (ArrayUtil.hasNull(httpExchange, file)) { + return; + } + addHeader(httpExchange, + Header.CONTENT_TYPE.toString(), + HttpUtil.getMimeType(file.getName(), "text/html")); + httpExchange.sendResponseHeaders(HttpStatus.HTTP_OK, 0); + try (OutputStream out = httpExchange.getResponseBody()) { + FileUtil.writeToStream(file, out); + } + } + + /** + * 增加响应头信息 + * + * @param httpExchange HttpExchange + * @param header 头名 + * @param value 头值 + */ + public static void addHeader(HttpExchange httpExchange, String header, String value) { + if (ArrayUtil.hasEmpty(httpExchange, header)) { + return; + } + httpExchange.getResponseHeaders().add(header, value); + } + + /** + * 获取响应头信息 + * + * @param httpExchange HttpExchange + * @param header 头名 + * @return 值,不存在返回null + */ + public static String getHeader(HttpExchange httpExchange, String header) { + if (ArrayUtil.hasEmpty(httpExchange, header)) { + return null; + } + return httpExchange.getRequestHeaders().getFirst(header); + } +} diff --git a/hutool-http/src/main/java/cn/hutool/http/server/handler/RootHandler.java b/hutool-http/src/main/java/cn/hutool/http/server/handler/RootHandler.java new file mode 100644 index 000000000..8e10274ee --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/server/handler/RootHandler.java @@ -0,0 +1,45 @@ +package cn.hutool.http.server.handler; + +import cn.hutool.core.io.FileUtil; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; + +import java.io.File; +import java.io.IOException; +import java.net.URI; + +/** + * 默认的处理器,通过解析用户传入的path,找到网页根目录下对应文件后返回 + * + * @author looly + * @since 5.2.6 + */ +public class RootHandler implements HttpHandler { + + private final String rootDir; + + /** + * 构造 + * + * @param rootDir 网页根目录 + */ + public RootHandler(String rootDir) { + this.rootDir = rootDir; + } + + @Override + public void handle(HttpExchange httpExchange) throws IOException { + final URI uri = httpExchange.getRequestURI(); + File file = FileUtil.file(rootDir, uri.getPath()); + if (file.exists()) { + if (file.isDirectory()) { + //默认读取主页 + file = FileUtil.file(file, "index.html"); + } + HandlerUtil.sendFile(httpExchange, file); + } + + // 文件未找到 + HandlerUtil.send404(httpExchange, null); + } +} diff --git a/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java b/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java new file mode 100644 index 000000000..70a70a510 --- /dev/null +++ b/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java @@ -0,0 +1,13 @@ +package cn.hutool.http.server; + +import cn.hutool.http.HttpUtil; +import cn.hutool.http.server.handler.RootHandler; + +public class SimpleServerTest { + + public static void main(String[] args) { + HttpUtil.createServer(8888) + .addHandler("/", new RootHandler("D:\\test")) + .start(); + } +} diff --git a/hutool-http/src/test/java/cn/hutool/http/test/SimpleServerTest.java b/hutool-http/src/test/java/cn/hutool/http/test/SimpleServerTest.java deleted file mode 100644 index 43d83973f..000000000 --- a/hutool-http/src/test/java/cn/hutool/http/test/SimpleServerTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.hutool.http.test; - -import cn.hutool.core.io.IoUtil; -import cn.hutool.core.util.StrUtil; -import cn.hutool.http.HttpStatus; -import cn.hutool.http.server.SimpleServer; - -import java.io.OutputStream; - -public class SimpleServerTest { - - public static void main(String[] args) { - final SimpleServer server = new SimpleServer(8888); - server.addHandler("/", httpExchange -> { - httpExchange.sendResponseHeaders(HttpStatus.HTTP_OK, 0); - final OutputStream out = httpExchange.getResponseBody(); - out.write(StrUtil.bytes("Hello Hutool Server!")); - IoUtil.close(out); - }).start(); - } -}