package com.tass;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.tass.entity.CommonRequest;
import com.tass.service.AuthManageService;
import com.tass.service.DeviceInitService;
import com.tass.service.DownLoadService;
import com.tass.service.InitConnectionService;
import com.tass.service.KeyAuthService;
import com.tass.service.KeyReposityEccService;
import com.tass.service.KeyReposityRsaService;
import com.tass.service.KeyReposityService;
import com.tass.service.KeyRepositySymService;
import com.tass.service.ManagerService;
import com.tass.service.RestartHSMService;
import com.tass.service.SslManageService;
import com.tass.service.SystemUpgradeService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;
/**
* @ClassDes:描述信息(ChannelInboundHandler是用来接收消息,而ChannelInboundHandlerAdapter是实现于该类,而业务逻辑的生命周期在ChannelInboundHandler中):
* @author 郭德龙
* @time 2018年7月10日
* @package com.netty.server
* @class EchoServerHandler
* • ChannelHandlerAdapter
* • ChannelInboundHandlerAdapter
* • ChannelOutboundHandlerAdapter
* 以上三种继承方法
*
*/
@Sharable
@Component
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
@Autowired
private InitConnectionService initConnectionService;
@Autowired
private KeyRepositySymService keySymService;
@Autowired
private KeyReposityRsaService keyRsaService;
@Autowired
private KeyReposityEccService keyReposityEccService;
@Autowired
private KeyReposityService keyReposityService;
@Autowired
private DownLoadService downLoadService;
@Autowired
private DeviceInitService deviceInitService;
@Autowired
private SslManageService sslManageService;
@Autowired
private AuthManageService authManageService;
@Autowired
private SystemUpgradeService systemUpgradeService;
@Autowired
private KeyAuthService keyAuthService;
@Autowired
private RestartHSMService restartHSMService;
@Autowired
private ManagerService managerService;
private String result = "";
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (!(msg instanceof FullHttpRequest)) {
result = "未知请求!";
send(ctx, result, HttpResponseStatus.BAD_REQUEST);
return;
}
FullHttpRequest httpRequest = (FullHttpRequest) msg;
try {
HttpHeaders headers = httpRequest.headers();// 获取请求头信息
String path = httpRequest.uri(); // 获取路径
System.out.println("path==="+path);
String body = getBody(httpRequest); // 获取参数
System.out.println("body==="+body);
HttpMethod method = httpRequest.method();// 获取请求方法
// 只接受POST请求,如果是GET请求返回
if (HttpMethod.GET.equals(method)) {
send(ctx, "The service accepts only post requests", HttpResponseStatus.OK);
return;
}
//body空值判断
if (body == null || "".equals(body)) {
send(ctx, "The request body can not be null or empty!", HttpResponseStatus.OK);
return;
}
// 根据路径作出业务逻辑处理
if (Constants.URL_HELLO.equalsIgnoreCase(path)) {
String responseStr = initConnectionService.initConnection(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
} else if (Constants.URL_KEY_SYM_LIST.equalsIgnoreCase(path)) {
//获取对称密钥列表
String responseStr = keySymService.SymKeyList(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_RSA_LIST.equalsIgnoreCase(path)) {
//获取RSA非对称密钥列表
String responseStr = keyRsaService.getRsaKeyList(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_ECC_LIST.equalsIgnoreCase(path)) {
//获取ECC非对称密钥列表
String responseStr = keyReposityEccService.EccKeyList(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_SYM_GENERATE.equalsIgnoreCase(path)) {
//产生对称密钥
String responseStr = keySymService.SymKeyGenerate(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_RSA_GENERATE.equalsIgnoreCase(path)) {
//产生非对称密钥RSA
String responseStr = keyRsaService.getRsaKeyGenerate(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_ECC_GENERATE.equalsIgnoreCase(path)) {
//产生非对称密钥ECC
String responseStr = keyReposityEccService.EccKeyGenerate(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_SYM_INFO.equalsIgnoreCase(path)) {
//获取对称密钥详情
String responseStr = keySymService.SymKeyInfo(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_RSA_INFO.equalsIgnoreCase(path)) {
//获取RSA密钥详情
String responseStr = keyRsaService.getRsaKeyInfo(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_ECC_INFO.equalsIgnoreCase(path)) {
//获取ECC密钥详情
String responseStr = keyReposityEccService.EccKeyGetKeyInfo(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_SYM_DEL.equalsIgnoreCase(path)) {
//删除对称密钥
String responseStr = keySymService.SymKeyDelete(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_RSA_DELETE.equalsIgnoreCase(path)) {
//删除RSA密钥
String responseStr = keyRsaService.getRsaKeyDelete(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_ECC_DELETE.equalsIgnoreCase(path)) {
//删除ECC密钥
String responseStr = keyReposityEccService.EccKeyDelete(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_RSA_CLEAR.equalsIgnoreCase(path)) {
//清除非对称密钥
String responseStr = keyRsaService.getRsaKeyClear(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_SYM_CLEAR.equalsIgnoreCase(path)) {
//清除对称密钥
String responseStr = keySymService.SymKeyClear(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_EXPORT.equalsIgnoreCase(path)) {
//导出密钥
String responseStr = keyReposityService.ExportKey(getCommonRequest(ctx,headers,body));
send(ctx, responseStr, HttpResponseStatus.OK);
}else if (Constants.URL_KEY_IMPORT.equalsIgnoreCase(path)) {
//导入密钥
String responseStr = keyReposityService.ImportKey(getCommonRequest(ctx,he