import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.text.MessageFormat;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**
* 通过注解实现调用指定的拦截器
*
* @author Saleson Lee.
* @date 2016年3月19日
* @time 下午6:58:10
*/
public class AnnotationHandlerInterceptor extends HandlerInterceptorAdapter {
private static final Logger LOG = LoggerFactory
.getLogger(AnnotationHandlerInterceptor.class);
/**
* 需要过滤的注解类型
*/
@SuppressWarnings("rawtypes")
private Class annotationClass;
/**
* 过滤器
*/
private AsyncHandlerInterceptor[] handlerInterceptors;
/**
* 检测类或方法的注解
*/
private AnnotationTarget annotationTarget;
@SuppressWarnings("unchecked")
@PostConstruct
public void init() throws Exception {
if (this.annotationClass == null) {
throw new Exception("属性annotationClass为空");
} else if (handlerInterceptors == null) {
throw new Exception("属性handlerInterceptors为空");
}
if (annotationTarget == null) {
Target target = (Target) annotationClass
.getAnnotation(Target.class);
if (target == null) {
throw new Exception(MessageFormat.format("注解{0}没有定义{1}注解",
annotationClass, target));
}
ElementType[] eTypes = target.value();
if (ArrayUtils.contains(eTypes, ElementType.METHOD)
&& ArrayUtils.contains(eTypes, ElementType.TYPE)) {
annotationTarget = AnnotationTarget.TYPE_AND_METHOD;
} else if (ArrayUtils.contains(eTypes, ElementType.METHOD)) {
annotationTarget = AnnotationTarget.METHOD;
} else if (ArrayUtils.contains(eTypes, ElementType.TYPE)) {
annotationTarget = AnnotationTarget.TYPE;
} else {
throw new Exception(MessageFormat.format(
"注解{0}中{1}注解没有定义{2}或{3}", annotationClass, target,
ElementType.TYPE, AnnotationTarget.METHOD));
}
}
printDebugInitLog();
}
@SuppressWarnings("unchecked")
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
HandlerMethod method = (HandlerMethod) handler;
boolean invokable = false;
switch (annotationTarget) {
case METHOD:
invokable = method.getMethod().getAnnotation(annotationClass) != null;
break;
case TYPE:
invokable = method.getBeanType().getAnnotation(annotationClass) != null;
break;
case TYPE_AND_METHOD:
invokable = method.getMethod().getAnnotation(annotationClass) != null
|| method.getBeanType().getAnnotation(annotationClass) != null;
break;
}
if (invokable) {
return invokeHandlerInterceptorsPreHandle(request, response,
handler);
}
return true;
}
/**
* 调用过滤器
*
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
private boolean invokeHandlerInterceptorsPreHandle(
HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
for (AsyncHandlerInterceptor handlerInterceptor : handlerInterceptors) {
if (!handlerInterceptor.preHandle(request, response,
handler)) {
return false;
}
}
return true;
}
/**
* @param handlerInterceptors
* the handlerInterceptors to set
*/
public void setHandlerInterceptors(
AsyncHandlerInterceptor[] handlerInterceptors) {
this.handlerInterceptors = handlerInterceptors;
}
/**
* @param annotationClass
* the annotationClass to set
*/
public void setAnnotationClass(String annotationClass) {
try {
this.annotationClass = Class.forName(annotationClass);
} catch (ClassNotFoundException e) {
LOG.error(MessageFormat.format("找不到类 ==> {0}", annotationClass), e);
}
}
/**
* @param annotationTarget
* the annotationTarget to set
*/
public void setAnnotationTarget(AnnotationTarget annotationTarget) {
this.annotationTarget = annotationTarget;
}
/**
* 打印Debug日志
*/
private void printDebugInitLog() {
switch (annotationTarget) {
case TYPE_AND_METHOD:
LOG.debug("拦截标有{}注解的Controller类和方法, 调用过滤器{}", annotationClass,
handlerInterceptors);
break;
case TYPE:
LOG.debug("拦截标有{}注解的Controller类, 调用过滤器{}", annotationClass,
handlerInterceptors);
break;
case METHOD:
LOG.debug("拦截标有{}注解的Controller类方法, 调用过滤器{}", annotationClass,
handlerInterceptors);
}
}
enum AnnotationTarget {
TYPE_AND_METHOD, TYPE, METHOD
}
}
通过注解实现调用指定的拦截器
最新推荐文章于 2021-08-30 11:31:06 发布