server api添加日志
This commit is contained in:
@@ -8,9 +8,13 @@
|
|||||||
|
|
||||||
package com.xiaoleilu.loServer.action;
|
package com.xiaoleilu.loServer.action;
|
||||||
|
|
||||||
|
import cn.wildfirechat.common.IMExceptionEvent;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonSyntaxException;
|
||||||
import com.xiaoleilu.loServer.RestResult;
|
import com.xiaoleilu.loServer.RestResult;
|
||||||
|
import com.xiaoleilu.loServer.action.channel.ChannelAction;
|
||||||
|
import com.xiaoleilu.loServer.action.robot.RobotAction;
|
||||||
import com.xiaoleilu.loServer.annotation.RequireAuthentication;
|
import com.xiaoleilu.loServer.annotation.RequireAuthentication;
|
||||||
import com.xiaoleilu.loServer.handler.Request;
|
import com.xiaoleilu.loServer.handler.Request;
|
||||||
import com.xiaoleilu.loServer.handler.Response;
|
import com.xiaoleilu.loServer.handler.Response;
|
||||||
@@ -23,7 +27,10 @@ import io.netty.handler.codec.http.FullHttpRequest;
|
|||||||
import io.netty.handler.codec.http.HttpRequest;
|
import io.netty.handler.codec.http.HttpRequest;
|
||||||
import io.netty.handler.codec.http.HttpResponseStatus;
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||||
import cn.wildfirechat.common.ErrorCode;
|
import cn.wildfirechat.common.ErrorCode;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import win.liyufan.im.RateLimiter;
|
import win.liyufan.im.RateLimiter;
|
||||||
|
import win.liyufan.im.Utility;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
@@ -38,6 +45,7 @@ import static io.moquette.BrokerConstants.*;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
abstract public class Action {
|
abstract public class Action {
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(Action.class);
|
||||||
public static IMessagesStore messagesStore = null;
|
public static IMessagesStore messagesStore = null;
|
||||||
public static ISessionsStore sessionsStore = null;
|
public static ISessionsStore sessionsStore = null;
|
||||||
protected static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
protected static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
||||||
@@ -154,8 +162,21 @@ abstract public class Action {
|
|||||||
byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content());
|
byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content());
|
||||||
String content = new String(bytes, StandardCharsets.UTF_8);//contribute by JiaRG from github
|
String content = new String(bytes, StandardCharsets.UTF_8);//contribute by JiaRG from github
|
||||||
|
|
||||||
T t = gson.fromJson(content, cls);
|
try {
|
||||||
return t;
|
T t = gson.fromJson(content, cls);
|
||||||
|
return t;
|
||||||
|
} catch (JsonSyntaxException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
LOG.error("Object {} from json {} failure", cls.getName(), content);
|
||||||
|
int exception = IMExceptionEvent.EventType.ADMIN_API_Exception;
|
||||||
|
if (this instanceof RobotAction) {
|
||||||
|
exception = IMExceptionEvent.EventType.ROBOT_API_Exception;
|
||||||
|
} else if (this instanceof ChannelAction) {
|
||||||
|
exception = IMExceptionEvent.EventType.CHANNEL_API_Exception;
|
||||||
|
}
|
||||||
|
Utility.printExecption(LOG, e, exception);
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,18 +105,6 @@ abstract public class ChannelAction extends Action {
|
|||||||
response.send();
|
response.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T> T getRequestBody(HttpRequest request, Class<T> cls) {
|
|
||||||
if (request instanceof FullHttpRequest) {
|
|
||||||
FullHttpRequest fullHttpRequest = (FullHttpRequest) request;
|
|
||||||
byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content());
|
|
||||||
String content = new String(bytes, StandardCharsets.UTF_8);
|
|
||||||
|
|
||||||
T t = gson.fromJson(content, cls);
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void sendApiMessage(Response response, String topic, byte[] message, Callback callback) {
|
protected void sendApiMessage(Response response, String topic, byte[] message, Callback callback) {
|
||||||
sendApiMessage(response, channelInfo.getOwner(), topic, message, callback);
|
sendApiMessage(response, channelInfo.getOwner(), topic, message, callback);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,24 +106,6 @@ abstract public class RobotAction extends Action {
|
|||||||
response.send();
|
response.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T> T getRequestBody(HttpRequest request, Class<T> cls) {
|
|
||||||
if (request instanceof FullHttpRequest) {
|
|
||||||
FullHttpRequest fullHttpRequest = (FullHttpRequest) request;
|
|
||||||
byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content());
|
|
||||||
String content = null;
|
|
||||||
try {
|
|
||||||
content = new String(bytes, "UTF-8");
|
|
||||||
|
|
||||||
T t = gson.fromJson(content, cls);
|
|
||||||
return t;
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Utility.printExecption(LOG, e, IMExceptionEvent.EventType.ROBOT_API_Exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void sendApiRequest(Response response, String topic, byte[] message, Callback callback) {
|
protected void sendApiRequest(Response response, String topic, byte[] message, Callback callback) {
|
||||||
ServerAPIHelper.sendRequest(robot.getUid(), null, topic, message, new ServerAPIHelper.Callback() {
|
ServerAPIHelper.sendRequest(robot.getUid(), null, topic, message, new ServerAPIHelper.Callback() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user