Compare commits

...
5 Commits
20 changed files with 128 additions and 25 deletions
@@ -21,6 +21,7 @@ import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import com.xiaoleilu.loServer.action.ClassUtil;
import org.slf4j.LoggerFactory;
import win.liyufan.im.Utility;
import java.io.IOException;
@@ -141,6 +142,7 @@ public class LoServer {
}
} catch (IOException e) {
e.printStackTrace();
Utility.printExecption(Logger, e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
@@ -28,7 +28,7 @@ public class UnknownErrorAction extends Action{
@Override
public boolean action(Request request, Response response) {
response.setStatus(HttpResponseStatus.NOT_FOUND);
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
return true;
}
}
@@ -9,6 +9,7 @@
package com.xiaoleilu.loServer.action.admin;
import com.google.gson.Gson;
import com.xiaoleilu.loServer.LoServer;
import com.xiaoleilu.loServer.RestResult;
import com.xiaoleilu.loServer.action.Action;
import com.xiaoleilu.loServer.handler.Request;
@@ -17,9 +18,12 @@ import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.internal.StringUtil;
import org.apache.commons.codec.digest.DigestUtils;
import cn.wildfirechat.common.ErrorCode;
import org.slf4j.LoggerFactory;
import win.liyufan.im.RateLimiter;
import win.liyufan.im.Utility;
abstract public class AdminAction extends Action {
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(AdminAction.class);
private static String SECRET_KEY = "123456";
private static boolean NO_CHECK_TIME = false;
private final RateLimiter mLimitCounter = new RateLimiter(10, 500);
@@ -52,6 +56,7 @@ abstract public class AdminAction extends Action {
ts = Long.parseLong(timestamp);
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
return ErrorCode.INVALID_PARAMETER;
}
@@ -22,11 +22,15 @@ import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.internal.StringUtil;
import cn.wildfirechat.common.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import win.liyufan.im.UUIDGenerator;
import win.liyufan.im.Utility;
@Route(APIPath.Create_Robot)
@HttpMethod("POST")
public class CreateRobotAction extends AdminAction {
private static final Logger LOG = LoggerFactory.getLogger(CreateRobotAction.class);
@Override
public boolean isTransactionAction() {
@@ -55,7 +59,16 @@ public class CreateRobotAction extends AdminAction {
WFCMessage.User newUser = inputCreateRobot.toUser();
messagesStore.addUserInfo(newUser, inputCreateRobot.getPassword());
try {
messagesStore.addUserInfo(newUser, inputCreateRobot.getPassword());
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
response.setStatus(HttpResponseStatus.OK);
RestResult result = RestResult.resultOf(ErrorCode.ERROR_CODE_SERVER_ERROR, e.getMessage());
response.setContent(new Gson().toJson(result));
return true;
}
if (StringUtil.isNullOrEmpty(inputCreateRobot.getOwner())) {
inputCreateRobot.setOwner(inputCreateRobot.getUserId());
@@ -22,11 +22,16 @@ import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.internal.StringUtil;
import cn.wildfirechat.common.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import win.liyufan.im.DBUtil;
import win.liyufan.im.UUIDGenerator;
import win.liyufan.im.Utility;
@Route(APIPath.Create_User)
@HttpMethod("POST")
public class CreateUserAction extends AdminAction {
private static final Logger LOG = LoggerFactory.getLogger(CreateUserAction.class);
@Override
public boolean isTransactionAction() {
@@ -75,7 +80,17 @@ public class CreateUserAction extends AdminAction {
newUserBuilder.setUpdateDt(System.currentTimeMillis());
messagesStore.addUserInfo(newUserBuilder.build(), inputCreateUser.getPassword());
try {
messagesStore.addUserInfo(newUserBuilder.build(), inputCreateUser.getPassword());
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
response.setStatus(HttpResponseStatus.OK);
RestResult result = RestResult.resultOf(ErrorCode.ERROR_CODE_SERVER_ERROR, e.getMessage());
response.setContent(new Gson().toJson(result));
return true;
}
response.setStatus(HttpResponseStatus.OK);
RestResult result = RestResult.ok(new OutputCreateUser(inputCreateUser.getUserId(), inputCreateUser.getName()));
@@ -12,6 +12,7 @@ import cn.wildfirechat.proto.WFCMessage;
import com.google.gson.Gson;
import com.xiaoleilu.loServer.RestResult;
import com.xiaoleilu.loServer.action.Action;
import com.xiaoleilu.loServer.action.admin.AdminAction;
import com.xiaoleilu.loServer.handler.Request;
import com.xiaoleilu.loServer.handler.Response;
import io.moquette.spi.impl.Utils;
@@ -21,9 +22,12 @@ import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.internal.StringUtil;
import org.apache.commons.codec.digest.DigestUtils;
import cn.wildfirechat.common.ErrorCode;
import org.slf4j.LoggerFactory;
import win.liyufan.im.RateLimiter;
import win.liyufan.im.Utility;
abstract public class ChannelAction extends Action {
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(ChannelAction.class);
private final RateLimiter mLimitCounter = new RateLimiter(10, 200);
protected WFCMessage.ChannelInfo channelInfo;
@@ -47,6 +51,7 @@ abstract public class ChannelAction extends Action {
ts = Long.parseLong(timestamp);
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
return ErrorCode.INVALID_PARAMETER;
}
@@ -14,6 +14,7 @@ import com.xiaoleilu.loServer.RestResult;
import com.xiaoleilu.loServer.action.Action;
import com.xiaoleilu.loServer.handler.Request;
import com.xiaoleilu.loServer.handler.Response;
import io.moquette.imhandler.IMHandler;
import io.moquette.spi.impl.Utils;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpRequest;
@@ -21,11 +22,15 @@ import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.internal.StringUtil;
import org.apache.commons.codec.digest.DigestUtils;
import cn.wildfirechat.common.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import win.liyufan.im.RateLimiter;
import win.liyufan.im.Utility;
import java.io.UnsupportedEncodingException;
abstract public class RobotAction extends Action {
protected static final Logger LOG = LoggerFactory.getLogger(RobotAction.class);
private final RateLimiter mLimitCounter = new RateLimiter(10, 1000);
protected WFCMessage.Robot robot;
@@ -49,6 +54,7 @@ abstract public class RobotAction extends Action {
ts = Long.parseLong(timestamp);
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
return ErrorCode.INVALID_PARAMETER;
}
@@ -89,6 +95,7 @@ abstract public class RobotAction extends Action {
return t;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
}
return null;
@@ -21,6 +21,7 @@ import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import win.liyufan.im.Utility;
/**
* Action处理单元
@@ -52,6 +53,7 @@ abstract public class ActionHandler extends SimpleChannelInboundHandler<FullHttp
}
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(Logger, e);
Action errorAction = ServerSetting.getErrorAction(ServerSetting.MAPPING_ERROR);
request.putParam(UnknownErrorAction.ERROR_PARAM_NAME, e);
response.setContent(e.toString());
@@ -20,6 +20,7 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,7 +38,7 @@ public class IMActionHandler extends ActionHandler {
@Override
boolean isValidePath(String path) {
if (!path.startsWith("/admin")) {
if (!StringUtil.isNullOrEmpty(path) && !path.startsWith("/admin")) {
return true;
}
return false;
@@ -8,6 +8,7 @@
package io.moquette.imhandler;
import cn.wildfirechat.proto.ProtoConstants;
import cn.wildfirechat.proto.WFCMessage;
import io.moquette.spi.impl.Qos1PublishHandler;
import io.netty.buffer.ByteBuf;
@@ -24,6 +25,13 @@ public class LoadRemoteMessagesHandler extends IMHandler<WFCMessage.LoadRemoteMe
if (beforeUid == 0) {
beforeUid = Long.MAX_VALUE;
}
if (request.getConversation().getType() == ProtoConstants.ConversationType.ConversationType_Group) {
if (!m_messagesStore.isMemberInGroup(fromUser, request.getConversation().getTarget())) {
return ErrorCode.ERROR_CODE_NOT_IN_GROUP;
}
}
WFCMessage.PullMessageResult result = m_messagesStore.loadRemoteMessages(fromUser, request.getConversation(), beforeUid, request.getCount());
byte[] data = result.toByteArray();
LOG.info("User {} load message with count({}), payload size({})", fromUser, result.getMessageCount(), data.length);
@@ -13,11 +13,18 @@ import io.moquette.spi.impl.Qos1PublishHandler;
import io.netty.buffer.ByteBuf;
import cn.wildfirechat.common.ErrorCode;
import win.liyufan.im.IMTopic;
import win.liyufan.im.Utility;
@Handler(IMTopic.ModifyMyInfoTopic)
public class ModifyMyInfoHandler extends IMHandler<WFCMessage.ModifyMyInfoRequest> {
@Override
public ErrorCode action(ByteBuf ackPayload, String clientID, String fromUser, boolean isAdmin, WFCMessage.ModifyMyInfoRequest request, Qos1PublishHandler.IMCallback callback) {
try {
return m_messagesStore.modifyUserInfo(fromUser, request);
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
return ErrorCode.ERROR_CODE_SERVER_ERROR;
}
}
}
@@ -17,6 +17,7 @@ import io.netty.buffer.ByteBuf;
import cn.wildfirechat.common.ErrorCode;
import win.liyufan.im.IMTopic;
import win.liyufan.im.MessageShardingUtil;
import win.liyufan.im.Utility;
import java.util.Set;
@@ -39,12 +40,14 @@ public class SendMessageHandler extends IMHandler<WFCMessage.Message> {
mSensitiveType = Integer.parseInt(mServer.getConfig().getProperty(BrokerConstants.SENSITIVE_Filter_Type));
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
try {
mBlacklistStrategy = Integer.parseInt(mServer.getConfig().getProperty(BrokerConstants.MESSAGE_Blacklist_Strategy));
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
}
@@ -2128,7 +2128,7 @@ public class DatabaseStore {
});
}
void updateUser(final WFCMessage.User user) {
void updateUser(final WFCMessage.User user) throws Exception {
LOG.info("Database update user info {} {}", user.getUid(), user.getUpdateDt());
Connection connection = null;
PreparedStatement statement = null;
@@ -2202,6 +2202,7 @@ public class DatabaseStore {
// TODO Auto-generated catch block
e.printStackTrace();
Utility.printExecption(LOG, e);
throw new Exception(e.getMessage());
} finally {
DBUtil.closeDB(connection, statement);
}
@@ -138,30 +138,35 @@ public class MemoryMessagesStore implements IMessagesStore {
mDisableStrangerChat = Boolean.parseBoolean(m_Server.getConfig().getProperty(BrokerConstants.MESSAGE_Disable_Stranger_Chat));
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
try {
mFriendRequestDuration = Long.parseLong(m_Server.getConfig().getProperty(FRIEND_Repeat_Request_Duration));
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
try {
mFriendRejectDuration = Long.parseLong(m_Server.getConfig().getProperty(FRIEND_Reject_Request_Duration));
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
try {
mChatroomRejoinWhenActive = Boolean.parseBoolean(m_Server.getConfig().getProperty(BrokerConstants.CHATROOM_Rejoin_When_Active));
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
try {
mChatroomParticipantIdleTime = Long.parseLong(m_Server.getConfig().getProperty(CHATROOM_Participant_Idle_Time, "900000"));
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
}
@@ -592,6 +597,14 @@ public class MemoryMessagesStore implements IMessagesStore {
MemorySessionStore.Session session = m_Server.getStore().sessionsStore().getSession(clientId);
String existChatroomId = (String)m_Server.getHazelcastInstance().getMap(USER_CHATROOM).get(user);
if (chatroomId == null) {
if (existChatroomId == null) {
return false;
} else {
chatroomId = existChatroomId;
}
}
if (StringUtil.isNullOrEmpty(existChatroomId) || !existChatroomId.equals(chatroomId)) {
if (mChatroomRejoinWhenActive) {
if (!StringUtil.isNullOrEmpty(existChatroomId)) {
@@ -1259,8 +1272,9 @@ public class MemoryMessagesStore implements IMessagesStore {
if (members == null || members.size() == 0) {
members = loadGroupMemberFromDB(hzInstance, groupId);
}
for (WFCMessage.GroupMember member : members
) {
boolean isInGroup = false;
for (WFCMessage.GroupMember member : members) {
if (member.getMemberId().equals(memberId)) {
if (member.getType() == GroupMemberType_Silent) {
return ErrorCode.ERROR_CODE_NOT_RIGHT;
@@ -1273,11 +1287,16 @@ public class MemoryMessagesStore implements IMessagesStore {
if (member.getMemberId().equals(memberId) && member.getType() == GroupMemberType_Removed) {
return ErrorCode.ERROR_CODE_NOT_IN_GROUP;
}
isInGroup = true;
break;
}
}
if (!isInGroup) {
return ErrorCode.ERROR_CODE_NOT_IN_GROUP;
}
return ErrorCode.ERROR_CODE_SUCCESS;
}
@@ -1411,7 +1430,7 @@ public class MemoryMessagesStore implements IMessagesStore {
}
@Override
public ErrorCode modifyUserInfo(String userId, WFCMessage.ModifyMyInfoRequest request) {
public ErrorCode modifyUserInfo(String userId, WFCMessage.ModifyMyInfoRequest request) throws Exception {
HazelcastInstance hzInstance = m_Server.getHazelcastInstance();
IMap<String, WFCMessage.User> mUserMap = hzInstance.getMap(USERS);
@@ -1474,8 +1493,8 @@ public class MemoryMessagesStore implements IMessagesStore {
if (modified) {
builder.setUpdateDt(System.currentTimeMillis());
user = builder.build();
databaseStore.updateUser(user);
mUserMap.set(userId, user);
// databaseStore.updateUser(user);
return ErrorCode.ERROR_CODE_SUCCESS;
} else {
return ErrorCode.ERROR_CODE_NOT_MODIFIED;
@@ -1520,9 +1539,10 @@ public class MemoryMessagesStore implements IMessagesStore {
}
@Override
public void addUserInfo(WFCMessage.User user, String password) {
public void addUserInfo(WFCMessage.User user, String password) throws Exception {
HazelcastInstance hzInstance = m_Server.getHazelcastInstance();
IMap<String, WFCMessage.User> mUserMap = hzInstance.getMap(USERS);
databaseStore.updateUser(user);
mUserMap.put(user.getUid(), user);
databaseStore.updateUserPassword(user.getUid(), password);
}
@@ -1533,7 +1553,13 @@ public class MemoryMessagesStore implements IMessagesStore {
IMap<String, WFCMessage.User> mUserMap = hzInstance.getMap(USERS);
WFCMessage.User us = mUserMap.get(userId);
if (us != null) {
us = WFCMessage.User.newBuilder().setUid(userId).setUpdateDt(System.currentTimeMillis()).setDisplayName("已删除用户").setName(userId).setDeleted(1).build();
us = WFCMessage.User.newBuilder().setUid(userId).setUpdateDt(System.currentTimeMillis()).setDisplayName("").setName(userId).setDeleted(1).build();
try {
databaseStore.updateUser(us);
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
mUserMap.put(userId, us);
}
@@ -2284,6 +2310,7 @@ public class MemoryMessagesStore implements IMessagesStore {
}
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
}
@@ -2494,6 +2521,7 @@ public class MemoryMessagesStore implements IMessagesStore {
newInfoBuilder.setAutomatic(Integer.parseInt(value));
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
}
@@ -29,6 +29,7 @@ import io.moquette.spi.IMessagesStore.StoredMessage;
import io.netty.handler.codec.mqtt.MqttVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import win.liyufan.im.Utility;
import java.util.*;
import java.util.concurrent.*;
@@ -272,6 +273,7 @@ public class MemorySessionStore implements ISessionsStore {
supportMultiEndpoint = Boolean.parseBoolean(server.getConfig().getProperty(BrokerConstants.SERVER_MULTI_ENDPOINT));
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
}
@@ -19,7 +19,7 @@ import java.util.Map;
public class UserLoader implements MapStore<String, WFCMessage.User> {
@Override
public void store(String s, WFCMessage.User user) {
getDatabaseStore().updateUser(user);
// getDatabaseStore().updateUser(user);
}
@Override
@@ -129,13 +129,13 @@ public interface IMessagesStore {
WFCMessage.Robot getRobot(String robotId);
void addRobot(WFCMessage.Robot robot);
ErrorCode getUserInfo(List<WFCMessage.UserRequest> requestList, WFCMessage.PullUserResult.Builder builder);
ErrorCode modifyUserInfo(String userId, WFCMessage.ModifyMyInfoRequest request);
ErrorCode modifyUserInfo(String userId, WFCMessage.ModifyMyInfoRequest request) throws Exception;
ErrorCode modifyUserStatus(String userId, int status);
int getUserStatus(String userId);
List<InputOutputUserBlockStatus> getUserStatusList();
void addUserInfo(WFCMessage.User user, String password);
void addUserInfo(WFCMessage.User user, String password) throws Exception;
void destoryUser(String userId);
WFCMessage.User getUserInfo(String userId);
WFCMessage.User getUserInfoByName(String name);
@@ -87,6 +87,7 @@ public class MessagesPublisher {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
chatroomScheduler.shutdown();
}
@@ -187,6 +187,7 @@ public class Qos1PublishHandler extends QosPublishHandler {
gzip.close();
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
data = out.toByteArray();
code = (byte)ErrorCode.ERROR_CODE_SUCCESS_GZIPED.code;
@@ -67,31 +67,33 @@ public class AdminHttpUtils {
HttpResponse response = httpClient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK){
LOG.info("Request error: "+statusCode);
throw new Exception("Http request error with code:" + statusCode);
}else{
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent(),"utf-8"));
StringBuffer sb = new StringBuffer();
String content = null;
if (response.getEntity().getContentLength() > 0) {
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"));
StringBuilder sb = new StringBuilder();
String line;
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
sb.append(line).append(NL);
}
in.close();
String content = sb.toString();
content = sb.toString();
LOG.info("http request response content: {}", content);
}
if(statusCode != HttpStatus.SC_OK){
LOG.info("Request error: " + statusCode + " error msg:" + content);
throw new Exception("Http request error with code:" + statusCode);
} else {
return fromJsonObject(content, clazz);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
if(post != null){
} finally {
if(post != null) {
post.releaseConnection();
}
}