public void init(HttpContext ctx) { this.ctx = ctx; this.api = new JDApiBase(); this.api.env = this; this._GET = new NameValueCollection(ctx.Request.QueryString); this._POST = new NameValueCollection(ctx.Request.Form); this.isTestMode = int.Parse(ConfigurationManager.AppSettings["P_TEST_MODE"] ?? "0") != 0; this.debugLevel = int.Parse(ConfigurationManager.AppSettings["P_DEBUG"] ?? "0"); this.appName = api.param("_app", "user", "G") as string; this.appType = Regex.Replace(this.appName, @"(\d+|-\w+)$", ""); if (ctx.Request.ContentType != null && ctx.Request.ContentType.IndexOf("/json") > 0) { var rd = new StreamReader(ctx.Request.InputStream); var jsonStr = rd.ReadToEnd(); this.JsonContent = this.api.jsonDecode(jsonStr); if (JsonContent is IDictionary <string, object> ) { var dict = (IDictionary <string, object>)JsonContent; foreach (var kv in dict) { if (!(kv.Value is IList || kv.Value is IDictionary)) { this._POST[kv.Key] = kv.Value.ToString(); } } } } if (this.isTestMode) { api.header("X-Daca-Test-Mode", "1"); } // TODO: X-Daca-Mock-Mode, X-Daca-Server-Rev }
public MyException(int code, object debugInfo = null, string message = null) : base(message != null? message : JDApiBase.GetErrInfo(code)) { Code = code; DebugInfo = debugInfo; }
// TODO: asAdmin public object callSvc(string ac, JsObject param = null, JsObject postParam = null, CallSvcOpt opt = null) { Match m = Regex.Match(ac, @"(\w+)(?:\.(\w+))?$"); string ac1 = null; string table = null; string clsName = null; string methodName = null; if (m.Groups[2].Length > 0) { table = m.Groups[1].Value; ac1 = m.Groups[2].Value; clsName = onCreateAC(table); methodName = "api_" + ac1; } else { clsName = "Global"; methodName = "api_" + m.Groups[1].Value; } JDApiBase api = null; Assembly asm = JDEnvBase.getAsmembly(); api = asm.CreateInstance("JDApi." + clsName) as JDApiBase; if (api == null) { if (table == null) { throw new MyException(JDApiBase.E_PARAM, "bad ac=`" + ac + "` (no Global)"); } int code = !this.api.hasPerm(JDApiBase.AUTH_LOGIN)? JDApiBase.E_NOAUTH : JDApiBase.E_FORBIDDEN; throw new MyException(code, string.Format("Operation is not allowed for current user on object `{0}`", table)); } Type t = api.GetType(); MethodInfo mi = t.GetMethod(methodName); if (mi == null) { throw new MyException(JDApiBase.E_PARAM, "bad ac=`" + ac + "` (no method)"); } api.env = this; NameValueCollection[] bak = null; if (opt != null) { if (opt.backupEnv) { bak = new NameValueCollection[] { this._GET, this._POST } } ; if (opt.isCleanCall) { this._GET = new NameValueCollection(); this._POST = new NameValueCollection(); } } if (param != null) { foreach (var kv in param) { this._GET[kv.Key] = kv.Value.ToString(); } } if (postParam != null) { foreach (var kv in postParam) { this._POST[kv.Key] = kv.Value.ToString(); } } object ret = null; if (clsName == "Global") { ret = mi.Invoke(api, null); } else if (t.IsSubclassOf(typeof(AccessControl))) { AccessControl accessCtl = api as AccessControl; accessCtl.init(table, ac1); accessCtl.before(); object rv = mi.Invoke(api, null); //ret[1] = t.InvokeMember(methodName, BindingFlags.InvokeMethod, null, api, null); accessCtl.after(ref rv); ret = rv; } else { throw new MyException(JDApiBase.E_SERVER, "misconfigured ac=`" + ac + "`"); } if (ret == null) { ret = "OK"; } if (bak != null) { this._GET = bak[0] as NameValueCollection; this._POST = bak[1] as NameValueCollection; } return(ret); }