From bd5c8bccb675e6bb6ca6241bc1818cdcb30c6804 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Wed, 13 Jul 2022 18:58:28 +0800 Subject: [PATCH] =?UTF-8?q?web=E6=9C=8D=E5=8A=A1=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../neutrino/core/util/HttpServerUtil.java | 46 +++++++++++++++++++ .../fun/asgc/neutrino/core/web/HttpEntry.java | 31 +++++++++++++ .../neutrino/core/web/HttpResponseEntry.java | 42 +++++++++++++++++ .../core/web/WebApplicationServer.java | 23 ++++++++-- .../src/main/resources/application.yml | 2 +- 5 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/util/HttpServerUtil.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpEntry.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpResponseEntry.java diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/HttpServerUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/HttpServerUtil.java new file mode 100644 index 00000000..460061e2 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/HttpServerUtil.java @@ -0,0 +1,46 @@ +/** + * Copyright (C) 2018-2022 Zeyi information technology (Shanghai) Co., Ltd. + *
+ * All right reserved. + *
+ * This software is the confidential and proprietary
+ * information of Zeyi Company of China.
+ * ("Confidential Information"). You shall not disclose
+ * such Confidential Information and shall use it only
+ * in accordance with the terms of the contract agreement
+ * you entered into with Zeyi inc.
+ */
+package fun.asgc.neutrino.core.util;
+
+import com.alibaba.fastjson.JSON;
+import fun.asgc.neutrino.core.web.HttpResponseEntry;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.http.*;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+
+/**
+ *
+ * @author: wen.y
+ * @date: 2022/5/28
+ */
+public class HttpServerUtil {
+
+ public static void send404Response(ChannelHandlerContext context, String url) {
+ HttpResponseEntry responseEntry = new HttpResponseEntry(HttpResponseStatus.NOT_FOUND.code(), HttpResponseStatus.NOT_FOUND.reasonPhrase(), String.format("未找到指定资源: %s", url));
+ String res = JSON.toJSONString(responseEntry);
+ FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, Unpooled.wrappedBuffer(res.getBytes()));
+ fullHttpResponse.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
+ context.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE);
+ }
+
+ public static void send500Response(ChannelHandlerContext context, Throwable throwable) {
+ HttpResponseEntry responseEntry = new HttpResponseEntry(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), HttpResponseStatus.INTERNAL_SERVER_ERROR.reasonPhrase(), String.format("服务异常: %s", ExceptionUtils.getStackTrace(throwable)));
+ String res = JSON.toJSONString(responseEntry);
+ FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(res.getBytes()));
+ fullHttpResponse.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
+ context.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE);
+ }
+
+}
diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpEntry.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpEntry.java
new file mode 100644
index 00000000..66ab2209
--- /dev/null
+++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpEntry.java
@@ -0,0 +1,31 @@
+/**
+ * Copyright (c) 2022 aoshiguchen
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package fun.asgc.neutrino.core.web;
+
+/**
+ *
+ * @author: aoshiguchen
+ * @date: 2022/5/28
+ */
+public interface HttpEntry {
+
+}
diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpResponseEntry.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpResponseEntry.java
new file mode 100644
index 00000000..62cd0d12
--- /dev/null
+++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpResponseEntry.java
@@ -0,0 +1,42 @@
+/**
+ * Copyright (c) 2022 aoshiguchen
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package fun.asgc.neutrino.core.web;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.experimental.Accessors;
+
+/**
+ *
+ * @author: aoshiguchen
+ * @date: 2022/5/28
+ */
+@AllArgsConstructor
+@NoArgsConstructor
+@Accessors(chain = true)
+@Data
+public class HttpResponseEntry implements HttpEntry {
+ private int code;
+ private String status;
+ private String msg;
+}
diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/WebApplicationServer.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/WebApplicationServer.java
index d476f025..bb3b7a88 100644
--- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/WebApplicationServer.java
+++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/WebApplicationServer.java
@@ -27,16 +27,16 @@ import fun.asgc.neutrino.core.annotation.Destroy;
import fun.asgc.neutrino.core.annotation.Init;
import fun.asgc.neutrino.core.context.ApplicationConfig;
import fun.asgc.neutrino.core.context.ApplicationRunner;
+import fun.asgc.neutrino.core.util.HttpServerUtil;
import fun.asgc.neutrino.core.util.NumberUtil;
import fun.asgc.neutrino.core.util.StringUtil;
import io.netty.bootstrap.ServerBootstrap;
+import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
-import io.netty.handler.codec.http.FullHttpRequest;
-import io.netty.handler.codec.http.HttpObjectAggregator;
-import io.netty.handler.codec.http.HttpServerCodec;
+import io.netty.handler.codec.http.*;
import io.netty.handler.stream.ChunkedWriteHandler;
import lombok.extern.slf4j.Slf4j;
@@ -71,6 +71,10 @@ public class WebApplicationServer implements ApplicationRunner {
@Override
public void run(String[] args) throws Exception {
ApplicationConfig.Http http = rootApplicationConfig.getHttp();
+ if (!http.getContextPath().endsWith("/")) {
+ http.setContextPath(http.getContextPath() + "/");
+ }
+
this.serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
@@ -86,9 +90,18 @@ public class WebApplicationServer implements ApplicationRunner {
pipeline.addLast(new HttpObjectAggregator(http.getMaxContentLength().intValue()));
pipeline.addLast(new SimpleChannelInboundHandler