解决短链接响应错乱问题

This commit is contained in:
imhao183
2021-05-24 10:31:09 +08:00
parent 141c8f38d8
commit b0fe3fd57f
82 changed files with 264 additions and 244 deletions
@@ -92,7 +92,7 @@ abstract public class ChannelAction extends Action {
return localSign.equals(sign) ? ErrorCode.ERROR_CODE_SUCCESS : ErrorCode.ERROR_CODE_AUTH_FAILURE;
}
protected void sendResponse(ErrorCode errorCode, Object data) {
protected void sendResponse(Response response, ErrorCode errorCode, Object data) {
response.setStatus(HttpResponseStatus.OK);
if (errorCode == null) {
errorCode = ErrorCode.ERROR_CODE_SUCCESS;
@@ -115,10 +115,10 @@ abstract public class ChannelAction extends Action {
return null;
}
protected void sendApiMessage(String topic, byte[] message, Callback callback) {
sendApiMessage(channelInfo.getOwner(), topic, message, callback);
protected void sendApiMessage(Response response, String topic, byte[] message, Callback callback) {
sendApiMessage(response, channelInfo.getOwner(), topic, message, callback);
}
protected void sendApiMessage(String user, String topic, byte[] message, Callback callback) {
protected void sendApiMessage(Response response, String user, String topic, byte[] message, Callback callback) {
ServerAPIHelper.sendRequest(user, null, topic, message, new ServerAPIHelper.Callback() {
@Override
public void onSuccess(byte[] response) {
@@ -126,12 +126,12 @@ abstract public class ChannelAction extends Action {
}
@Override
public void onError(ErrorCode errorCode) {
sendResponse(errorCode, null);
sendResponse(response, errorCode, null);
}
@Override
public void onTimeout() {
sendResponse(ErrorCode.ERROR_CODE_TIMEOUT, null);
sendResponse(response, ErrorCode.ERROR_CODE_TIMEOUT, null);
}
@Override
@@ -29,9 +29,9 @@ import io.netty.util.internal.StringUtil;
public class GetChannelAction extends ChannelAction {
@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
setResponseContent(RestResult.ok(OutputGetChannelInfo.fromPbInfo(channelInfo)));
setResponseContent(RestResult.ok(OutputGetChannelInfo.fromPbInfo(channelInfo)), response);
}
return true;
}
@@ -31,12 +31,12 @@ import java.util.ArrayList;
public class GetChannelSubscribersAction extends ChannelAction {
@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
OutputStringList outputStringList = new OutputStringList();
outputStringList.setList(new ArrayList<>());
outputStringList.getList().addAll(messagesStore.getChannelSubscriber(channelInfo.getTargetId()));
setResponseContent(RestResult.ok(outputStringList));
setResponseContent(RestResult.ok(outputStringList), response);
}
return true;
}
@@ -31,7 +31,7 @@ import io.netty.util.internal.StringUtil;
public class GetUserAction extends ChannelAction {
@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputGetUserInfo inputUserId = getRequestBody(request.getNettyRequest(), InputGetUserInfo.class);
if (inputUserId != null
@@ -69,9 +69,9 @@ public class GetUserAction extends ChannelAction {
}
}
setResponseContent(result);
setResponseContent(result, response);
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER));
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}
@@ -28,24 +28,24 @@ import win.liyufan.im.IMTopic;
@HttpMethod("POST")
public class ModifyChannelProfileAction extends ChannelAction {
@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputModifyChannelInfo input = getRequestBody(request.getNettyRequest(), InputModifyChannelInfo.class);
if (input != null) {
WFCMessage.ModifyChannelInfo modifyChannelInfo = WFCMessage.ModifyChannelInfo.newBuilder().setChannelId(channelInfo.getTargetId()).setType(input.getType()).setValue(input.getValue()).build();
sendApiMessage(IMTopic.ModifyChannelInfoTopic, modifyChannelInfo.toByteArray(), result -> {
sendApiMessage(response, IMTopic.ModifyChannelInfoTopic, modifyChannelInfo.toByteArray(), result -> {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeBytes(result);
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
if (errorCode == ErrorCode.ERROR_CODE_SUCCESS) {
sendResponse(null, null);
sendResponse(response, null, null);
} else {
sendResponse(errorCode, null);
sendResponse(response, errorCode, null);
}
});
return false;
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER));
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}
@@ -40,14 +40,14 @@ public class SendMessageAction extends ChannelAction {
}
@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
SendChannelMessageData sendChannelMessageData = getRequestBody(request.getNettyRequest(), SendChannelMessageData.class);
if(sendChannelMessageData.getTargets() != null && !sendChannelMessageData.getTargets().isEmpty()) {
if((channelInfo.getStatus() & ProtoConstants.ChannelState.Channel_State_Mask_Message_Unsubscribed) == 0 && (channelInfo.getStatus() & ProtoConstants.ChannelState.Channel_State_Mask_Global) == 0) {
for (String target:sendChannelMessageData.getTargets()) {
if(!messagesStore.checkUserInChannel(target, channelInfo.getTargetId())) {
setResponseContent(RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_RIGHT, "User " + target + " not in channel"));
setResponseContent(RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_RIGHT, "User " + target + " not in channel"), response);
return true;
}
}
@@ -63,21 +63,21 @@ public class SendMessageAction extends ChannelAction {
sendMessageData.setPayload(sendChannelMessageData.getPayload());
sendMessageData.setToUsers(sendChannelMessageData.getTargets());
if (SendMessageData.isValide(sendMessageData)) {
sendApiMessage(IMTopic.SendMessageTopic, sendMessageData.toProtoMessage().toByteArray(), result -> {
sendApiMessage(response, IMTopic.SendMessageTopic, sendMessageData.toProtoMessage().toByteArray(), result -> {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeBytes(result);
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
if (errorCode == ErrorCode.ERROR_CODE_SUCCESS) {
long messageId = byteBuf.readLong();
long timestamp = byteBuf.readLong();
sendResponse(null, new SendMessageResult(messageId, timestamp));
sendResponse(response, null, new SendMessageResult(messageId, timestamp));
} else {
sendResponse(errorCode, null);
sendResponse(response, errorCode, null);
}
});
return false;
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER));
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}
return true;
@@ -19,6 +19,7 @@ import com.xiaoleilu.loServer.RestResult;
import com.xiaoleilu.loServer.annotation.HttpMethod;
import com.xiaoleilu.loServer.annotation.Route;
import com.xiaoleilu.loServer.handler.Request;
import com.xiaoleilu.loServer.handler.Response;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.FullHttpRequest;
@@ -30,7 +31,7 @@ import win.liyufan.im.IMTopic;
public class SubscriberChannelAction extends ChannelAction {
@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputChannelSubscribe input = getRequestBody(request.getNettyRequest(), InputChannelSubscribe.class);
if (input != null && !StringUtil.isNullOrEmpty(input.getTarget())) {
@@ -41,19 +42,19 @@ public class SubscriberChannelAction extends ChannelAction {
return true;
}
WFCMessage.ListenChannel listenChannel = WFCMessage.ListenChannel.newBuilder().setChannelId(channelInfo.getTargetId()).setListen(input.getSubscribe()).build();
sendApiMessage(input.getTarget(), IMTopic.ChannelListenTopic, listenChannel.toByteArray(), result -> {
sendApiMessage(response, input.getTarget(), IMTopic.ChannelListenTopic, listenChannel.toByteArray(), result -> {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeBytes(result);
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
if (errorCode == ErrorCode.ERROR_CODE_SUCCESS) {
sendResponse(null, null);
sendResponse(response, null, null);
} else {
sendResponse(errorCode, null);
sendResponse(response, errorCode, null);
}
});
return false;
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER));
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}