UNPKG

node-web-mvc

Version:
98 lines (97 loc) 3.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * @module AbstractHandlerMethodMapping * @description 抽象请求方法映射 */ const AbstractHandlerMapping_1 = __importDefault(require("./AbstractHandlerMapping")); const MappingRegistration_1 = __importDefault(require("./registry/MappingRegistration")); const HandlerMethod_1 = __importDefault(require("../method/HandlerMethod")); class AbstractHandlerMethodMapping extends AbstractHandlerMapping_1.default { constructor() { super(); this.corsLookup = new Map(); this.registrations = new Map(); } /** * 注册一个映射方法 */ registerHandlerMethod(name, mapping, handler, method) { const methodHandler = new HandlerMethod_1.default(handler, method, this.appContext.getBeanFactory()); const corsConfig = this.initCorsConfiguration(handler, method); if (corsConfig) { this.corsLookup.set(methodHandler, corsConfig); } this.registrations.set(mapping, new MappingRegistration_1.default(mapping, methodHandler, name)); } /** * 移除一个映射方法 * @param mapping */ removeHandlerMethod(mapping) { this.registrations.delete(mapping); } /** * 获取所有注册的Mappings * @returns */ getRegistrations() { return this.registrations; } /** * 根据请求上下信息,获取用于处理当前请求的处理器. */ getHandlerInternal(context) { const lookupPath = this.initLookupPath(context.request); const handler = this.lookupHandlerMethod(lookupPath, context.request); return handler ? handler.createWithResolvedBean() : null; } /** * 根据请求查找对应的HandlerMethod */ lookupHandlerMethod(lookupPath, request) { for (const registration of this.registrations.values()) { const handler = this.match(registration, lookupPath, request); // 如果没有找到,则继续查找 if (!handler) continue; return handler; } } getMappingForMethod(handler) { for (const info of this.registrations.values()) { if (info.getHandlerMethod() === handler) { return info.getMapping(); } } } hasCorsConfigurationSource(handler) { const resolvedHandler = handler.resolvedFromHandlerMethod || handler; return super.hasCorsConfigurationSource(handler) || !!this.corsLookup.get(resolvedHandler); } getCorsConfiguration(handler, request) { let corsConfig = super.getCorsConfiguration(handler, request); if (handler instanceof HandlerMethod_1.default) { const resolvedHandler = handler.resolvedFromHandlerMethod || handler; // 获取方法cors配置 const methodCorsConfig = this.corsLookup.get(resolvedHandler); if (corsConfig && methodCorsConfig) { // 合并 corsConfig = corsConfig.combine(methodCorsConfig); } else { // 补全 corsConfig = corsConfig || methodCorsConfig; } } return corsConfig; } initCorsConfiguration(beanType, method) { // 子类重写实现 return null; } } exports.default = AbstractHandlerMethodMapping;