com.addplus.server.action.config.system.AddplusContainer Maven / Gradle / Ivy
The newest version!
package com.addplus.server.action.config.system;
import com.addplus.server.action.config.constant.ConfigConsts;
import com.addplus.server.core.constant.ConfigConstsBase;
import com.addplus.server.core.exception.ErrorCodeBase;
import com.addplus.server.core.exception.ErrorException;
import com.addplus.server.core.model.base.ReturnDataSet;
import com.addplus.server.core.model.base.ServiceMap;
import com.addplus.server.core.utils.SortUtil;
import com.addplus.server.core.utils.security.AESUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.reflectasm.MethodAccess;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Parameter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
/**
* 类名: AddplusContainer
*
* @author zhangjiehang
* @version V1.0
* 时间: 2020/5/27 2:25 下午
* 类描述: 通用请求共用类
*/
@Slf4j
public class AddplusContainer {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private HttpServletRequest request;
@Value("${action.param.encrypted:false}")
private Boolean encryted;
@Value("${action.param.model:com.addplus.server}")
private String modePrefix;
private static AddplusContainer addplusContainer;
private Map serviceClassMap;
private String packageName;
private Map descriptMap;
private Map serviceParameterMap;
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static AddplusContainer newInstance(ServiceMap serviceMap, String packageName) {
if (addplusContainer == null) {
addplusContainer = new AddplusContainer(serviceMap.getServiceClassMap(), serviceMap.getServiceParameterMap(), serviceMap.getDecrtipetMap(), packageName);
}
return addplusContainer;
}
private AddplusContainer(Map serviceClassMap, Map serviceParameterMap, Map descriptMap, String packageName) {
this.descriptMap = descriptMap;
this.serviceClassMap = serviceClassMap;
this.serviceParameterMap = serviceParameterMap;
this.packageName = packageName;
}
private AddplusContainer() {
}
public String encryptAESContent(ReturnDataSet returnDataSet, String className, String method) {
StringJoiner stringJoiner = new StringJoiner(".");
stringJoiner.add(packageName);
stringJoiner.add(className);
String invokeName = stringJoiner.toString() + "_" + method;
return AESUtils.encryptAES(JSON.toJSONString(returnDataSet), descriptMap.get(invokeName), 1);
}
public Object sortMapToPojo(String context, Parameter[] parameters, String invokeName, Boolean encryted, String module) throws Exception {
if (encryted && ConfigConsts.REST.equals(module)) {
context = AESUtils.decryptAES(context, descriptMap.get(invokeName), 1);
}
if (parameters.length > 0) {
try {
// 当只有一个入参,判断是否是基本类型
if (parameters.length == 1) {
// 判断jsonObject里面是否有对应参数名称,若没有判断认为是实体类
String classTypeName = parameters[0].getParameterizedType().getTypeName();
boolean isProject = classTypeName.startsWith(modePrefix);
if (!isProject){
isProject = classTypeName.startsWith(ConfigConsts.MODE_PREFIX);
}
if (isProject) {
return JSON.parseObject(context, parameters[0].getParameterizedType());
} else {
if ( ConfigConsts.T.equals(parameters[0].getParameterizedType().getTypeName())) {
return JSON.parseObject(context, parameters[0].getType());
} else {
JSONObject jsonObject = JSON.parseObject(context);
String params = jsonObject.getString(parameters[0].getName());
if (StringUtils.isNotBlank(params)) {
if (String.class.equals(parameters[0].getParameterizedType())) {
return params;
} else if (LocalDateTime.class.equals(parameters[0].getParameterizedType())) {
return LocalDateTime.parse(params, dateTimeFormatter);
} else {
return JSON.parseObject(params, parameters[0].getParameterizedType());
}
}
}
}
}
return SortUtil.sortMapByKey(JSON.parseObject(context), parameters);
} catch (JSONException exception) {
log.error("入参转换类型错误,原因:[{}]", exception.getMessage());
throw new ErrorException(ErrorCodeBase.CLIENT_ERROR_DATA_FORMAT);
}
}
return null;
}
public ReturnDataSet invoke(String module, String serviceName, String methodName, String ipAddress, Object param, Boolean httpType, Object token) throws Exception {
ReturnDataSet returnDataSet = new ReturnDataSet();
if (!httpType && null == param) {
returnDataSet.setErrorCode(ErrorCodeBase.SYSTEM_ERROR_TYPE_LENGTH);
return returnDataSet;
}
StringJoiner stringJoiner = new StringJoiner(".");
stringJoiner.add(packageName);
stringJoiner.add(serviceName);
String geneticClass = stringJoiner.toString();
String invokeName = geneticClass + "_" + methodName;
if (!this.serviceClassMap.containsKey(geneticClass)) {
// 接口不存在
returnDataSet.setErrorCode(ErrorCodeBase.SYSTEM_ERROR_PATH);
return returnDataSet;
}
Parameter[] parameters = this.serviceParameterMap.get(invokeName);
if (parameters == null) {
returnDataSet.setErrorCode(ErrorCodeBase.SYSTEM_ERROR_PATH);
return returnDataSet;
}
Map params = null;
if (httpType) {
if (parameters.length > 0) {
Map map = (Map) param;
if (map.isEmpty()) {
returnDataSet.setErrorCode(ErrorCodeBase.SYSTEM_ERROR_TYPE_LENGTH);
return returnDataSet;
} else {
params = new HashMap(map);
if (parameters.length != params.size()) {
// 参数不一致
returnDataSet.setErrorCode(ErrorCodeBase.SYSTEM_ERROR_TYPE_LENGTH);
return returnDataSet;
}
}
}
}
if ( ConfigConsts.REST.equals(module)) {
if (token == null) {
token = ConfigConstsBase.NOT_TOKEN;
}
request.setAttribute(ConfigConstsBase.SERVICE, invokeName);
request.setAttribute(ConfigConstsBase.REQ_TOKEN_KEY, (String) token);
request.setAttribute(ConfigConstsBase.REST, ConfigConstsBase.REST);
request.setAttribute(ConfigConstsBase.TOKEN_CHECK, false);
}
request.setAttribute(ConfigConstsBase.IP_ADDRESS, ipAddress);
request.setAttribute(ConfigConstsBase.MODULE, module);
Class serviceClass = this.serviceClassMap.get(geneticClass);
Object beanService = applicationContext.getBean(serviceClass);
if (beanService == null) {
returnDataSet.setErrorCode(ErrorCodeBase.SYSTEM_ERROR_PATH);
return returnDataSet;
}
MethodAccess methodAccess = MethodAccess.get(serviceClass);
int methodIndex = methodAccess.getIndex(methodName);
if (httpType) {
if (parameters.length > 0 && !params.isEmpty()) {
returnDataSet.setDataSet(methodAccess.invoke(beanService, methodIndex, SortUtil.sortMapByKey(params, parameters)));
} else {
returnDataSet.setDataSet(methodAccess.invoke(beanService, methodIndex));
}
} else {
Object paramsPost = this.sortMapToPojo(param.toString(), parameters, invokeName, encryted, module);
//http post请求
if (paramsPost instanceof Object[]) {
returnDataSet.setDataSet(methodAccess.invoke(beanService, methodIndex, (Object[]) paramsPost));
} else {
returnDataSet.setDataSet(methodAccess.invoke(beanService, methodIndex, paramsPost));
}
}
returnDataSet.setErrorCode(ErrorCodeBase.SUCCESS);
if (returnDataSet.getDataSet() == null) {
returnDataSet.setDataSet("");
}
return returnDataSet;
}
}