|
| 1 | +package com.featureprobe.api.aop; |
| 2 | + |
| 3 | +import com.featureprobe.api.auth.TokenHelper; |
| 4 | +import com.featureprobe.api.base.hook.Action; |
| 5 | +import com.featureprobe.api.base.hook.IHookQueue; |
| 6 | +import com.featureprobe.api.base.hook.Resource; |
| 7 | +import com.featureprobe.api.base.hook.Hook; |
| 8 | +import com.featureprobe.api.base.model.HookContext; |
| 9 | +import com.featureprobe.api.base.tenant.TenantContext; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.apache.commons.lang3.StringUtils; |
| 12 | +import org.aspectj.lang.JoinPoint; |
| 13 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 14 | +import org.aspectj.lang.annotation.Around; |
| 15 | +import org.aspectj.lang.annotation.Aspect; |
| 16 | +import org.aspectj.lang.annotation.Pointcut; |
| 17 | +import org.aspectj.lang.reflect.MethodSignature; |
| 18 | +import org.springframework.core.LocalVariableTableParameterNameDiscoverer; |
| 19 | +import org.springframework.stereotype.Component; |
| 20 | +import org.springframework.web.bind.annotation.PathVariable; |
| 21 | +import org.springframework.web.bind.annotation.RequestBody; |
| 22 | +import java.lang.annotation.Annotation; |
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.util.Objects; |
| 25 | + |
| 26 | +@Slf4j |
| 27 | +@Aspect |
| 28 | +@Component |
| 29 | +public class HookAspect { |
| 30 | + |
| 31 | + @javax.annotation.Resource |
| 32 | + IHookQueue hookQueue; |
| 33 | + |
| 34 | + @Pointcut("@annotation(com.featureprobe.api.base.hook.Hook)") |
| 35 | + public void hook (){} |
| 36 | + |
| 37 | + @Around("hook()") |
| 38 | + public Object around(ProceedingJoinPoint jp) throws Throwable { |
| 39 | + Hook webHookAnnotation = getMethodAnnotation(jp, Hook.class); |
| 40 | + Action action = webHookAnnotation.action(); |
| 41 | + Resource resource = webHookAnnotation.resource(); |
| 42 | + HookContext context = new HookContext(resource, action); |
| 43 | + context.setOperator(TokenHelper.getAccount()); |
| 44 | + context.setOrganizationId(Long.parseLong(TenantContext.getCurrentTenant())); |
| 45 | + Object ret = jp.proceed(); |
| 46 | + composeParam(jp, context); |
| 47 | + context.setResponse(ret); |
| 48 | + hookQueue.push(context); |
| 49 | + return ret; |
| 50 | + } |
| 51 | + |
| 52 | + private void composeParam(ProceedingJoinPoint jp, HookContext context) { |
| 53 | + Object requestBody = getRequestBody(jp); |
| 54 | + if (Objects.nonNull(requestBody)) { |
| 55 | + context.setRequest(requestBody); |
| 56 | + } |
| 57 | + String projectKey = getProjectKeyArg(jp); |
| 58 | + if (StringUtils.isNotBlank(projectKey)) { |
| 59 | + context.setProjectKey(projectKey); |
| 60 | + } |
| 61 | + String environmentKey = getEnvironmentKeyArg(jp); |
| 62 | + if(StringUtils.isNotBlank(environmentKey)) { |
| 63 | + context.setEnvironmentKey(environmentKey); |
| 64 | + } |
| 65 | + String toggleKey = getToggleKeyArg(jp); |
| 66 | + if (StringUtils.isNotBlank(toggleKey)) { |
| 67 | + context.setToggleKey(toggleKey); |
| 68 | + } |
| 69 | + String segmentKey = getSegmentKeyArg(jp); |
| 70 | + if (StringUtils.isNotBlank(segmentKey)) { |
| 71 | + context.setSegmentKey(segmentKey); |
| 72 | + } |
| 73 | + Long id = getIdArg(jp); |
| 74 | + if (Objects.nonNull(id)){ |
| 75 | + context.setId(id); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private Object getRequestBody(ProceedingJoinPoint jp) { |
| 80 | + MethodSignature signature = (MethodSignature) jp.getSignature(); |
| 81 | + Method method = signature.getMethod(); |
| 82 | + Annotation[][] parameterAnnotations = method.getParameterAnnotations(); |
| 83 | + Object[] args = jp.getArgs(); |
| 84 | + for (int i = 0 ; i < parameterAnnotations.length; i++) { |
| 85 | + for (Annotation annotation : parameterAnnotations[i]) { |
| 86 | + if (annotation instanceof RequestBody) { |
| 87 | + return args[i]; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + private String getProjectKeyArg(ProceedingJoinPoint jp) { |
| 95 | + Object projectKey = getPathArg(jp, "projectKey"); |
| 96 | + if (Objects.isNull(projectKey)) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + return String.valueOf(projectKey); |
| 100 | + } |
| 101 | + |
| 102 | + private String getEnvironmentKeyArg(ProceedingJoinPoint jp) { |
| 103 | + Object environmentKey = getPathArg(jp, "environmentKey"); |
| 104 | + if (Objects.isNull(environmentKey)) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + return String.valueOf(environmentKey); |
| 108 | + } |
| 109 | + |
| 110 | + private String getToggleKeyArg(ProceedingJoinPoint jp) { |
| 111 | + Object toggleKey = getPathArg(jp, "toggleKey"); |
| 112 | + if (Objects.isNull(toggleKey)) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + return String.valueOf(toggleKey); |
| 116 | + } |
| 117 | + |
| 118 | + private String getSegmentKeyArg(ProceedingJoinPoint jp) { |
| 119 | + Object segmentKey = getPathArg(jp, "segmentKey"); |
| 120 | + if (Objects.isNull(segmentKey)) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + return String.valueOf(segmentKey); |
| 124 | + } |
| 125 | + |
| 126 | + private Long getIdArg(ProceedingJoinPoint jp) { |
| 127 | + Object idArg = getPathArg(jp, "id"); |
| 128 | + Long id = null; |
| 129 | + if (Objects.isNull(idArg)) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + try { |
| 133 | + id = (Long) idArg; |
| 134 | + } catch (Exception e) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + return id; |
| 138 | + } |
| 139 | + |
| 140 | + private Object getPathArg(ProceedingJoinPoint jp, String name) { |
| 141 | + MethodSignature signature = (MethodSignature) jp.getSignature(); |
| 142 | + Method method = signature.getMethod(); |
| 143 | + Annotation[][] parameterAnnotations = method.getParameterAnnotations(); |
| 144 | + Object[] args = jp.getArgs(); |
| 145 | + for (int i = 0 ; i < parameterAnnotations.length; i++) { |
| 146 | + for (Annotation annotation : parameterAnnotations[i]) { |
| 147 | + if (annotation instanceof PathVariable && ((PathVariable) annotation).value().equals(name)) { |
| 148 | + return args[i]; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + private <T extends Annotation> T getMethodAnnotation(JoinPoint joinPoint, Class<T> clazz) { |
| 156 | + MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); |
| 157 | + Method method = methodSignature.getMethod(); |
| 158 | + return method.getAnnotation(clazz); |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments