2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 # Copyright (c) 2020, CMCC Technologies Co., Ltd.
7 # Licensed under the Apache License, Version 2.0 (the "License")
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.so.adapters.nssmf.interceptor;
23 import lombok.ToString;
24 import org.aspectj.lang.JoinPoint;
25 import org.aspectj.lang.ProceedingJoinPoint;
26 import org.aspectj.lang.annotation.*;
27 import org.aspectj.lang.reflect.MethodSignature;
28 import org.onap.so.adapters.nssmf.annotation.RequestLogger;
29 import org.onap.so.adapters.nssmf.annotation.ServiceLogger;
30 import org.onap.so.adapters.nssmf.exceptions.ApplicationException;
31 import org.onap.so.adapters.nssmf.util.NssmfAdapterUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.core.annotation.Order;
35 import org.springframework.stereotype.Component;
36 import org.springframework.web.context.request.RequestContextHolder;
37 import org.springframework.web.context.request.ServletRequestAttributes;
38 import org.springframework.web.multipart.MultipartFile;
39 import javax.servlet.http.HttpServletRequest;
40 import java.lang.reflect.Method;
41 import java.util.HashMap;
45 * support to print logger of service method
50 public class RequestLogAspect {
52 private static final Logger logger = LoggerFactory.getLogger(RequestLogAspect.class);
54 @Pointcut("execution(* org.onap.so.adapters.nssmf.service..*(..))")
55 public void serviceLogger() {
59 @Around("serviceLogger()")
60 public Object around(ProceedingJoinPoint joinPoint) {
62 MethodSignature signature = (MethodSignature) joinPoint.getSignature();
63 Method method = signature.getMethod();
65 Class<?> targetClass = method.getDeclaringClass();
67 StringBuilder classAndMethod = new StringBuilder();
68 ServiceLogger classAnnotation = targetClass.getAnnotation(ServiceLogger.class);
69 ServiceLogger methodAnnotation = method.getAnnotation(ServiceLogger.class);
71 if (classAnnotation == null && methodAnnotation == null) {
72 return joinPoint.proceed();
75 if (classAnnotation != null) {
76 if (classAnnotation.ignore()) {
77 return joinPoint.proceed();
79 classAndMethod.append(classAnnotation.value()).append("-");
82 String target = targetClass.getName() + "#" + method.getName();
84 String params = NssmfAdapterUtil.marshal(joinPoint.getArgs());
86 logger.info("{} Start: Method = {} \nParams = {}", classAndMethod.toString(), target, params);
88 long start = System.currentTimeMillis();
89 Object result = joinPoint.proceed();
90 long timeConsuming = System.currentTimeMillis() - start;
92 logger.info("\n{} End: Method = {}, Spend time = {}ms \nResult = {}", classAndMethod.toString(), target,
93 timeConsuming, NssmfAdapterUtil.marshal(result));
96 } catch (Throwable e) {
97 logger.error(e.getMessage(), e);
102 @Pointcut("execution(* org.onap.so.adapters.nssmf.controller..*(..))")
103 public void controllerLogger() {
107 @Around("controllerLogger()")
108 public Object doAroundRequest(ProceedingJoinPoint point) throws Throwable {
109 MethodSignature signature = (MethodSignature) point.getSignature();
110 Method method = signature.getMethod();
111 Class<?> targetClass = method.getDeclaringClass();
113 RequestLogger classAnnotation = targetClass.getAnnotation(RequestLogger.class);
114 RequestLogger methodAnnotation = method.getAnnotation(RequestLogger.class);
116 if ((classAnnotation == null && methodAnnotation == null)
117 || (classAnnotation != null && classAnnotation.ignore())
118 || (methodAnnotation != null && methodAnnotation.ignore())) {
119 return point.proceed();
122 long start = System.currentTimeMillis();
123 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
125 assert attributes != null;
126 HttpServletRequest request = attributes.getRequest();
127 Object result = point.proceed();
128 RequestInfo requestInfo = new RequestInfo();
129 requestInfo.setIp(request.getRemoteAddr());
130 requestInfo.setUrl(request.getRequestURL().toString());
131 requestInfo.setHttpMethod(request.getMethod());
132 requestInfo.setClassMethod(String.format("%s.%s", signature.getDeclaringTypeName(), signature.getName()));
133 requestInfo.setRequestParams(getRequestParamsByProceedingJoinPoint(point));
134 requestInfo.setResult(result);
135 requestInfo.setTimeCost(System.currentTimeMillis() - start);
136 logger.info("Request Info : {}", NssmfAdapterUtil.marshal(requestInfo));
140 @AfterThrowing(pointcut = "controllerLogger()", throwing = "e")
141 public void doAfterRequestThrow(JoinPoint joinPoint, RuntimeException e) {
142 MethodSignature signature = (MethodSignature) joinPoint.getSignature();
143 Method method = signature.getMethod();
144 Class<?> targetClass = method.getDeclaringClass();
146 RequestLogger classAnnotation = targetClass.getAnnotation(RequestLogger.class);
147 RequestLogger methodAnnotation = method.getAnnotation(RequestLogger.class);
149 if ((classAnnotation == null && methodAnnotation == null)
150 || (classAnnotation != null && classAnnotation.ignore())
151 || (methodAnnotation != null && methodAnnotation.ignore())) {
154 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
155 assert attributes != null;
156 HttpServletRequest request = attributes.getRequest();
157 RequestErrorInfo requestErrorInfo = new RequestErrorInfo();
158 requestErrorInfo.setIp(request.getRemoteAddr());
159 requestErrorInfo.setUrl(request.getRequestURL().toString());
160 requestErrorInfo.setHttpMethod(request.getMethod());
161 requestErrorInfo.setClassMethod(String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(),
162 joinPoint.getSignature().getName()));
163 requestErrorInfo.setRequestParams(getRequestParamsByJoinPoint(joinPoint));
164 requestErrorInfo.setException(e);
167 res = NssmfAdapterUtil.marshal(requestErrorInfo);
168 logger.info("Error Request Info : {}", res);
169 } catch (ApplicationException ex) {
170 logger.info("Error Request Info : {}", requestErrorInfo);
174 private Map<String, Object> getRequestParamsByJoinPoint(JoinPoint joinPoint) {
175 String[] paramNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
176 Object[] paramValues = joinPoint.getArgs();
178 return buildRequestParam(paramNames, paramValues);
182 private Map<String, Object> getRequestParamsByProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) {
183 String[] paramNames = ((MethodSignature) proceedingJoinPoint.getSignature()).getParameterNames();
184 Object[] paramValues = proceedingJoinPoint.getArgs();
186 return buildRequestParam(paramNames, paramValues);
189 private Map<String, Object> buildRequestParam(String[] paramNames, Object[] paramValues) {
190 Map<String, Object> requestParams = new HashMap<>();
191 for (int i = 0; i < paramNames.length; i++) {
192 Object value = paramValues[i];
194 if (value instanceof MultipartFile) {
195 MultipartFile file = (MultipartFile) value;
196 value = file.getOriginalFilename();
199 requestParams.put(paramNames[i], value);
202 return requestParams;
207 private class RequestInfo {
210 private String httpMethod;
211 private String classMethod;
212 private Object requestParams;
213 private Object result;
214 private Long timeCost;
219 private class RequestErrorInfo {
222 private String httpMethod;
223 private String classMethod;
224 private Object requestParams;
225 private RuntimeException exception;