Skip to content

Commit

Permalink
[ISSUE #4345]Fix publish EventMeshMessage without requestCode throw j…
Browse files Browse the repository at this point in the history
…ava.lang.NullPointerException (#4352)

* [ISSUE #4345]Fix publish EventMeshMessage without requestCode throw java.lang.NullPointerException

* optimize logic
  • Loading branch information
mxsm authored Aug 14, 2023
1 parent 85ee0fb commit 8be4180
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
import org.apache.eventmesh.common.protocol.http.body.BaseResponseBody;
import org.apache.eventmesh.common.protocol.http.body.Body;
import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode;
import org.apache.eventmesh.common.protocol.http.common.RequestCode;
import org.apache.eventmesh.common.protocol.http.header.BaseResponseHeader;
import org.apache.eventmesh.common.protocol.http.header.Header;
import org.apache.eventmesh.common.utils.JsonUtils;

import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -82,8 +81,8 @@ public HttpCommand(String httpMethod, String httpVersion, String requestCode) {
}

public HttpCommand createHttpCommandResponse(Header header, Body body) {
if (StringUtils.isBlank(requestCode)) {
return null;
if (this.requestCode == null) {
this.requestCode = RequestCode.UNKNOWN.getRequestCode().toString();
}
HttpCommand response = new HttpCommand(this.httpMethod, this.httpVersion, this.requestCode);
response.setOpaque(this.opaque);
Expand All @@ -96,8 +95,8 @@ public HttpCommand createHttpCommandResponse(Header header, Body body) {
}

public HttpCommand createHttpCommandResponse(EventMeshRetCode eventMeshRetCode) {
if (StringUtils.isBlank(requestCode)) {
return null;
if (this.requestCode == null) {
this.requestCode = RequestCode.UNKNOWN.getRequestCode().toString();
}
HttpCommand response = new HttpCommand(this.httpMethod, this.httpVersion, this.requestCode);
response.setOpaque(this.opaque);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@
public enum EventMeshRetCode {

SUCCESS(0, "success"),
OVERLOAD(1, "eventMesh overload, try later, "),
EVENTMESH_REQUESTCODE_INVALID(2, "requestCode can't be null, or must be number, "),
EVENTMESH_SEND_SYNC_MSG_ERR(3, "eventMesh send rr msg err, "),
EVENTMESH_WAITING_RR_MSG_ERR(4, "eventMesh waiting rr msg err, "),
EVENTMESH_PROTOCOL_HEADER_ERR(6, "eventMesh protocol[header] err, "),
EVENTMESH_PROTOCOL_BODY_ERR(7, "eventMesh protocol[body] err, "),
EVENTMESH_PROTOCOL_BODY_SIZE_ERR(8, "event size exceeds the limit, "),
EVENTMESH_STOP(9, "eventMesh will stop or had stopped, "),
EVENTMESH_REJECT_BY_PROCESSOR_ERROR(10, "eventMesh reject by processor error, "),
EVENTMESH_BATCH_PRODUCER_STOPED_ERR(11, "eventMesh batch msg producer stopped, "),
EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR(12, "eventMesh batch msg speed over the limit, "),
OVERLOAD(1, "eventMesh overload, try later"),
EVENTMESH_REQUESTCODE_INVALID(2, "requestCode can't be null, or must be number"),
EVENTMESH_SEND_SYNC_MSG_ERR(3, "eventMesh send rr msg error"),
EVENTMESH_WAITING_RR_MSG_ERR(4, "eventMesh waiting rr msg error"),
EVENTMESH_PROTOCOL_HEADER_ERR(6, "eventMesh protocol[header] error"),
EVENTMESH_PROTOCOL_BODY_ERR(7, "eventMesh protocol[body] error"),
EVENTMESH_PROTOCOL_BODY_SIZE_ERR(8, "event size exceeds the limit"),
EVENTMESH_STOP(9, "eventMesh will stop or had stopped"),
EVENTMESH_REJECT_BY_PROCESSOR_ERROR(10, "eventMesh reject by processor error"),
EVENTMESH_BATCH_PRODUCER_STOPED_ERR(11, "eventMesh batch msg producer stopped"),
EVENTMESH_BATCH_SPEED_OVER_LIMIT_ERR(12, "eventMesh batch msg speed over the limit"),
EVENTMESH_PACKAGE_MSG_ERR(13, "eventMesh package msg err, "),
EVENTMESH_GROUP_PRODUCER_STOPED_ERR(14, "eventMesh group producer stopped, "),
EVENTMESH_SEND_ASYNC_MSG_ERR(15, "eventMesh send async msg err, "),
EVENTMESH_GROUP_PRODUCER_STOPED_ERR(14, "eventMesh group producer stopped"),
EVENTMESH_SEND_ASYNC_MSG_ERR(15, "eventMesh send async msg error"),
EVENTMESH_REPLY_MSG_ERR(16, "eventMesh reply msg err, "),
EVENTMESH_SEND_BATCHLOG_MSG_ERR(17, "eventMesh send batchlog msg err, "),
EVENTMESH_RUNTIME_ERR(18, "eventMesh runtime err, "),
EVENTMESH_SUBSCRIBE_ERR(19, "eventMesh subscribe err"),
EVENTMESH_UNSUBSCRIBE_ERR(20, "eventMesh unsubscribe err"),
EVENTMESH_HEARTBEAT_ERR(21, "eventMesh heartbeat err"),
EVENTMESH_ACL_ERR(22, "eventMesh acl err"),
EVENTMESH_HTTP_MES_SEND_OVER_LIMIT_ERR(23, "eventMesh http msg send over the limit, "),
EVENTMESH_OPERATE_FAIL(100, "operate fail, ");
EVENTMESH_SEND_BATCHLOG_MSG_ERR(17, "eventMesh send batch log msg error"),
EVENTMESH_RUNTIME_ERR(18, "eventMesh runtime error"),
EVENTMESH_SUBSCRIBE_ERR(19, "eventMesh subscribe error"),
EVENTMESH_UNSUBSCRIBE_ERR(20, "eventMesh unsubscribe error"),
EVENTMESH_HEARTBEAT_ERR(21, "eventMesh heartbeat error"),
EVENTMESH_ACL_ERR(22, "eventMesh acl error"),
EVENTMESH_HTTP_MES_SEND_OVER_LIMIT_ERR(23, "eventMesh http msg send over the limit"),
EVENTMESH_OPERATE_FAIL(100, "operate fail");

private final Integer retCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

public enum RequestCode {

UNKNOWN(0, "UNKNOWN"),

MSG_BATCH_SEND(102, "SEND BATCH MSG"),

MSG_BATCH_SEND_V2(107, "SEND BATCH MSG V2"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.mockito.Mockito.when;

import org.apache.eventmesh.common.protocol.http.body.Body;
import org.apache.eventmesh.common.protocol.http.common.EventMeshRetCode;
import org.apache.eventmesh.common.protocol.http.header.Header;

import java.util.HashMap;
Expand Down Expand Up @@ -89,4 +90,13 @@ public void testHttpResponseWithREQCmdType() throws Exception {
DefaultFullHttpResponse response = httpCommand.httpResponse();
Assert.assertNull(response);
}

@Test
public void testCreateHttpCommandResponse() {
HttpCommand command = new HttpCommand();
HttpCommand response = command.createHttpCommandResponse(EventMeshRetCode.SUCCESS);
Assert.assertNotNull(response);
Assert.assertEquals("0", response.getRequestCode());

}
}

0 comments on commit 8be4180

Please sign in to comment.