@Aspect
@Component
public class ExpAspect {
private static Logger logger = LoggerFactory.getLogger(ExpAspect.class);
private static final String LINE_SEPARATOR = System.lineSeparator();
@Autowired
private ExpLogAPI expLogAPI;
private ExpLogVO expLogVO = null;
@Pointcut("@annotation(com.sgai.tpl.webapps.op.controller.exportsAspect.ExpLog)")
public void ExpLog(){};
/**
* 在切点之前织入
* @param joinPoint
* @throws Throwable
*/
@Before("ExpLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String methodDescription = getAspectLogDescription(joinPoint);
String acctId = AssertContext.getAcctId();
expLogVO = new ExpLogVO();
expLogVO.setUrl(request.getRequestURL().toString());
expLogVO.setDescription(methodDescription);
expLogVO.setMethod(joinPoint.getSignature().getDeclaringTypeName()+","+joinPoint.getSignature().getName());
expLogVO.setIp(request.getRemoteAddr());
ObjectMapper objectMapper = new ObjectMapper();
Object obj = objectMapper.convertValue(joinPoint.getArgs()[0], Object.class);
expLogVO.setRequest(obj.toString());
int a = expLogVO.getRequest().indexOf("null");
while(a!=-1){
String b = expLogVO.getRequest().substring(0,a+4);
String str = jq(b);
expLogVO.setRequest(expLogVO.getRequest().replace(str,""));
a = expLogVO.getRequest().indexOf("null");
};
expLogVO.setCreatedBy(acctId);
expLogVO.setCreatedDt(new Date());
expLogVO.setState(1);
expLogVO.setId(expLogAPI.insert(expLogVO).getRetContent());
}
private static String jq(String str) {
int max = str.length();
int a = 0;
int dh = str.lastIndexOf(",");
int zk = str.lastIndexOf("[");
int dk = str.lastIndexOf("{");
if(dh>zk){
if(dh>dk){
a = dh;
}else{
a = dk+1;
}
}else if(zk>dk){
a = zk+1;
}else{
a = dk+1;
}
str = str.substring(a,max);
return str;
}
/**
* 在切点之后织入
* @throws Throwable
*/
@After("ExpLog()")
public void doAfter() throws Throwable {
}
/**
* 环绕
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around("ExpLog()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
ObjectMapper objectMapper = new ObjectMapper();
JsonResponse<Object> jsonResponse = objectMapper.convertValue(result, JsonResponse.class);
ExpLogVO expLogVO1 = new ExpLogVO();
expLogVO1.setId(expLogVO.getId());
expLogVO1.setSize(jsonResponse.getRspBody().toString());
expLogVO1.setTime(String.valueOf(System.currentTimeMillis() - startTime));
expLogVO1.setUpdateDt(new Date());
expLogVO1.setState(2);
expLogAPI.update(expLogVO1);
return result;
}
/**
* 获取切面注解的描述
*
* @param joinPoint 切点
* @return 描述信息
* @throws Exception
*/
public String getAspectLogDescription(JoinPoint joinPoint)
throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
StringBuilder description = new StringBuilder("");
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
description.append(method.getAnnotation(ExpLog.class).description());
break;
}
}
}
return description.toString();
}
}