diff --git a/jcommon/docean/pom.xml b/jcommon/docean/pom.xml index 5e4621c9e..af119859a 100644 --- a/jcommon/docean/pom.xml +++ b/jcommon/docean/pom.xml @@ -16,8 +16,8 @@ run.mone easy - - 1.5.0-jdk21 + 1.4-jdk21-SNAPSHOT + cglib diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java index d6cd74510..df8f4804f 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Mvc.java @@ -78,6 +78,10 @@ private void setConfig(Ioc ioc) { this.mvcConfig.setAllowCross(Boolean.valueOf(ioc.getBean(MvcConst.ALLOW_CROSS_DOMAIN, MvcConst.FALSE))); this.mvcConfig.setDownload(Boolean.valueOf(ioc.getBean(MvcConst.MVC_DOWNLOAD, MvcConst.FALSE))); this.mvcConfig.setUseCglib(Boolean.valueOf(ioc.getBean(MvcConst.CGLIB, MvcConst.TRUE))); + + this.mvcConfig.setOpenStaticFile(Boolean.valueOf(ioc.getBean(MvcConst.OPEN_STATIC_FILE, MvcConst.FALSE))); + this.mvcConfig.setStaticFilePath(ioc.getBean(MvcConst.STATIC_FILE_PATH, MvcConst.EMPTY)); + this.mvcConfig.setResponseOriginalValue(Boolean.valueOf(ioc.getBean(MvcConst.RESPONSE_ORIGINAL_VALUE, MvcConst.FALSE))); this.mvcConfig.setPoolSize(Integer.valueOf(ioc.getBean(MvcConst.MVC_POOL_SIZE, String.valueOf(MvcConst.DEFAULT_MVC_POOL_SIZE)))); this.mvcConfig.setVirtualThread(Boolean.valueOf(ioc.getBean(MvcConst.VIRTUAL_THREAD, MvcConst.TRUE))); diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/bo/MvcConfig.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/bo/MvcConfig.java index 6159d4b91..f93e818ca 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/bo/MvcConfig.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/bo/MvcConfig.java @@ -38,4 +38,8 @@ public class MvcConfig implements Serializable { */ private boolean virtualThread; + private String staticFilePath; + + private boolean openStaticFile; + } diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/config/HttpServerConfig.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/config/HttpServerConfig.java index 904a0ee18..2b8780eb5 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/config/HttpServerConfig.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/config/HttpServerConfig.java @@ -52,10 +52,9 @@ public class HttpServerConfig { private boolean userWs; - public static int HTTP_POOL_SIZE = 500; - public static int HTTP_POOL_QUEUE_SIZE = 1000; + public static int HTTP_POOL_QUEUE_SIZE = 1000; public enum HttpVersion { http1, http2 diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java index 20a94e674..ea1ebdd13 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/MvcRunnable.java @@ -2,11 +2,14 @@ import com.google.gson.Gson; import com.xiaomi.youpin.docean.Mvc; +import com.xiaomi.youpin.docean.bo.MvcConfig; import com.xiaomi.youpin.docean.common.Cons; import com.xiaomi.youpin.docean.common.Safe; +import com.xiaomi.youpin.docean.common.StringUtils; import com.xiaomi.youpin.docean.config.HttpServerConfig; import com.xiaomi.youpin.docean.mvc.common.MvcConst; import com.xiaomi.youpin.docean.mvc.download.Download; +import com.xiaomi.youpin.docean.mvc.html.Html; import com.xiaomi.youpin.docean.mvc.upload.MvcUpload; import com.xiaomi.youpin.docean.mvc.util.ExceptionUtil; import com.xiaomi.youpin.docean.mvc.util.MethodFinder; @@ -31,6 +34,8 @@ public class MvcRunnable implements Runnable { private MvcResponse response; + private MvcConfig config; + private ConcurrentHashMap requestMethodMap; private Mvc mvc; @@ -43,6 +48,7 @@ public MvcRunnable(Mvc mvc, MvcContext context, MvcRequest request, MvcResponse this.response = response; this.requestMethodMap = requestMethodMap; this.mvc = mvc; + this.config = this.mvc.getMvcConfig(); } @@ -65,6 +71,7 @@ public MvcRunnable(Mvc mvc, HttpServerConfig config, ChannelHandlerContext ctx, this.request.setBody(body); this.response.setCtx(ctx); this.mvc = mvc; + this.config = this.mvc.getMvcConfig(); this.requestMethodMap = requestMethodMap; } @@ -107,6 +114,17 @@ private void call() { } String path = request.getPath(); + + if (config.isOpenStaticFile() && Html.isHtmlFile(path)) { + String content = Html.view(config.getStaticFilePath() + path); + if (StringUtils.isEmpty(content)) { + sendNotFoundResponse(); + return; + } + response.writeAndFlush(context, content); + return; + } + //Support file download (/download) and must enable downloads. if (isDownload(path) && mvc.getMvcConfig().isDownload()) { Download.download(context, request, response); @@ -127,16 +145,20 @@ private void call() { } if (!path.equals(Cons.Service)) { - MvcResult result = new MvcResult<>(); - result.setCode(HttpResponseStatus.NOT_FOUND.code()); - result.setMessage(HttpResponseStatus.NOT_FOUND.reasonPhrase()); - response.writeAndFlush(context, gson.toJson(result)); + sendNotFoundResponse(); return; } //rate limited or exceeded quota mvc.callService(context, request, response); } + private void sendNotFoundResponse() { + MvcResult result = new MvcResult<>(); + result.setCode(HttpResponseStatus.NOT_FOUND.code()); + result.setMessage(HttpResponseStatus.NOT_FOUND.reasonPhrase()); + response.writeAndFlush(context, gson.toJson(result)); + } + private boolean isDownload(String path) { return path.equals("/download"); diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/common/MvcConst.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/common/MvcConst.java index 0d01c175d..d2538ee71 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/common/MvcConst.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/common/MvcConst.java @@ -10,8 +10,14 @@ public abstract class MvcConst { public static final String CGLIB = "$cglib"; + public static final String STATIC_FILE_PATH = "$staticFilePath"; + + public static final String OPEN_STATIC_FILE = "$openStaticFile"; + public static final String TRUE = "true"; + public static final String EMPTY = ""; + public static final String FALSE = "false"; public static final String ALLOW_CROSS_DOMAIN = "$allow-cross-domain"; diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/html/Html.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/html/Html.java new file mode 100644 index 000000000..6f57e96e6 --- /dev/null +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/mvc/html/Html.java @@ -0,0 +1,35 @@ +package com.xiaomi.youpin.docean.mvc.html; + +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * @author goodjava@qq.com + * @date 2024/2/24 14:10 + */ +@Slf4j +public class Html { + + //读取指定html页面文件内容并返回 + public static String view(String file) { + try { + Path path = Paths.get(file); + return new String(Files.readAllBytes(path), StandardCharsets.UTF_8); + } catch (IOException e) { + log.error(e.getMessage(), e); + e.printStackTrace(); + return ""; + } + } + + //判断一个文件是否是.html文件,并且返回boolean值(class) + public static boolean isHtmlFile(String filePath) { + return filePath != null && filePath.toLowerCase().endsWith(".html"); + } + +} diff --git a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java index 00b677105..4c7fd66e0 100644 --- a/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java +++ b/jcommon/docean/src/test/java/com/xiaomi/youpin/docean/test/HttpServerTest.java @@ -79,7 +79,7 @@ public void testHttpServer() throws InterruptedException { } - Ioc.ins().putBean("$response-original-value", "true"); + Ioc.ins().putBean("$response-original-value", "true").putBean("$openStaticFile","true").putBean("$staticFilePath","/tmp/"); // Ioc.ins().putBean("$ssl_domain", "zzy.com"); // Ioc.ins().putBean("$ssl_self_sign", "false"); // Ioc.ins().putBean("$ssl_certificate","/Users/zhangzhiyong/key/zzy.com/certificate.crt"); @@ -88,8 +88,8 @@ public void testHttpServer() throws InterruptedException { Mvc.ins(); DoceanHttpServer server = new DoceanHttpServer(HttpServerConfig.builder() .httpVersion(HttpServerConfig.HttpVersion.http1) -// .ssl(true) - .port(8999) + .ssl(false) + .port(8899) .websocket(false) .uploadDir("/tmp/v").upload(false) .build()); diff --git a/jcommon/docean/src/test/resources/config.properties b/jcommon/docean/src/test/resources/config.properties index cc623e74e..a4c52ffe1 100644 --- a/jcommon/docean/src/test/resources/config.properties +++ b/jcommon/docean/src/test/resources/config.properties @@ -9,4 +9,8 @@ demoVo=com.xiaomi.youpin.docean.test.demo.DemoVo val=123 +openStaticFile=true +staticFilePath=/tmp/ + +