From 2102c9e7a5c79c122d8ce172672c6edaf2f1e209 Mon Sep 17 00:00:00 2001 From: chaos Date: Mon, 28 Oct 2024 18:19:38 +0800 Subject: [PATCH] =?UTF-8?q?HTTP=20TRACE&TRACK=E6=BC=8F=E6=B4=9E=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xxl/job/core/biz/model/ReturnT.java | 102 ++++++++++-------- .../com/xxl/job/core/server/EmbedServer.java | 18 +++- 2 files changed, 69 insertions(+), 51 deletions(-) diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/biz/model/ReturnT.java b/xxl-job-core/src/main/java/com/xxl/job/core/biz/model/ReturnT.java index 83d7a361..9955ae55 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/biz/model/ReturnT.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/biz/model/ReturnT.java @@ -4,54 +4,64 @@ import java.io.Serializable; /** * common return - * @author xuxueli 2015-12-4 16:32:31 + * * @param + * @author xuxueli 2015-12-4 16:32:31 */ public class ReturnT implements Serializable { - public static final long serialVersionUID = 42L; - - public static final int SUCCESS_CODE = 200; - public static final int FAIL_CODE = 500; - - public static final ReturnT SUCCESS = new ReturnT(null); - public static final ReturnT FAIL = new ReturnT(FAIL_CODE, null); - - private int code; - private String msg; - private T content; - - public ReturnT(){} - public ReturnT(int code, String msg) { - this.code = code; - this.msg = msg; - } - public ReturnT(T content) { - this.code = SUCCESS_CODE; - this.content = content; - } - - public int getCode() { - return code; - } - public void setCode(int code) { - this.code = code; - } - public String getMsg() { - return msg; - } - public void setMsg(String msg) { - this.msg = msg; - } - public T getContent() { - return content; - } - public void setContent(T content) { - this.content = content; - } - - @Override - public String toString() { - return "ReturnT [code=" + code + ", msg=" + msg + ", content=" + content + "]"; - } + public static final long serialVersionUID = 42L; + + public static final int SUCCESS_CODE = 200; + public static final int FAIL_CODE = 500; + public static final int METHOD_NOT_ALLOWED = 405; + + public static final ReturnT SUCCESS = new ReturnT(null); + public static final ReturnT FAIL = new ReturnT(FAIL_CODE, null); + + private int code; + private String msg; + private T content; + + public ReturnT() { + } + + public ReturnT(int code, String msg) { + this.code = code; + this.msg = msg; + } + + public ReturnT(T content) { + this.code = SUCCESS_CODE; + this.content = content; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public T getContent() { + return content; + } + + public void setContent(T content) { + this.content = content; + } + + @Override + public String toString() { + return "ReturnT [code=" + code + ", msg=" + msg + ", content=" + content + "]"; + } } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/server/EmbedServer.java b/xxl-job-core/src/main/java/com/xxl/job/core/server/EmbedServer.java index 540e0ea2..b8732fc0 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/server/EmbedServer.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/server/EmbedServer.java @@ -156,16 +156,24 @@ public class EmbedServer { public void run() { // do invoke Object responseObj = process(httpMethod, uri, requestData, accessTokenReq); - + HttpResponseStatus status = resolveHttpStatus(responseObj); // to json String responseJson = GsonTool.toJson(responseObj); - // write response - writeResponse(ctx, keepAlive, responseJson); + writeResponse(ctx, keepAlive, status, responseJson); } }); } + private HttpResponseStatus resolveHttpStatus(Object responseObj) { + if (responseObj instanceof ReturnT) { + ReturnT returnT = (ReturnT) responseObj; + return returnT.getCode() == ReturnT.METHOD_NOT_ALLOWED ? HttpResponseStatus.METHOD_NOT_ALLOWED : HttpResponseStatus.OK; + } else { + return HttpResponseStatus.OK; + } + } + private Object process(HttpMethod httpMethod, String uri, String requestData, String accessTokenReq) { // valid if (HttpMethod.POST != httpMethod) { @@ -209,9 +217,9 @@ public class EmbedServer { /** * write response */ - private void writeResponse(ChannelHandlerContext ctx, boolean keepAlive, String responseJson) { + private void writeResponse(ChannelHandlerContext ctx, boolean keepAlive, HttpResponseStatus status, String responseJson) { // write response - FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer(responseJson, CharsetUtil.UTF_8)); // Unpooled.wrappedBuffer(responseJson) + FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer(responseJson, CharsetUtil.UTF_8)); // Unpooled.wrappedBuffer(responseJson) response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8"); // HttpHeaderValues.TEXT_PLAIN.toString() response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); if (keepAlive) {