Skip to content

Commit

Permalink
解决短链接响应错乱问题
Browse files Browse the repository at this point in the history
  • Loading branch information
imhao183 committed May 24, 2021
1 parent 141c8f3 commit b0fe3fd
Show file tree
Hide file tree
Showing 82 changed files with 264 additions and 244 deletions.
10 changes: 4 additions & 6 deletions broker/src/main/java/com/xiaoleilu/loServer/action/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ abstract public class Action {
public static ISessionsStore sessionsStore = null;

public ChannelHandlerContext ctx;
public Response response;

protected class Result {
Object data;
ErrorCode errorCode;
Expand Down Expand Up @@ -73,7 +71,7 @@ public ErrorCode preAction(Request request, Response response) {

return ERROR_CODE_SUCCESS;
}
public boolean doAction(Request request) {
public boolean doAction(Request request, Response response) {
ErrorCode errorCode = preAction(request, response);
boolean isSync = true;
if (errorCode == ErrorCode.ERROR_CODE_SUCCESS) {
Expand All @@ -89,7 +87,7 @@ public boolean doAction(Request request) {
// throw e;
// }
// } else {
isSync = action(request);
isSync = action(request, response);
// }
} else {
response.setStatus(HttpResponseStatus.OK);
Expand All @@ -107,7 +105,7 @@ public boolean doAction(Request request) {
public boolean isTransactionAction() {
return false;
}
abstract public boolean action(Request request);
abstract public boolean action(Request request, Response response);

protected <T> T getRequestBody(HttpRequest request, Class<T> cls) {
if (request instanceof FullHttpRequest) {
Expand All @@ -121,7 +119,7 @@ protected <T> T getRequestBody(HttpRequest request, Class<T> cls) {
return null;
}

protected void setResponseContent(RestResult result) {
protected void setResponseContent(RestResult result, Response response) {
response.setStatus(HttpResponseStatus.OK);
response.setContent(new Gson().toJson(result));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class CheckTokenAction extends Action {
private final RateLimiter mLimitCounter = new RateLimiter(10, 1);
@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
response.setStatus(HttpResponseStatus.OK);
String userId = request.getParam("userId");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
public class DefaultIndexAction extends Action{
@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
response.setContent("Welcome to LoServer.");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class FileAction extends Action {
private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(FileAction.class);

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
response.setHeader("Access-Control-Allow-Origin", "*");
if (false == Request.METHOD_GET.equalsIgnoreCase(request.getMethod())) {
response.sendError(HttpResponseStatus.METHOD_NOT_ALLOWED, "Please use GET method to request file!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class IMAction extends Action {

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {

FullHttpRequest fullHttpRequest = (FullHttpRequest)request.getNettyRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class NotFoundAction extends Action{
private final static String TEMPLATE_ERROR = "<!DOCTYPE html><html><head><title>LoServer - Error report</title><style>h1,h3 {color:white; background-color: gray;}</style></head><body><h1>HTTP Status {} - {}</h1><hr size=\"1\" noshade=\"noshade\" /><p>{}</p><hr size=\"1\" noshade=\"noshade\" /><h3>LoServer</h3></body></html>";

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
response.sendError(HttpResponseStatus.NOT_FOUND, "404 Not found!");
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class RouteAction extends Action {
private static final Logger LOG = LoggerFactory.getLogger(RouteAction.class);

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {

response.setContentType("application/octet-stream");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class UnknownErrorAction extends Action{
private final static String TEMPLATE_ERROR = "<!DOCTYPE html><html><head><title>LoServer - Error report</title><style>h1,h3 {color:white; background-color: gray;}</style></head><body><h1>HTTP Status {} - {}</h1><hr size=\"1\" noshade=\"noshade\" /><p>{}</p><hr size=\"1\" noshade=\"noshade\" /><h3>LoServer</h3></body></html>";

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static int validateToken(String token) throws InvalidateTokenExecption {
}

@Override
public boolean action(Request r) {
public boolean action(Request r, Response response) {
if (r.getNettyRequest() instanceof FullHttpRequest) {

FullHttpRequest request = (FullHttpRequest) r.getNettyRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class VersionAction extends Action {


@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
response.setStatus(HttpResponseStatus.OK);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand All @@ -31,7 +32,7 @@ public boolean isTransactionAction() {
}

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputAddFriendRequest input = getRequestBody(request.getNettyRequest(), InputAddFriendRequest.class);

Expand All @@ -41,7 +42,7 @@ public boolean action(Request request) {
}

WFCMessage.AddFriendRequest addFriendRequest = WFCMessage.AddFriendRequest.newBuilder().setReason(input.getReason()).setTargetUid(input.getFriendUid()).build();
sendApiMessage(input.getUserId(), IMTopic.AddFriendRequestTopic, addFriendRequest.toByteArray(), result -> {
sendApiMessage(response, input.getUserId(), IMTopic.AddFriendRequestTopic, addFriendRequest.toByteArray(), result -> {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeBytes(result);
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.xiaoleilu.loServer.annotation.Route;
import com.xiaoleilu.loServer.handler.Request;
import cn.wildfirechat.pojos.InputAddGroupMember;
import com.xiaoleilu.loServer.handler.Response;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.FullHttpRequest;
Expand All @@ -33,19 +34,19 @@ public boolean isTransactionAction() {
}

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputAddGroupMember inputAddGroupMember = getRequestBody(request.getNettyRequest(), InputAddGroupMember.class);
if (inputAddGroupMember.isValide()) {
sendApiMessage(inputAddGroupMember.getOperator(), IMTopic.AddGroupMemberTopic, inputAddGroupMember.toProtoGroupRequest().toByteArray(), result -> {
sendApiMessage(response, inputAddGroupMember.getOperator(), IMTopic.AddGroupMemberTopic, inputAddGroupMember.toProtoGroupRequest().toByteArray(), result -> {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeBytes(result);
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
return new Result(errorCode);
});
return false;
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER));
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ protected void sendResponse(Response response, ErrorCode errorCode, Object data)
response.setContent(new Gson().toJson(result));
response.send();
}
protected void sendApiMessage(String fromUser, String topic, byte[] message, ApiCallback callback) {
sendApiMessage(fromUser, null, topic, message, callback, false);
protected void sendApiMessage(Response response, String fromUser, String topic, byte[] message, ApiCallback callback) {
sendApiMessage(response, fromUser, null, topic, message, callback, false);
}

protected void sendApiMessage(String fromUser, String topic, byte[] message, ApiCallback callback, boolean noAdmin) {
sendApiMessage(fromUser, null, topic, message, callback, noAdmin);
protected void sendApiMessage(Response response, String fromUser, String topic, byte[] message, ApiCallback callback, boolean noAdmin) {
sendApiMessage(response, fromUser, null, topic, message, callback, noAdmin);
}

protected void sendApiMessage(String fromUser, String clientId, String topic, byte[] message, ApiCallback callback, boolean noAdmin) {
protected void sendApiMessage(Response response, String fromUser, String clientId, String topic, byte[] message, ApiCallback callback, boolean noAdmin) {
ServerAPIHelper.sendRequest(fromUser, clientId, topic, message, new ServerAPIHelper.Callback() {
@Override
public void onSuccess(byte[] result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public boolean isTransactionAction() {
}

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputGetAlias input = getRequestBody(request.getNettyRequest(), InputGetAlias.class);
List<FriendData> dataList = messagesStore.getFriendList(input.getOperator(), null, 0);
Expand All @@ -51,7 +51,7 @@ public boolean action(Request request) {
}
}

setResponseContent(RestResult.ok(out));
setResponseContent(RestResult.ok(out), response);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public boolean isTransactionAction() {
}

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputUpdateAlias input = getRequestBody(request.getNettyRequest(), InputUpdateAlias.class);
ErrorCode errorCode = messagesStore.setFriendAliasRequest(input.getOperator(), input.getTargetId(), input.getAlias(), new long[1]);
setResponseContent(RestResult.resultOf(errorCode));
setResponseContent(RestResult.resultOf(errorCode), response);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand All @@ -34,23 +35,23 @@ public boolean isTransactionAction() {
}

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputBlacklistRequest inputData = getRequestBody(request.getNettyRequest(), InputBlacklistRequest.class);
if (inputData != null
&& !StringUtil.isNullOrEmpty(inputData.getUserId())
&& !StringUtil.isNullOrEmpty(inputData.getTargetUid())
) {
WFCMessage.BlackUserRequest friendRequest = WFCMessage.BlackUserRequest.newBuilder().setUid(inputData.getTargetUid()).setStatus(inputData.getStatus()).build();
sendApiMessage(inputData.getUserId(), IMTopic.BlackListUserTopic, friendRequest.toByteArray(), result -> {
sendApiMessage(response, inputData.getUserId(), IMTopic.BlackListUserTopic, friendRequest.toByteArray(), result -> {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeBytes(result);
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
return new Result(errorCode);
});
return false;
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER));
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public boolean isTransactionAction() {
}

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputUserId inputGetFriendList = getRequestBody(request.getNettyRequest(), InputUserId.class);
List<FriendData> dataList = messagesStore.getFriendList(inputGetFriendList.getUserId(), null, 0);
Expand All @@ -45,7 +45,7 @@ public boolean action(Request request) {
list.add(data.getFriendUid());
}
}
setResponseContent(RestResult.ok(new OutputStringList(list)));
setResponseContent(RestResult.ok(new OutputStringList(list)), response);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.xiaoleilu.loServer.annotation.Route;
import com.xiaoleilu.loServer.handler.Request;
import cn.wildfirechat.pojos.InputOutputUserBlockStatus;
import com.xiaoleilu.loServer.handler.Response;
import io.moquette.persistence.ServerAPIHelper;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
Expand All @@ -31,7 +32,7 @@ public boolean isTransactionAction() {
}

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputOutputUserBlockStatus inputUserBlock = getRequestBody(request.getNettyRequest(), InputOutputUserBlockStatus.class);
if (inputUserBlock != null
Expand All @@ -44,12 +45,12 @@ public boolean action(Request request) {
response.setContent(new Gson().toJson(result));

if (inputUserBlock.getStatus() == 2) {
sendApiMessage(null, ServerAPIHelper.KICKOFF_USER_REQUEST, inputUserBlock.getUserId().getBytes(), null);
sendApiMessage(response, null, ServerAPIHelper.KICKOFF_USER_REQUEST, inputUserBlock.getUserId().getBytes(), null);
}

sendResponse(response, ErrorCode.ERROR_CODE_SUCCESS, null);
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER));
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -35,11 +36,11 @@ public boolean isTransactionAction() {
}

@Override
public boolean action(Request request) {
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
BroadMessageData sendMessageData = getRequestBody(request.getNettyRequest(), BroadMessageData.class);
if (BroadMessageData.isValide(sendMessageData) && !StringUtil.isNullOrEmpty(sendMessageData.getSender())) {
sendApiMessage(sendMessageData.getSender(), IMTopic.BroadcastMessageTopic, sendMessageData.toProtoMessage().toByteArray(), result -> {
sendApiMessage(response, sendMessageData.getSender(), IMTopic.BroadcastMessageTopic, sendMessageData.toProtoMessage().toByteArray(), result -> {
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeBytes(result);
ErrorCode errorCode = ErrorCode.fromCode(byteBuf.readByte());
Expand All @@ -53,7 +54,7 @@ public boolean action(Request request) {
});
return false;
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER));
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.xiaoleilu.loServer.annotation.Route;
import com.xiaoleilu.loServer.handler.Request;
import cn.wildfirechat.pojos.InputGetUserInfo;
import com.xiaoleilu.loServer.handler.Response;
import io.moquette.persistence.ServerAPIHelper;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.internal.StringUtil;
Expand All @@ -30,11 +31,11 @@ public boolean isTransactionAction() {
}

@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 || !StringUtil.isNullOrEmpty(inputUserId.getUserId())) {
sendApiMessage(inputUserId.getUserId(), ServerAPIHelper.CHECK_USER_ONLINE_REQUEST, inputUserId.getUserId().getBytes(), res -> {
sendApiMessage(response, inputUserId.getUserId(), ServerAPIHelper.CHECK_USER_ONLINE_REQUEST, inputUserId.getUserId().getBytes(), res -> {
OutputCheckUserOnline out = new Gson().fromJson(new String(res), OutputCheckUserOnline.class);
return new Result(ErrorCode.ERROR_CODE_SUCCESS, out);
});
Expand Down
Loading

0 comments on commit b0fe3fd

Please sign in to comment.