Looly 5 years ago
parent
commit
f2b44605d0

+ 32 - 0
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;
+	}
+}

+ 3 - 5
hutool-http/src/main/java/cn/hutool/http/server/HttpRequest.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<String, HttpCookie> 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);
 	}
 
 	/**

+ 89 - 0
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;
+	}
+}

+ 27 - 3
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
 	 */
@@ -63,6 +66,27 @@ public class SimpleServer {
 	}
 
 	/**
+	 * 设置根目录,默认的页面从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));
+	}
+
+	/**
 	 * 设置自定义线程池
 	 *
 	 * @param executor {@link Executor}
@@ -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();
 	}
 

+ 22 - 0
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;
+
+/**
+ * 请求处理接口<br>
+ * 当用户请求某个Path,则调用相应Action的doAction方法
+ *
+ * @author Looly
+ * @since 5.2.6
+ */
+public interface Action {
+
+	/**
+	 * 处理请求
+	 *
+	 * @param request  请求对象
+	 * @param response 响应对象
+	 */
+	void doAction(HttpServerRequest request, HttpServerResponse response);
+}

+ 32 - 0
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));
+	}
+}