1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.cloud.helper.CookiesHelper;
import com.yanzuoguang.util.helper.ArrayHelper;
import com.yanzuoguang.util.helper.StringHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.core.Message;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/**
* 配置文件处理
*
* @author 颜佐光
*/
@Component
public class AspectLogUrl {
/**
* /**
* 获取方法名称
*
* @return 方法路径
*/
public String getWebMethodUrl(ProceedingJoinPoint joinPoint) {
String url = StringHelper.EMPTY;
Signature signature = joinPoint.getSignature();
Class<?> declaringType = signature.getDeclaringType();
String name = signature.getName();
FeignClient feignClient = declaringType.getAnnotation(FeignClient.class);
// 类路径
RequestMapping[] classRequests = declaringType.getAnnotationsByType(RequestMapping.class);
PostMapping[] classPosts = declaringType.getAnnotationsByType(PostMapping.class);
GetMapping[] classGets = declaringType.getAnnotationsByType(GetMapping.class);
List<Annotation> annotation = new ArrayList<>();
ArrayHelper.addList(annotation, classRequests, classPosts, classGets);
String baseUrl = String.format("%s:%s", declaringType.getSimpleName(), name);
if (!annotation.isEmpty() && signature instanceof MethodSignature) {
MethodSignature methodSignature = (MethodSignature) signature;
Method targetMethod = methodSignature.getMethod();
// 方法路径
RequestMapping[] requests = targetMethod.getAnnotationsByType(RequestMapping.class);
PostMapping[] posts = targetMethod.getAnnotationsByType(PostMapping.class);
GetMapping[] gets = targetMethod.getAnnotationsByType(GetMapping.class);
url = getFeignOrRequestUrl(feignClient, classRequests, classPosts, classGets, requests, posts, gets);
}
if (StringHelper.isEmpty(url)) {
return baseUrl;
}
return String.format("%s:%s", baseUrl, url);
}
/**
* 获取feign请求地址
*
* @param feignClient
* @param classRequests
* @param classPosts
* @param classGets
* @param requests
* @param posts
* @param gets
* @return
*/
private String getFeignOrRequestUrl(FeignClient feignClient, RequestMapping[] classRequests, PostMapping[] classPosts, GetMapping[] classGets, RequestMapping[] requests, PostMapping[] posts, GetMapping[] gets) {
StringBuilder sb = new StringBuilder();
if (feignClient != null) {
sb.append(feignClient.value());
sb.append(":");
List<RequestMapping> reqList = ArrayHelper.mergeList(classRequests, requests);
List<PostMapping> postList = ArrayHelper.mergeList(classPosts, posts);
List<GetMapping> getList = ArrayHelper.mergeList(classGets, gets);
for (RequestMapping item : reqList) {
if (item.value().length > 0) {
sb.append(item.value()[0]);
}
}
for (PostMapping item : postList) {
if (item.value().length > 0) {
sb.append(item.value()[0]);
}
}
for (GetMapping item : getList) {
if (item.value().length > 0) {
sb.append(item.value()[0]);
}
}
} else {
HttpServletRequest request = CookiesHelper.getRequest();
sb.append(request.getRequestURI());
if (!StringHelper.isEmpty(request.getQueryString())) {
sb.append("?");
sb.append(request.getQueryString());
}
}
return sb.toString();
}
/**
* 根据类型获取对象
*
* @param joinPoint 参数
* @return 消息实体
*/
public Message getMessage(ProceedingJoinPoint joinPoint) {
for (Object item : joinPoint.getArgs()) {
if (item instanceof Message) {
return (Message) item;
}
}
return null;
}
/**
* 获取消息路径
*
* @param joinPoint 函数
* @param message 消息内容
* @return 消息路径
*/
public String getMessageUrl(ProceedingJoinPoint joinPoint, Message message) {
Signature signature = joinPoint.getSignature();
Class<?> declaringType = signature.getDeclaringType();
String name = signature.getName();
String baseUrl = String.format("%s:%s", declaringType.getSimpleName(), name);
String url;
if (message != null) {
url = message.getMessageProperties().getConsumerQueue();
} else {
url = StringHelper.EMPTY;
}
if (StringHelper.isEmpty(url)) {
return baseUrl;
}
return String.format("%s:%s", baseUrl, url);
}
}