node-web-mvc
Version:
node spring mvc
155 lines (154 loc) • 6.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const HandlerAdapter_1 = __importDefault(require("./method/HandlerAdapter"));
const HttpRequestValidation_1 = __importDefault(require("./http/HttpRequestValidation"));
const HttpMethod_1 = __importDefault(require("./http/HttpMethod"));
const Normalizer_1 = __importDefault(require("../errors/Normalizer"));
const HandlerExceptionResolverComposite_1 = __importDefault(require("./method/exception/HandlerExceptionResolverComposite"));
const HttpStatus_1 = __importDefault(require("./http/HttpStatus"));
const HttpStatusError_1 = __importDefault(require("../errors/HttpStatusError"));
const AbstractHandlerMapping_1 = __importDefault(require("./mapping/AbstractHandlerMapping"));
const InternalErrorHandler_1 = __importDefault(require("./http/error/InternalErrorHandler"));
const NoHandlerFoundException_1 = __importDefault(require("../errors/NoHandlerFoundException"));
const ViewResolverRegistry_1 = __importDefault(require("./view/ViewResolverRegistry"));
const ViewRender_1 = __importDefault(require("./view/ViewRender"));
class DispatcherServlet {
constructor(appContext) {
this.appContext = appContext;
this.initStrategies();
}
getHandler(servletContext) {
for (const handlerMapping of this.handlerMappings) {
const executionChain = handlerMapping.getHandler(servletContext);
if (executionChain) {
return executionChain;
}
}
}
initHandlerMappings() {
this.handlerMappings = [
...this.appContext.getBeanFactory().getBeansOfType(AbstractHandlerMapping_1.default),
];
}
initHandlerAdapters() {
this.handlerAdapters = [
...this.appContext.getBeanFactory().getBeansOfType(HandlerAdapter_1.default),
];
}
initExceptionResolvers() {
this.exceptionResolver = this.appContext.getBeanFactory().getBeansOfType(HandlerExceptionResolverComposite_1.default)[0];
}
initErrorHandler() {
this.fallbackErrorHandler = this.appContext.getBeanFactory().getBeansOfType(InternalErrorHandler_1.default)[0];
}
initStrategies() {
this.initHandlerMappings();
this.initExceptionResolvers();
this.initHandlerAdapters();
this.initErrorHandler();
this.initViewResolvers();
}
initViewResolvers() {
this.viewResolverRegistry = this.appContext.getBeanFactory().getBeansOfType(ViewResolverRegistry_1.default)[0];
}
/**
* 根据当前处理的handler获取对应的处理适配器
* @param handler
*/
getHandlerAdapter(handler) {
return this.handlerAdapters.find((adapter) => adapter.supports(handler));
}
async doService(servletContext) {
var _a, _b;
try {
await this.doDispatch(servletContext);
}
catch (ex) {
console.error(ex);
servletContext.response.sendError(HttpStatus_1.default.INTERNAL_SERVER_ERROR);
}
finally {
(_b = (_a = this.fallbackErrorHandler) === null || _a === void 0 ? void 0 : _a.tryResolveException) === null || _b === void 0 ? void 0 : _b.call(_a, servletContext);
servletContext.doReleaseQueues();
}
}
async doDispatch(servletContext) {
var _a;
let handler = null;
const runtime = { error: null, mv: null };
const mappedHandler = this.getHandler(servletContext);
const request = servletContext.request;
const response = servletContext.response;
try {
if (!mappedHandler) {
throw new NoHandlerFoundException_1.default(servletContext.request);
}
// 执行拦截器: preHandler
const isKeeping = await ((_a = mappedHandler === null || mappedHandler === void 0 ? void 0 : mappedHandler.applyPreHandle) === null || _a === void 0 ? void 0 : _a.call(mappedHandler));
if (!isKeeping) {
// 如果拦截器中断了本次请求
return;
}
handler = mappedHandler.getHandler();
// 获取handler当前执行适配器
const ha = this.getHandlerAdapter(handler);
// 304处理
if (request.method == HttpMethod_1.default.GET || request.method == HttpMethod_1.default.HEAD) {
const validation = new HttpRequestValidation_1.default(request, response);
const lastModified = ha.getLastModified(request, handler);
if (validation.checkNotModified(null, lastModified)) {
return;
}
}
// 开始执行handler
const mv = await ha.handle(servletContext, handler);
// 执行拦截器:postHandler
await mappedHandler.applyPostHandle(mv);
runtime.mv = mv;
}
catch (ex) {
ex = Normalizer_1.default.normalizeError(ex);
runtime.error = ex;
}
try {
if (!response.headersSent && response.hasError) {
runtime.error = runtime.error || new HttpStatusError_1.default(response.status, request.path);
}
// 处理结果
await this.processDispatchResult(runtime.error, runtime.mv, servletContext, handler);
}
finally {
process.nextTick(() => {
var _a;
// 执行拦截器: afterCompletion
(_a = mappedHandler === null || mappedHandler === void 0 ? void 0 : mappedHandler.applyAfterCompletion) === null || _a === void 0 ? void 0 : _a.call(mappedHandler, runtime.error);
});
}
}
/**
* 处理异常
* @param { Error } error 异常信息
* @param {ControllerContext} servletContext 请求上下文
*/
async handleException(error, servletContext, handler) {
const mv = await this.exceptionResolver.resolveException(servletContext, handler, error);
if (!mv) {
// 如果异常处理器没有解决异常,则直接抛出原始错误
throw error;
}
return mv.isEmpty() ? null : mv;
}
async processDispatchResult(error, mv, servletContext, handler) {
if (error) {
// 处理异常
mv = await this.handleException(error, servletContext, handler);
}
if (mv) {
await new ViewRender_1.default(this.viewResolverRegistry).render(mv, servletContext);
}
}
}
exports.default = DispatcherServlet;