mirror of
https://gitee.com/dromara/easy-es.git
synced 2025-12-08 01:59:18 +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();
|
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.Intercepts;
|
||||||
import cn.easyes.annotation.Signature;
|
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.common.utils.ExceptionUtils;
|
||||||
import cn.easyes.extension.context.Interceptor;
|
import cn.easyes.extension.context.Interceptor;
|
||||||
import cn.easyes.extension.context.Invocation;
|
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.InvocationHandler;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@ -76,11 +81,23 @@ public class Plugin implements InvocationHandler {
|
|||||||
// 检查被@Signature标记的方法是否存在
|
// 检查被@Signature标记的方法是否存在
|
||||||
for (Signature sig : sigs) {
|
for (Signature sig : sigs) {
|
||||||
Set<Method> methods = signatureMap.computeIfAbsent(sig.type(), k -> new HashSet<>());
|
Set<Method> methods = signatureMap.computeIfAbsent(sig.type(), k -> new HashSet<>());
|
||||||
|
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 {
|
try {
|
||||||
Method method = sig.type().getMethod(sig.method(), sig.args());
|
Method method = sig.type().getMethod(sig.method(), sig.args());
|
||||||
methods.add(method);
|
methods.add(method);
|
||||||
} catch (NoSuchMethodException e) {
|
} catch (NoSuchMethodException e) {
|
||||||
throw new RuntimeException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
|
throw new EasyEsException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return signatureMap;
|
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