Beispiel #1
0
 private static void AddBadParameterInfo(ref JObject result, Functions function, string language)
 {
     result.Add(FieldKeyword.Success, false);
     result.Add(FieldKeyword.ResultType, ResultType.Null);
     result.Add(FieldKeyword.CommonError, ErrorNumber.SeeDetail.ToString());
     result.Add(FieldKeyword.ErrorDetail, MessageHelper.GetMessage(ErrorNumber.BadParameter, language,
                                                                   new[] { FunctionKeyword.GetFunctionsName(function, language) }));
 }
Beispiel #2
0
        public Stream Method(string funcName, Stream stream)
        {
            var timer = new System.Diagnostics.Stopwatch();

            timer.Start();
            if (_tracer.IsInfoEnabled)
            {
                _tracer.InfoFormat("Call function {0}.", funcName);
            }
            var result = new JObject();

            WebOperationContext context = WebOperationContext.Current;

            #region check context
            if (context == null)
            {
                if (_tracer.IsErrorEnabled)
                {
                    _tracer.Error(ErrorNumber.CommonBadContext.ToString());
                }
                AddFailInfo(ref result, ErrorNumber.CommonBadContext.ToString(), null);
                return(new MemoryStream(KORT.Util.Tools.GZipCompress(Encoding.UTF8.GetBytes(result.ToString()))));
            }
            else
            {
                context.OutgoingResponse.ContentType = "application/json";
            }
            #endregion

            if (!DBHelper.IsWorking())
            {
                AddFailInfo(ref result, ErrorNumber.ServerInMaintenance.ToString(), null);
                return(new MemoryStream(KORT.Util.Tools.GZipCompress(Encoding.UTF8.GetBytes(result.ToString()))));
            }

            Functions func;

            #region check function
            try
            {
                func = (Functions)Enum.Parse(typeof(Functions), funcName);
            }
            catch (Exception)
            {
                if (_tracer.IsErrorEnabled)
                {
                    _tracer.Error(ErrorNumber.CommonFunctionNotExist.ToString());
                }
                AddFailInfo(ref result, ErrorNumber.CommonFunctionNotExist.ToString(), null);
                return(new MemoryStream(KORT.Util.Tools.GZipCompress(Encoding.UTF8.GetBytes(result.ToString()))));
            }

            if (!MethodDictionary.ContainsKey(func))
            {
                if (_tracer.IsErrorEnabled)
                {
                    _tracer.Error(ErrorNumber.CommonFunctionNotExist.ToString());
                }
                AddFailInfo(ref result, ErrorNumber.CommonFunctionNotExist.ToString(), null);
                return(new MemoryStream(KORT.Util.Tools.GZipCompress(Encoding.UTF8.GetBytes(result.ToString()))));
            }
            #endregion

            JObject request;
            #region parse request
            try
            {
                using (var decompressStream = new MemoryStream())
                {
                    KORT.Util.Tools.GZipDecompress(stream, decompressStream);
                    decompressStream.Position = 0;
                    StreamReader reader = new StreamReader(decompressStream, Encoding.UTF8);
                    string       text   = reader.ReadToEnd();
                    request = JObject.Parse(text);
                }
            }
            catch (Exception e)
            {
                if (_tracer.IsErrorEnabled)
                {
                    _tracer.Error(ErrorNumber.CommonBadRequest.ToString(), e);
                }
                AddFailInfo(ref result, ErrorNumber.CommonBadRequest.ToString(), funcName);
                return(new MemoryStream(KORT.Util.Tools.GZipCompress(Encoding.UTF8.GetBytes(result.ToString()))));
            }
            #endregion
            if (_tracer.IsDebugEnabled)
            {
                _tracer.Debug(request.ToString(Formatting.Indented));
            }

            string token = request[FieldKeyword.Token] != null ? request[FieldKeyword.Token].ToString() : null;
            #region check token
            if (func != Functions.Login && (string.IsNullOrEmpty(token) || !_session.Hit(token)))
            {
                //no token or expires token
                if (_tracer.IsInfoEnabled)
                {
                    _tracer.Info(ErrorNumber.CommonExpireToken.ToString());
                }
                AddFailInfo(ref result, ErrorNumber.CommonExpireToken.ToString(), null);
                return(new MemoryStream(KORT.Util.Tools.GZipCompress(Encoding.UTF8.GetBytes(result.ToString()))));
            }
            #endregion

            #region call functions
            currentSession = _session.GetSession(token);

            if (func != Functions.Login &&
                (currentSession == null || !FunctionKeyword.CheckFunction(currentSession.UserType, func)))
            {
                if (_tracer.IsInfoEnabled)
                {
                    _tracer.Info(ErrorNumber.CommonWrongAuthority.ToString());
                }
                AddFailInfo(ref result, ErrorNumber.CommonWrongAuthority.ToString(), funcName);
            }
            else if (MethodDictionary[func].CheckRequest != null &&
                     !MethodDictionary[func].CheckRequest(request))
            {
                if (_tracer.IsInfoEnabled)
                {
                    _tracer.Info(ErrorNumber.CommonBadParameter.ToString());
                }
                AddFailInfo(ref result, ErrorNumber.CommonBadParameter.ToString(), funcName);
            }
            else
            {
                try
                {
                    var currentLanguage = request[FieldKeyword.Language] != null ? request[FieldKeyword.Language].ToString().ToLower() : null;
                    if (currentSession == null && !MessageHelper.IsSupport(currentLanguage))
                    {
                        currentLanguage = SupportLanguage.Default;
                    }
                    if (currentSession != null)
                    {
                        if (MessageHelper.IsSupport(currentLanguage))
                        {
                            currentSession.Language = currentLanguage;
                        }
                        else
                        {
                            currentLanguage = currentSession.Language;
                        }
                    }
                    System.Diagnostics.Debug.WriteLine("use language:" + currentLanguage);
                    MethodDictionary[func].Logic(request, ref result, currentLanguage, ref currentSession);
                }
                catch (Exception e)
                {
                    if (_tracer.IsErrorEnabled)
                    {
                        _tracer.Error(ErrorNumber.Other.ToString(), e);
                    }
                    AddFailInfo(ref result, ErrorNumber.Other.ToString(), e.Message);
                }
            }
            #endregion

            if (currentSession != null)
            {
                try
                {
                    timer.Stop();
                    LogHelper.SaveLog(currentSession.UserName, func.ToString(), request.ToString(), result.ToString(), timer.ElapsedMilliseconds);
                }
                catch (Exception e)
                {
                    if (_tracer.IsErrorEnabled)
                    {
                        _tracer.Error(ErrorNumber.Other.ToString(), e);
                    }
                    AddFailInfo(ref result, ErrorNumber.Other.ToString(), e.Message);
                }
            }

            if (_tracer.IsInfoEnabled)
            {
                _tracer.InfoFormat("End function {0}.", funcName);
            }
            return(new MemoryStream(KORT.Util.Tools.GZipCompress(Encoding.UTF8.GetBytes(result.ToString()))));
        }