通过注解实现调用指定的拦截器

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
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值