use of com.akaxin.common.command.CommandResponse in project openzaly by akaxincom.
the class HttpFriendService method apply.
/**
* 提供扩展服务中添加好友的功能
*
* @param command
* @return
*/
public CommandResponse apply(Command command) {
CommandResponse commandResponse = new CommandResponse();
ErrorCode2 errCode = ErrorCode2.ERROR;
try {
HaiFriendApplyProto.HaiFriendApplyRequest request = HaiFriendApplyProto.HaiFriendApplyRequest.parseFrom(command.getParams());
String siteUserId = command.getSiteUserId();
String siteFriendId = request.getSiteFriendId();
String applyReason = request.getApplyReason();
LogUtils.requestDebugLog(logger, command, request.toString());
if (StringUtils.isBlank(siteUserId)) {
errCode = ErrorCode2.ERROR_PARAMETER;
} else if (siteUserId.equals(siteFriendId)) {
errCode = ErrorCode2.ERROR2_FRIEND_APPLYSELF;
} else {
int applyTimes = UserFriendDao.getInstance().getApplyCount(siteFriendId, siteUserId);
if (applyTimes >= 5) {
errCode = ErrorCode2.ERROR2_FRIEND_APPLYCOUNT;
} else {
if (UserFriendDao.getInstance().saveFriendApply(siteUserId, siteFriendId, applyReason)) {
errCode = ErrorCode2.SUCCESS;
}
}
}
if (ErrorCode2.SUCCESS.equals(errCode)) {
logger.debug("user apply friend notice. to siteUserId={}", siteFriendId);
new User2Notice().applyFriendNotice(siteFriendId);
}
} catch (Exception e) {
errCode = ErrorCode2.ERROR_SYSTEMERROR;
LogUtils.requestErrorLog(logger, command, e);
}
return commandResponse.setErrCode2(errCode);
}
use of com.akaxin.common.command.CommandResponse in project openzaly by akaxincom.
the class Bootstrap method startNettyServer.
/**
* 启动Netty服务器,提供用户与站点服务之间的长链接功能
*
* @param address
* @param port
* @throws Exception
*/
private static void startNettyServer(String address, int port) throws Exception {
new NettyServer() {
@Override
public void loadExecutor(AbstracteExecutor<Command, CommandResponse> executor) {
executor.addChain(RequestAction.IM_SITE.getName(), new ImSiteAuthHandler());
executor.addChain(RequestAction.IM.getName(), new ImMessageHandler());
executor.addChain(RequestAction.API.getName(), new ApiRequestHandler());
}
}.start(address, port);
logger.info("{} start netty server {}:{} ok.", AkxProject.PLN, address, port);
}
use of com.akaxin.common.command.CommandResponse in project openzaly by akaxincom.
the class ImSiteAuthHandler method helloResponse.
private CommandResponse helloResponse(Channel channel) {
CommandResponse commandResponse = new CommandResponse().setVersion(CommandConst.PROTOCOL_VERSION).setAction(CommandConst.ACTION_RES);
ImSiteHelloProto.ImSiteHelloResponse response = ImSiteHelloProto.ImSiteHelloResponse.newBuilder().setSiteVersion(CommandConst.SITE_VERSION).build();
commandResponse.setErrCode(ErrorCode.SUCCESS);
commandResponse.setParams(response.toByteArray());
ChannelWriter.write(channel, commandResponse);
return commandResponse;
}
use of com.akaxin.common.command.CommandResponse in project openzaly by akaxincom.
the class ImSiteAuthHandler method authResponse.
private CommandResponse authResponse(Channel channel, Command command, boolean result) {
CommandResponse commandResponse = new CommandResponse().setVersion(CommandConst.PROTOCOL_VERSION).setAction(CommandConst.ACTION_RES);
ErrorCode2 errCode = ErrorCode2.ERROR2_IMAUTH_FAIL;
if (result) {
String siteServer = ServerAddressUtils.getAddressPort();
ImSiteAuthProto.ImSiteAuthResponse authResponse = ImSiteAuthProto.ImSiteAuthResponse.newBuilder().setSiteServer(siteServer).build();
commandResponse.setParams(authResponse.toByteArray());
errCode = ErrorCode2.SUCCESS;
ChannelWriter.write(channel, commandResponse.setErrCode2(errCode));
} else {
ChannelWriter.writeAndClose(channel, commandResponse.setErrCode2(errCode));
}
logger.debug("{} client={} siteUserId={} action={} auth response result={}", AkxProject.PLN, command.getClientIp(), command.getSiteUserId(), RequestAction.IM_SITE_AUTH, errCode.toString());
return commandResponse;
}
use of com.akaxin.common.command.CommandResponse in project openzaly by akaxincom.
the class HttpServerHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
/**
* http-request包含: <br>
* 1.请求行 ,Method Request-URI Http-Version CRLF<br>
* 2.消息头 <br>
* 3.请求正文 <br>
*/
if (msg instanceof HttpRequest) {
request = (HttpRequest) msg;
if (!checkLegalRequest()) {
logger.error("{} http request method error. please use post!", AkxProject.PLN);
ctx.close();
return;
}
String clientIp = request.headers().get(HttpConst.HTTP_H_FORWARDED);
if (clientIp == null) {
InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
clientIp = address.getAddress().getHostAddress();
}
if (!checkLegalClientIp(clientIp)) {
logger.error("{} http request illegal IP={}.", AkxProject.PLN, clientIp);
ctx.close();
return;
}
logger.debug("{} request uri:{} clientIp={}", AkxProject.PLN, request.uri(), clientIp);
}
/**
* HttpContent:表示HTTP实体正文和内容标头的基类 <br>
* method.name=POST 传输消息体存在内容
*/
if (msg instanceof LastHttpContent) {
HttpContent content = (HttpContent) msg;
ByteBuf httpByteBuf = content.content();
if (httpByteBuf == null) {
return;
}
if (!checkLegalRequest()) {
ctx.close();
return;
}
String clientIp = request.headers().get(HttpConst.HTTP_H_FORWARDED);
String sitePluginId = request.headers().get(PluginConst.SITE_PLUGIN_ID);
byte[] contentBytes = new byte[httpByteBuf.readableBytes()];
httpByteBuf.readBytes(contentBytes);
httpByteBuf.release();
// 查询扩展的auth——key
String authKey = PluginSession.getInstance().getPluginAuthKey(sitePluginId);
if (StringUtils.isNotEmpty(authKey)) {
// byte[] tsk = AESCrypto.generateTSKey(authKey);
byte[] tsk = authKey.getBytes(CharsetCoding.ISO_8859_1);
byte[] decContent = AESCrypto.decrypt(tsk, contentBytes);
contentBytes = decContent;
}
PluginProto.ProxyPluginPackage pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(contentBytes);
Map<Integer, String> proxyHeader = pluginPackage.getPluginHeaderMap();
String requestTime = proxyHeader.get(PluginProto.PluginHeaderKey.PLUGIN_TIMESTAMP_VALUE);
long currentTime = System.currentTimeMillis();
boolean timeOut = true;
if (StringUtils.isNotEmpty(requestTime)) {
long timeMills = Long.valueOf(requestTime);
if (currentTime - timeMills < 10 * 1000l) {
timeOut = false;
}
}
logger.debug("{} client={} http request timeOut={} currTime={} reqTime={}", AkxProject.PLN, clientIp, timeOut, currentTime, requestTime);
if (!timeOut) {
Command command = new Command();
command.setField(PluginConst.PLUGIN_AUTH_KEY, authKey);
if (proxyHeader != null) {
command.setSiteUserId(proxyHeader.get(PluginProto.PluginHeaderKey.CLIENT_SITE_USER_ID_VALUE));
}
command.setChannelContext(ctx);
command.setUri(request.uri());
command.setParams(Base64.getDecoder().decode(pluginPackage.getData()));
command.setClientIp(clientIp);
command.setStartTime(System.currentTimeMillis());
logger.debug("{} client={} http server handler command={}", AkxProject.PLN, clientIp, command.toString());
CommandResponse response = this.executor.execute(HttpUriAction.HTTP_ACTION.getUri(), command);
LogUtils.requestResultLog(logger, command, response);
} else {
// 超时10s,认为此请求失效,直接断开连接
ctx.close();
logger.error("{} client={} http request error.timeOut={} currTime={} reqTime={}", AkxProject.PLN, clientIp, timeOut, currentTime, requestTime);
}
}
} catch (Exception e) {
ctx.close();
logger.error(StringHelper.format("{} http request error.", AkxProject.PLN), e);
}
}
Aggregations