UNPKG

node-web-mvc

Version:
181 lines (180 loc) 7.04 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * @module AbstractHandlerMapping * @description 抽象映射处理器 */ const HandlerMapping_1 = require("./HandlerMapping"); const HandlerExecutionChain_1 = __importDefault(require("../interceptor/HandlerExecutionChain")); const MappedInterceptor_1 = __importDefault(require("../interceptor/MappedInterceptor")); const UrlPathHelper_1 = __importDefault(require("../util/UrlPathHelper")); const PathMatcher_1 = __importDefault(require("../util/PathMatcher")); const ApplicationContextAware_1 = __importDefault(require("../context/ApplicationContextAware")); const CorsUtils_1 = __importDefault(require("../util/CorsUtils")); const CorsConfigurationSource_1 = __importDefault(require("../cors/CorsConfigurationSource")); const PreFlightHandler_1 = __importDefault(require("../cors/PreFlightHandler")); const CorsInterceptor_1 = __importDefault(require("../cors/CorsInterceptor")); const DefaultCorsProcessor_1 = __importDefault(require("../cors/DefaultCorsProcessor")); const UrlBasedCorsConfigurationSource_1 = __importDefault(require("../cors/UrlBasedCorsConfigurationSource")); const corsProcessorSymbol = Symbol['corsProcessor']; class AbstractHandlerMapping extends ApplicationContextAware_1.default { get corsProcessor() { return this[corsProcessorSymbol]; } set corsProcessor(value) { this[corsProcessorSymbol] = value; } constructor() { super(); this.urlPathHelper = new UrlPathHelper_1.default(); this.pathMatcher = new PathMatcher_1.default(); this[corsProcessorSymbol] = new DefaultCorsProcessor_1.default(); // 扩展拦截器配置,使用于子类 this.extendInterceptors(); } setUrlPathHelper(value) { this.urlPathHelper = value; } setPathMatcher(value) { this.pathMatcher = value; } /** * 所有设置的拦截器 */ setInterceptors(interceptors) { this.interceptors = interceptors; } /** * 从 this.interceptors 适配后拦截器 * 由于在框架中,暂时不需要适配,所以直接返回this.interceptors */ get adaptedInterceptors() { return this.interceptors; } /** * 获取类型为匹配型拦截器列表 */ get mappedInterceptors() { const mappedInteceptors = this.interceptors.filter((interceptor) => interceptor instanceof MappedInterceptor_1.default); return (mappedInteceptors.length > 0 ? mappedInteceptors : null); } /** * 设置当前默认处理器 * @param handler 处理器 */ setDefaultHandler(handler) { this.defaultHandler = handler; } /** * 获取当前默认处理器 */ getDefaultHandler() { return this.defaultHandler; } /** * 判断,当前是否使用路径匹配 * 暂时:不使用 */ usesPathPatterns() { return true; } /** * 扩展拦截器,主要用于子类使用。 */ extendInterceptors() { } setApplication(context) { this.appContext = context; } /** * 根据当前请求对象获取 处理器执行链 * @param context 请求上下文对象 */ getHandler(context) { const handler = this.getHandlerInternal(context) || this.getDefaultHandler(); if (!handler) { return null; } // 获取拦截器执行链 const chain = this.getHandlerExecutionChain(handler, context); // 进行跨域处理 if (this.hasCorsConfigurationSource(handler) || CorsUtils_1.default.isPreFlightRequest(context.request)) { return this.getCorsHandlerExecutionChain(chain, handler, context); } return chain; } initLookupPath(request) { const url = this.urlPathHelper.getServletPath(request); request.setAttribute(HandlerMapping_1.HANDLE_MAPPING_PATH, url); return url; } /** * 根据当前请求对应的处理器handler来获取对应的 拦截器执行链 */ getHandlerExecutionChain(handler, context) { const chain = handler instanceof HandlerExecutionChain_1.default ? handler : new HandlerExecutionChain_1.default(handler, context); // 依次遍历拦截器,将拦截器添加到调用链。 const interceptors = this.adaptedInterceptors || []; for (const interceptor of interceptors) { if (interceptor instanceof MappedInterceptor_1.default) { interceptor.matches(context.request) ? chain.addInterceptor(interceptor) : undefined; } else { chain.addInterceptor(interceptor); } } return chain; } getOrder() { return this.order; } setOrder(value) { return this.order = value; } findCorsConfigurationSource(handler) { if (handler instanceof HandlerExecutionChain_1.default) { handler = handler.getHandler(); } if (handler instanceof CorsConfigurationSource_1.default) { return handler; } return this.corsConfigurationSource; } getCorsHandlerExecutionChain(chain, handler, context) { const request = context.request; const corsConfig = this.getCorsConfiguration(handler, request); if (CorsUtils_1.default.isPreFlightRequest(request)) { // 如果是预请求 const preFlightHandler = new PreFlightHandler_1.default(corsConfig, this.corsProcessor); const corsChain = new HandlerExecutionChain_1.default(preFlightHandler, context); corsChain.setInterceptors(chain.getInterceptors()); corsChain.addInterceptor2(0, preFlightHandler); return corsChain; } // 如果是允许后的请求 chain.addInterceptor2(0, new CorsInterceptor_1.default(corsConfig, this.corsProcessor)); return chain; } hasCorsConfigurationSource(handler) { return !!this.findCorsConfigurationSource(handler); } getCorsConfiguration(handler, request) { var _a; const configSource = this.findCorsConfigurationSource(handler); return (_a = configSource === null || configSource === void 0 ? void 0 : configSource.getCorsConfiguration) === null || _a === void 0 ? void 0 : _a.call(configSource, request); } setCorsConfigurations(config) { if ((config === null || config === void 0 ? void 0 : config.size) < 1) { this.corsConfigurationSource = null; return; } const source = new UrlBasedCorsConfigurationSource_1.default(); source.setPatchMatcher(this.pathMatcher); source.setCorsConfigurations(config); this.corsConfigurationSource = source; } } exports.default = AbstractHandlerMapping;