ControllerUtil.java
10.3 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package com.brframework.commoncms.utils;
import com.brframework.commoncms.annatotion.Panel;
import com.brframework.commoncms.annatotion.option.CmsOption;
import com.brframework.commoncms.core.Option;
import com.brframework.commoncms.core.UriProtocol;
import com.brframework.commoncms.core.option.DefOption;
import com.brframework.commoncms.core.uri.PanelUri;
import com.brframework.commoncms.core.uri.RequestUri;
import com.brframework.commonsecurity.core.SecurityContextHolder;
import com.brframework.commonweb.exception.HandleException;
import com.brframework.commonweb.json.PageParam;
import com.brframework.commonweb.utils.ServletUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.brframework.commoncms.annatotion.LayoutColumn;
import com.brframework.commoncms.core.layout.PanelLayoutColumn;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerMapping;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Map;
/**
* @author xu
* @date 2018/10/15 15:26
*/
public class ControllerUtil {
private final static LocalVariableTableParameterNameDiscoverer LVTPND = new LocalVariableTableParameterNameDiscoverer();
/**
* 获取一个类所有的字段
* @param c
* @return
*/
public static List<Field> getAllField(Class<?> c){
List<Field> fields = Lists.newArrayList(c.getDeclaredFields());
if(Object.class.getName().equals(c.getSuperclass().getName())){
return fields;
}
fields.addAll(getAllField(c.getSuperclass()));
return fields;
}
/**
* 解析方法uri协议
* @param method 解析的method
* @param alert 如果是进行某个请求操作时的提示语
* @return
*/
public static UriProtocol parseMethodUriProtocol(Method method, String alert){
//获取Panel注解类型,用于判断方法的跳转意图
Panel panel = AnnotatedElementUtils.findMergedAnnotation(method, Panel.class);
String protocol;
String path;
String requestMethod = ControllerUtil.getHttpMethod(method);
String url = ControllerUtil.getControllerUrl(method);
if (panel == null) {
//如果不存在Panel相关注解,说明这是一个请求方法
protocol = UriProtocol.REQUEST;
RequestUri requestUri = new RequestUri();
requestUri.setAlert(alert);
requestUri.setBody(null);
requestUri.setMethod(requestMethod);
requestUri.setUrl(url);
path = requestUri.toString();
} else {
//如果存在Panel注解,则说明这是一个Panel模块
protocol = UriProtocol.PANEL;
PanelUri panelUri = new PanelUri();
panelUri.setMethod(requestMethod);
panelUri.setUrl(url);
path = panelUri.toString();
}
return UriProtocol.builder()
.protocol(protocol)
.path(path)
.build();
}
/**
* 生成列
*
* @param field
* @return
*/
public static PanelLayoutColumn createColumn(Field field) throws Throwable {
LayoutColumn cmsColumn = AnnotatedElementUtils.findMergedAnnotation(field, LayoutColumn.class);
PanelLayoutColumn column;
if (cmsColumn == null) {
//默认列
column = LayoutColumnUtil.defaultColumn(field);
} else {
column = cmsColumn.type().newInstance();
}
column.create(field);
return column;
}
/**
* 获取方法的参数,解决参数名为arg0, arg1的问题
* @param method
* @return
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static Parameter[] getHandleParameter(Method method) throws NoSuchFieldException, IllegalAccessException {
String[] parameterNames = LVTPND.getParameterNames(method);
Parameter[] parameters = method.getParameters();
Field nameField = Parameter.class.getDeclaredField("name");
nameField.setAccessible(true);
for (int i = 0; i < parameterNames.length; i++) {
nameField.set(parameters[i], parameterNames[i]);
}
return parameters;
}
/**
* 获取请求里的URI参数
* @param method
* @return
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public static Map<String, String> getUriParameter(Method method) throws NoSuchFieldException, IllegalAccessException {
Map<String, Object> uriParams = (Map<String, Object>) ServletUtils.request().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
Parameter[] parameters = ControllerUtil.getHandleParameter(method);
Map<String, String> uriParameter = Maps.newHashMap();
for (Parameter parameter : parameters) {
PathVariable pathVariable = parameter.getAnnotation(PathVariable.class);
if(pathVariable != null){
String name = pathVariable.value();
if(Strings.isNullOrEmpty(name)){
name = parameter.getName();
}
uriParameter.put(name, uriParams.get(name).toString());
}
}
return uriParameter;
}
/**
* 将包含有ApiModelProperty注解的字段筛选出来
*
* @param oldFields
* @return
*/
public static List<Field> handlerPropertyField(List<Field> oldFields) {
List<Field> newFields = Lists.newArrayList();
for (Field oldField : oldFields) {
ApiModelProperty apiModelProperty =
AnnotationUtils.findAnnotation(oldField, ApiModelProperty.class);
if (apiModelProperty != null) {
newFields.add(oldField);
}
}
return newFields;
}
/**
* 获取方法参数里有ApiModel注解的类
* 自动忽略PageParam
*
* @param method
* @return
*/
public static List<Class> getParamApiModelClass(Method method) {
Class[] parameterTypes = method.getParameterTypes();
List<Class> apiTypes = Lists.newArrayList();
for (Class<?> parameterType : parameterTypes) {
if (parameterType.getDeclaredAnnotation(ApiModel.class) != null) {
if (!parameterType.getName().equals(PageParam.class.getName())) {
apiTypes.add(parameterType);
}
}
}
return apiTypes;
}
/**
* 获取Controller完整的url
*
* @param method
* @return
*/
public static String getControllerUrl(Method method) {
//获取前缀
String prefix = getPrefixUrl(method);
//获取请求地址
String url = getMappingUrl(method);
if (!url.startsWith("/") && !prefix.endsWith("/")) {
url = "/" + url;
}
if (url.startsWith("/") && prefix.endsWith("/")) {
url = url.substring(1);
}
return prefix + url;
}
/**
* 获取方法的前缀
*
* @param method
* @return
*/
public static String getPrefixUrl(Method method) {
RequestMapping requestMapping = AnnotationUtils.findAnnotation(method.getDeclaringClass(), RequestMapping.class);
String prefix = "";
if (requestMapping != null) {
prefix = requestMapping.value()[0];
}
return prefix;
}
/**
* 获取标题
*
* @param method
* @return
*/
public static String getTitle(Method method) {
ApiOperation ao = AnnotatedElementUtils.findMergedAnnotation(method, ApiOperation.class);
if (ao == null) {
throw new HandleException("方法" + method.toString() + "不存在ApiOperation注解");
}
return ao.value();
}
/**
* 获取方法映射的url
*
* @param method
* @return
*/
public static String getMappingUrl(Method method) {
RequestMapping rm = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
return rm.value()[0];
}
/**
* 获取HTTP METHOD
*
* @param method
* @return
*/
public static String getHttpMethod(Method method) {
RequestMapping rm = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
if (rm.method().length == 0) {
throw new HandleException("当前方法中不存在RequestMapping注解");
}
return rm.method()[0].toString();
}
/**
* 方法上的Option是否有权限操作
* @return
*/
public static boolean hasMethodOptionRole(Method method, CmsOption option){
String role = ControllerUtil.getOptionRoleByMethod(method, option);
if(StringUtils.isEmpty(role) || SecurityContextHolder.hasRole("ROLE_" + role)){
return true;
}
return false;
}
/**
* 获取Option uriMappingMethod映射方法需要的权限
* @param method
* @param option
* @return
*/
public static String getOptionRoleByMethod(Method method, CmsOption option){
if(StringUtils.isEmpty(option.uriMappingMethod())){
return "";
}
Method[] declaredMethods = method.getDeclaringClass().getMethods();
for (Method declaredMethod : declaredMethods) {
if (declaredMethod.getName().equals(option.uriMappingMethod())) {
PreAuthorize authAnn = AnnotationUtils.findAnnotation(declaredMethod, PreAuthorize.class);
if(authAnn == null){
break;
}
return MenuPermissionUtils.getRoleName(authAnn.value());
}
}
return "";
}
}