package org.hsweb.web.core.logger;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.hsweb.web.bean.po.logger.LoggerInfo;
import org.hsweb.web.core.logger.annotation.AccessLogger;
import org.hsweb.web.core.utils.WebUtil;
import org.hsweb.commons.ClassUtils;
import org.hsweb.commons.MD5;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * Created by zhouhao on 16-4-28.
 */
public class AopAccessLoggerResolver {

    public LoggerInfo resolver(ProceedingJoinPoint pjp) {
        LoggerInfo logInfo = new LoggerInfo();
        HttpServletRequest request = WebUtil.getHttpServletRequest();
        Class<?> target = pjp.getTarget().getClass();
        StringBuilder describe = new StringBuilder();
        Method method = ((MethodSignature) pjp.getSignature()).getMethod();
        String methodName = getMethodName(pjp);

        AccessLogger classAnnotation = ClassUtils.getAnnotation(target, AccessLogger.class);
        AccessLogger methodAnnotation = ClassUtils.getAnnotation(method, AccessLogger.class);
        if (classAnnotation != null) {
            describe.append(classAnnotation.value());
        }
        if (methodAnnotation != null) {
            if (classAnnotation != null)
                describe.append("-");
            describe.append(methodAnnotation.value());
        }
        logInfo.setId(MD5.encode(String.valueOf(System.nanoTime())));
        logInfo.setModuleDesc(describe.toString());//方法描述
        logInfo.setClassName(target.getName());//当前访问映射到的类名
        logInfo.setClientIp(WebUtil.getIpAddr(request));//ip地址
        logInfo.setRequestMethod(request.getMethod().concat(".").concat(methodName));//方法：GET.select()
        logInfo.setRequestHeader(JSON.toJSONString(WebUtil.getHeaders(request)));//http请求头
        logInfo.setReferer(request.getHeader("referer"));//referer
        logInfo.setRequestUri(request.getRequestURI());//请求相对路径
        logInfo.setRequestUrl(WebUtil.getBasePath(request).concat(logInfo.getRequestUri().substring(1)));//请求绝对路径
        logInfo.setUserAgent(request.getHeader("UserAgent"));//客户端标识
        logInfo.setRequestParam(JSON.toJSONString(WebUtil.getParams(request)));//请求参数
        return logInfo;
    }

    protected String getMethodName(ProceedingJoinPoint pjp) {
        StringBuilder methodName = new StringBuilder(pjp.getSignature().getName()).append("(");
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        String[] names = signature.getParameterNames();
        Class[] args = signature.getParameterTypes();
        for (int i = 0, len = args.length; i < len; i++) {
            if (i != 0) methodName.append(",");
            methodName.append(args[i].getSimpleName()).append(" ").append(names[i]);
        }
        return methodName.append(")").toString();
    }

}

@@ -157,16 +157,56 @@
 nature;%0A
+import org.hsweb.commons.DateTimeUtils;%0A
 import o
@@ -410,16 +410,72 @@
 ons.MD5;
+%0Aimport org.springframework.web.multipart.MultipartFile;
 %0A%0Aimport
@@ -525,36 +525,199 @@
 ort java
-.lang.reflect.Method
+x.servlet.http.HttpServletResponse;%0Aimport javax.servlet.http.HttpSession;%0Aimport java.lang.reflect.Method;%0Aimport java.util.Date;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map
 ;%0A%0A/**%0A 
@@ -1090,23 +1090,41 @@
   Method
- method
+Signature methodSignature
  = ((Met
@@ -1156,16 +1156,57 @@
 ature())
+;%0A        Method method = methodSignature
 .getMeth
@@ -1726,32 +1726,1195 @@
 e());%0A        %7D%0A
+        Map%3CString, Object%3E param = new LinkedHashMap%3C%3E();%0A        String%5B%5D paramNames = methodSignature.getParameterNames();%0A        Object%5B%5D args = pjp.getArgs();%0A        for (int i = 0; i %3C paramNames.length; i++) %7B%0A            Object arg = args%5Bi%5D;%0A            String argString;%0A            if (arg instanceof HttpServletRequest%0A                    %7C%7C arg instanceof HttpSession%0A                    %7C%7C arg instanceof HttpServletResponse%0A                    %7C%7C arg instanceof MultipartFile%0A                    %7C%7C arg instanceof MultipartFile%5B%5D) continue;%0A            if (arg instanceof String) argString = (String) arg;%0A            else if (arg instanceof Number) argString = String.valueOf(arg);%0A            else if (arg instanceof Date) argString = DateTimeUtils.format(((Date) arg), DateTimeUtils.YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);%0A            else %7B%0A                try %7B%0A                    argString = JSON.toJSONString(arg);%0A                %7D catch (Exception e) %7B%0A                    continue;%0A                %7D%0A            %7D%0A            param.put(paramNames%5Bi%5D, argString);%0A        %7D%0A        Map%3CString, String%3E header = WebUtil.getHeaders(request);%0A
         logInfo.
@@ -3306,35 +3306,14 @@
 ing(
-WebUtil.getHeaders(request)
+header
 ));/
@@ -3352,28 +3352,21 @@
 rer(
-request.getHeader(%22r
+header.get(%22R
 efer
@@ -3592,31 +3592,25 @@
 ent(
-request.getHeader
+header.get
 (%22User
+-
 Agen
@@ -3676,34 +3676,13 @@
 ing(
-WebUtil.getParams(request)
+param
 ));/

修改参数逻辑,不再记录无关的参数