use of com.mendmix.common.annotation.ApiMetadata in project jeesuite-libs by vakinge.
the class AppMetadataHolder method scanApiInfos.
private static synchronized void scanApiInfos(AppMetadata metadata, List<String> classNameList) {
if (!metadata.getApis().isEmpty())
return;
String contextPath = GlobalRuntimeContext.getContextPath();
String pathPrefix;
if (ResourceUtils.containsAnyProperty("mendmix.request.pathPrefix")) {
pathPrefix = ResourceUtils.getProperty("mendmix.request.pathPrefix");
if (!pathPrefix.startsWith("/")) {
pathPrefix = "/" + pathPrefix;
}
pathPrefix = contextPath + pathPrefix;
} else {
pathPrefix = contextPath;
}
Map<String, String> packagePathPrefixs = ResourceUtils.getMappingValues("mendmix.request.pathPrefix.mapping");
Method[] methods;
String baseUri;
ApiInfo apiInfo;
ApiMetadata classMetadata;
ApiMetadata methodMetadata;
for (String className : classNameList) {
try {
Class<?> clazz = Class.forName(className);
if (!clazz.isAnnotationPresent(Controller.class) && !clazz.isAnnotationPresent(RestController.class))
continue;
//
if (packagePathPrefixs.containsKey(clazz.getPackage().getName())) {
baseUri = addFirstPathSeparator(packagePathPrefixs.get(clazz.getPackage().getName()));
} else {
baseUri = "";
}
RequestMapping requestMapping = AnnotationUtils.findAnnotation(clazz, RequestMapping.class);
if (requestMapping != null) {
baseUri = baseUri + addFirstPathSeparator(requestMapping.value().length > 0 ? requestMapping.value()[0] : requestMapping.path()[0]);
if (baseUri.endsWith("/")) {
baseUri = baseUri.substring(0, baseUri.length() - 1);
}
}
//
classMetadata = clazz.getAnnotation(ApiMetadata.class);
methods = clazz.getDeclaredMethods();
Map<String, Method> interfaceMethods = getInterfaceMethods(clazz);
methodLoop: for (Method method : methods) {
methodMetadata = method.isAnnotationPresent(ApiMetadata.class) ? method.getAnnotation(ApiMetadata.class) : classMetadata;
//
if (methodMetadata != null && methodMetadata.IntranetAccessOnly()) {
continue methodLoop;
}
String apiUri = null;
String apiHttpMethod = null;
requestMapping = getAnnotation(method, interfaceMethods.get(method.getName()), RequestMapping.class);
if (requestMapping != null) {
apiUri = requestMapping.value()[0];
if (requestMapping.method() != null && requestMapping.method().length > 0) {
apiHttpMethod = requestMapping.method()[0].name();
}
} else {
PostMapping postMapping = getAnnotation(method, interfaceMethods.get(method.getName()), PostMapping.class);
if (postMapping != null) {
apiUri = postMapping.value().length > 0 ? postMapping.value()[0] : postMapping.path()[0];
apiHttpMethod = RequestMethod.POST.name();
}
GetMapping getMapping = getAnnotation(method, interfaceMethods.get(method.getName()), GetMapping.class);
if (getMapping != null) {
apiUri = getMapping.value().length > 0 ? getMapping.value()[0] : getMapping.path()[0];
apiHttpMethod = RequestMethod.GET.name();
}
}
if (StringUtils.isBlank(apiUri)) {
continue methodLoop;
}
apiInfo = new ApiInfo();
if (apiUri == null) {
apiUri = baseUri;
} else {
apiUri = baseUri + addFirstPathSeparator(apiUri);
}
apiInfo.setUrl(pathPrefix + apiUri);
apiInfo.setMethod(apiHttpMethod);
if (methodMetadata == null) {
apiInfo.setPermissionType(PermissionLevel.LoginRequired);
} else {
apiInfo.setPermissionType(methodMetadata.permissionLevel());
apiInfo.setName(methodMetadata.actionName());
apiInfo.setActionLog(methodMetadata.actionLog());
apiInfo.setRequestLog(methodMetadata.requestLog());
apiInfo.setResponseLog(methodMetadata.responseLog());
apiInfo.setOpenApi(methodMetadata.openApi());
}
if (StringUtils.isBlank(apiInfo.getName())) {
String name = apiInfo.getUrl().replace("/", "_");
if (name.startsWith("_"))
name = name.substring(1);
apiInfo.setName(name);
}
// 分页查询
if (method.getReturnType() == Page.class) {
apiInfo.setResponseLog(false);
}
// 上传
Class<?>[] parameterTypes = method.getParameterTypes();
for (Class<?> type : parameterTypes) {
if (type == MultipartFile.class || type == MultipartFile[].class) {
apiInfo.setRequestLog(false);
break;
}
}
metadata.addApi(apiInfo);
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("error className:" + className);
}
}
}
use of com.mendmix.common.annotation.ApiMetadata in project jeesuite-libs by vakinge.
the class GlobalDefaultInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
ThreadLocalContext.unset();
Enumeration<String> headerNames = request.getHeaderNames();
String headerName;
while (headerNames.hasMoreElements()) {
headerName = headerNames.nextElement();
CurrentRuntimeContext.addContextHeader(headerName, request.getHeader(headerName));
}
//
if (AppConfigs.invokeTokenCheckEnabled) {
String uri = request.getRequestURI();
if (!invoketokenCheckIgnoreUriMather.match(uri)) {
String authCode = request.getHeader(CustomRequestHeaders.HEADER_INVOKE_TOKEN);
TokenGenerator.validate(authCode, true);
}
}
if (handler instanceof HandlerMethod) {
HandlerMethod method = (HandlerMethod) handler;
ApiMetadata config = method.getMethod().getAnnotation(ApiMetadata.class);
if (config != null) {
if (config.IntranetAccessOnly() && !WebUtils.isInternalRequest(request)) {
response.setStatus(403);
if (log.isDebugEnabled()) {
WebUtils.printRequest(request);
}
throw new ForbiddenAccessException();
}
// @ResponseBody and ResponseEntity的接口在postHandle addHeader不生效,因为会经过HttpMessageConverter
if (config.responseKeep()) {
response.addHeader(CustomRequestHeaders.HEADER_RESP_KEEP, Boolean.TRUE.toString());
}
}
}
return true;
}
use of com.mendmix.common.annotation.ApiMetadata in project jeesuite-libs by vakinge.
the class RequestLoggingInterceptor method around.
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
ActionLog actionLog = ActionLogCollector.currentActionLog();
if (actionLog == null) {
return pjp.proceed();
}
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
boolean requestLog = true;
boolean responseLog = !ignoreResponseBody;
if (method.isAnnotationPresent(ApiMetadata.class)) {
ApiMetadata metadata = method.getAnnotation(ApiMetadata.class);
requestLog = metadata.requestLog();
responseLog = metadata.responseLog();
if (!requestLog && !responseLog) {
return pjp.proceed();
}
}
Map<String, Object> parameters = null;
Object body = null;
if (requestLog) {
Object[] args = pjp.getArgs();
if (args.length > 0) {
List<ParameterLogConfig> parameterConfigs = getParameterConfigs(pjp.getTarget().getClass().getName(), method);
parameters = new HashMap<>(3);
ParameterLogConfig config;
for (int i = 0; i < args.length; i++) {
if ((config = parameterConfigs.get(i)).ignore)
continue;
if (config.isBody) {
body = args[i];
} else if (config.paramName != null && args[i] != null) {
parameters.put(config.paramName, args[i].toString());
}
}
}
if (log.isDebugEnabled()) {
String requestLogMessage = RequestLogBuilder.requestLogMessage(request.getRequestURI(), request.getMethod(), parameters, body);
log.debug(requestLogMessage);
}
}
actionLog.setQueryParameters(ParameterUtils.mapToQueryParams(parameters));
actionLog.setRequestData(body);
Object result = null;
try {
result = pjp.proceed();
//
if (responseLog) {
actionLog.setResponseData(result);
}
return result;
} catch (Exception e) {
if (e instanceof JeesuiteBaseException) {
actionLog.setExceptions(e.getMessage());
} else {
actionLog.setExceptions(ExceptionUtils.getMessage(e));
}
throw e;
}
}
Aggregations