node-web-mvc
Version:
node spring mvc
72 lines (71 loc) • 2.62 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const PathMatcher_1 = __importDefault(require("../util/PathMatcher"));
const HandlerMapping_1 = require("../mapping/HandlerMapping");
class MappedInterceptor {
/**
* 构造一个路径匹配型拦截器
* @param includePatterns 包含项规则列表
* @param excludePatterns 排除项规则列表
* @param interceptor 当前实际承载的拦截器
*/
constructor(includePatterns, excludePatterns, interceptor) {
// 路径匹配器
this.pathMatcher = new PathMatcher_1.default();
this.includePatterns = includePatterns || [];
this.excludePatterns = excludePatterns || [];
this.interceptor = interceptor;
}
/**
* 根据传入的请求对象进行路径匹配,用以判定是否可以使用当前拦截器。
*/
matches(request) {
// 这里仅做根据请求路径进行匹配,暂不支持高级路径匹配
const path = request.getAttribute(HandlerMapping_1.HANDLE_MAPPING_PATH);
// 1.优先执行排除项
for (const pattern of this.excludePatterns) {
if (this.matchPattern(pattern, path)) {
// 如果匹配为排除项,则返回false
return false;
}
}
if (this.includePatterns.length < 1) {
// 2. 如果没有配置包含项规则,则直接返回true
return true;
}
// 3. 匹配包含项
for (const pattern of this.includePatterns) {
if (this.matchPattern(pattern, path)) {
return true;
}
}
return false;
}
/**
* 执行路径规则匹配
* @param pattern 规则
* @param path 路径
*/
matchPattern(pattern, path) {
return this.pathMatcher.match(pattern, path);
}
/**
* 设置当前路径匹配器
*/
setPathMatcher(pathMatcher) {
this.pathMatcher = pathMatcher;
}
preHandle(request, response, handler) {
return this.interceptor.preHandle(request, response, handler);
}
postHandle(request, response, handler, modelAndView) {
return this.interceptor.postHandle(request, response, handler, modelAndView);
}
afterCompletion(request, response, handler, ex) {
return this.interceptor.afterCompletion(request, response, handler, ex);
}
}
exports.default = MappedInterceptor;