Skip to content

Commit b759be1

Browse files
committed
optimize logic
1 parent f5827c1 commit b759be1

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/HttpCommand.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.eventmesh.common.protocol.http.body.BaseResponseBody;
2323
import org.apache.eventmesh.common.protocol.http.body.Body;
2424
import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode;
25+
import org.apache.eventmesh.common.protocol.http.common.RequestCode;
2526
import org.apache.eventmesh.common.protocol.http.header.BaseResponseHeader;
2627
import org.apache.eventmesh.common.protocol.http.header.Header;
2728
import org.apache.eventmesh.common.utils.JsonUtils;
@@ -80,7 +81,9 @@ public HttpCommand(String httpMethod, String httpVersion, String requestCode) {
8081
}
8182

8283
public HttpCommand createHttpCommandResponse(Header header, Body body) {
83-
84+
if (this.requestCode == null) {
85+
this.requestCode = RequestCode.UNKNOWN.getRequestCode().toString();
86+
}
8487
HttpCommand response = new HttpCommand(this.httpMethod, this.httpVersion, this.requestCode);
8588
response.setOpaque(this.opaque);
8689
response.setReqTime(this.reqTime);
@@ -92,7 +95,9 @@ public HttpCommand createHttpCommandResponse(Header header, Body body) {
9295
}
9396

9497
public HttpCommand createHttpCommandResponse(EventMeshRetCode eventMeshRetCode) {
95-
98+
if (this.requestCode == null) {
99+
this.requestCode = RequestCode.UNKNOWN.getRequestCode().toString();
100+
}
96101
HttpCommand response = new HttpCommand(this.httpMethod, this.httpVersion, this.requestCode);
97102
response.setOpaque(this.opaque);
98103
response.setReqTime(this.reqTime);

eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/RequestCode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
public enum RequestCode {
2121

22+
UNKNOWN(0, "UNKNOWN"),
23+
2224
MSG_BATCH_SEND(102, "SEND BATCH MSG"),
2325

2426
MSG_BATCH_SEND_V2(107, "SEND BATCH MSG V2"),

eventmesh-common/src/test/java/org/apache/eventmesh/common/protocol/http/HttpCommandTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.mockito.Mockito.when;
2121

2222
import org.apache.eventmesh.common.protocol.http.body.Body;
23+
import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode;
2324
import org.apache.eventmesh.common.protocol.http.header.Header;
2425

2526
import java.util.HashMap;
@@ -89,4 +90,13 @@ public void testHttpResponseWithREQCmdType() throws Exception {
8990
DefaultFullHttpResponse response = httpCommand.httpResponse();
9091
Assert.assertNull(response);
9192
}
93+
94+
@Test
95+
public void testCreateHttpCommandResponse() {
96+
HttpCommand command = new HttpCommand();
97+
HttpCommand response = command.createHttpCommandResponse(EventMeshRetCode.SUCCESS);
98+
Assert.assertNotNull(response);
99+
Assert.assertEquals("0", response.getRequestCode());
100+
101+
}
92102
}

0 commit comments

Comments
 (0)