mirror of
https://gitee.com/dromara/easy-es.git
synced 2025-12-06 17:18:57 +08:00
!68 extension #I6DDTA 插件拦截实现,方法名称支持使用正则表达式的方式进行编写
Merge pull request !68 from 黄小赢/feature
This commit is contained in:
commit
b76ba612dc
@ -39,4 +39,10 @@ public @interface Signature {
|
||||
*/
|
||||
Class<?>[] args();
|
||||
|
||||
/**
|
||||
* use regexp
|
||||
* @return java method name is use regexp
|
||||
*/
|
||||
boolean useRegexp() default false;
|
||||
|
||||
}
|
||||
|
||||
@ -3,6 +3,8 @@ package cn.easyes.extension.plugins;
|
||||
|
||||
import cn.easyes.annotation.Intercepts;
|
||||
import cn.easyes.annotation.Signature;
|
||||
import cn.easyes.common.exception.EasyEsException;
|
||||
import cn.easyes.common.utils.CollectionUtils;
|
||||
import cn.easyes.common.utils.ExceptionUtils;
|
||||
import cn.easyes.extension.context.Interceptor;
|
||||
import cn.easyes.extension.context.Invocation;
|
||||
@ -10,10 +12,13 @@ import cn.easyes.extension.context.Invocation;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -76,14 +81,26 @@ public class Plugin implements InvocationHandler {
|
||||
// 检查被@Signature标记的方法是否存在
|
||||
for (Signature sig : sigs) {
|
||||
Set<Method> methods = signatureMap.computeIfAbsent(sig.type(), k -> new HashSet<>());
|
||||
try {
|
||||
Method method = sig.type().getMethod(sig.method(), sig.args());
|
||||
methods.add(method);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
|
||||
if (sig.useRegexp()) {
|
||||
Pattern pattern = Pattern.compile(sig.method());
|
||||
Set<Method> methodSet = Arrays.stream(sig.type().getMethods())
|
||||
.filter(item -> pattern.matcher(item.getName()).matches())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
if (CollectionUtils.isEmpty(methodSet)) {
|
||||
throw new EasyEsException("This regular expression does not match any methods:" + sig.type() + " named " + sig.method());
|
||||
}
|
||||
methods.addAll(methodSet);
|
||||
} else {
|
||||
try {
|
||||
Method method = sig.type().getMethod(sig.method(), sig.args());
|
||||
methods.add(method);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new EasyEsException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return signatureMap;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
package cn.easyes.test.interceptor;
|
||||
|
||||
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import cn.easyes.annotation.Intercepts;
|
||||
import cn.easyes.annotation.Signature;
|
||||
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
|
||||
import cn.easyes.core.conditions.interfaces.BaseEsMapper;
|
||||
import cn.easyes.extension.context.Interceptor;
|
||||
import cn.easyes.extension.context.Invocation;
|
||||
|
||||
/**
|
||||
* 测试方法使用正则表达式
|
||||
* @author huangjy
|
||||
*/
|
||||
@Intercepts(
|
||||
{
|
||||
@Signature(type = BaseEsMapper.class, method = "select.*", args = {LambdaEsQueryWrapper.class}, useRegexp = true),
|
||||
@Signature(type = BaseEsMapper.class, method = "search", args = {SearchRequest.class, RequestOptions.class}),
|
||||
@Signature(type = BaseEsMapper.class, method = "insert|update", args = {Object.class}, useRegexp = true),
|
||||
@Signature(type = BaseEsMapper.class, method = ".*ById", args = {Object.class}, useRegexp = true),
|
||||
}
|
||||
)
|
||||
// @Component
|
||||
public class TenantLineInnerInterceptor implements Interceptor {
|
||||
|
||||
|
||||
@Override
|
||||
public Object intercept(Invocation invocation) throws Throwable {
|
||||
// Object[] args = invocation.getArgs();
|
||||
// Object arg = args[0];
|
||||
// if (arg instanceof LambdaEsQueryWrapper) {
|
||||
// LambdaEsQueryWrapper wrapper = ((LambdaEsQueryWrapper) args[0]);
|
||||
// wrapper.eq("tenantId", "1");
|
||||
// return invocation.proceed();
|
||||
// }
|
||||
System.out.println("增则拦截方法");
|
||||
return invocation.proceed();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user