define([ "dojo/_base/declare", "dojo/_base/lang", "dojo/when", "./NotFoundException", "./HttpException", "./Resource", "implab/safe" ], function (declare, lang, when, notFoundException, httpException, resource, safe) { return declare([], { _resourcesConfig: null, constructor: function (options) { safe.argumentNotNull(options, "options"); safe.argumentNotNull(options.resourcesConfig, "options.resourcesConfig"); this._resourcesConfig = options.resourcesConfig; }, createResource: function (req) { let container = req.getContainer(); return container.configure(this._resourcesConfig) .then(function(){ let rc = container.getService("resource"); if(rc.hasOwnProperty("isInstanceOf") && rc.isInstanceOf(resource)) { return rc; } else { return new resource(lang.mixin(rc,{request: req})); } }); }, invoke: function (req/*, next*/) { return when(this.createResource(req), function (rc) { req.path.split(/\/+/).forEach(function (child) { if (child) { rc = rc.getChild(child); if (!rc) { throw new notFoundException(); } } }); return rc.invoke(); }); } }); });