예제 #1
0
 public AqiParam(AqiParam aqiParam, Dictionary <string, string> map)
 {
     pName        = aqiParam.pName;
     pRefer       = aqiParam.pRefer;
     pGroup       = aqiParam.pGroup;
     pRefer       = aqiParam.pRefer;
     bPermutation = aqiParam.bPermutation;
     pUnique      = aqiParam.pUnique;
     foreach (var kv in map)
     {
         this.Add(kv.Key, kv.Value);
         pName = pName.Replace("{" + kv.Key + "}", kv.Value);
     }
 }
예제 #2
0
        public AqiParam(string name, AqiParam aqiParam)
        {
            pName = name;
            if (aqiParam != null)
            {
                pRefer       = aqiParam.pRefer;
                pGroup       = aqiParam.pGroup;
                pRefer       = aqiParam.pRefer;
                bPermutation = aqiParam.bPermutation;
                pUnique      = aqiParam.pUnique;
                pHeader      = aqiParam.pHeader;
                pBody        = aqiParam.pBody;

                foreach (var kv in aqiParam)
                {
                    base.Add(kv.Key, kv.Value);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// 读取参数
        ///     自动识别Enabled、Name、Param、Refer、Group、各种小写
        /// </summary>
        /// <param name="jObject">JSONObject对象</param>
        /// <param name="baseAqiParam">模板AqiParam</param>
        /// <returns></returns>
        private static AqiParam createParamFormJsonObject(JObject jObject, AqiParam baseAqiParam = null)
        {
            //检查4个属性
            //1开启
            JToken jt = jObject.GetValue(ENEABLED);

            if (jt == null)
            {
                return(null);
            }
            else if (jt.ToObject <bool>() == false)
            {
                return(null);
            }
            AqiParam ap = null;

            //2名称
            jt = jObject.GetValue(TEMPLATE);
            if (jt != null && jt.ToObject <bool>() == true)
            {
                //模板
                ap            = new AqiParam(TEMPLATE);
                ap.IsTemplate = true;
            }
            else
            {
                jt = jObject.GetValue(NAME);
                if (jt == null)
                {
                    return(null);
                }
                //缓存名称
                string name = jt.ToString();
                ap = new AqiParam(name, baseAqiParam);
            }

            //3参数
            jt = jObject.GetValue(PARAM);
            if (jt == null)
            {
                //没有Param属性
                //将所有其他非首字母大写的属性当作参数
                foreach (KeyValuePair <string, JToken> kv in jObject)
                {
                    if (Regex.IsMatch(kv.Key, REGEX_LOWWORLD))
                    {
                        //TODO 是否使用ap[]= 代替可以忽略相同键错误
                        ap.Add(kv.Key, kv.Value.ToString());
                    }
                }
            }
            else
            {
                //有Param属性
                if (jt.Type == JTokenType.String)
                {
                    ap.Add("", jt.ToString());
                }
                else if (jt.Type == JTokenType.Object)
                {
                    foreach (KeyValuePair <string, JToken> kv in jt as JObject)
                    {
                        if (Regex.IsMatch(kv.Key, REGEX_LOWWORLD))
                        {
                            ap.Add(kv.Key, kv.Value.ToString());
                        }
                    }
                }
            }

            //4参数参照
            jt = jObject.GetValue(REFER);
            if (jt != null)
            {
                ap.pRefer = jt.ToString();
            }

            //5分组
            jt = jObject.GetValue(GROUP);
            if (jt != null)
            {
                ap.pGroup = jt.ToString();
            }

            //6置换
            jt = jObject.GetValue(PERMUTATION);
            if (jt != null)
            {
                ap.pGroup = jt.ToString();
            }

            //7Header
            jt = jObject.GetValue(HEADER);
            if (jt != null)
            {
                ap.pHeader = JsonConvert.DeserializeObject <Dictionary <string, string> >(jt.ToString());
            }

            //8Body
            jt = jObject.GetValue(BODY);
            if (jt != null)
            {
                AqiConstant.BodyFormatType bft = AqiConstant.BodyFormatType.NONE;
                string content = null;
                JToken jtsub   = jt.SelectToken(BODY_TYPE);
                if (jtsub != null)
                {
                    if (Enum.TryParse(jtsub.ToString(), out bft))
                    {
                        jtsub = jt.SelectToken(BODY_CONTENT);
                        if (jtsub != null)
                        {
                            content = jtsub.ToString();
                        }
                    }
                    else
                    {
                        throw new NotSupportedException("参数Body.Type=" + jtsub.ToString() + "是不被支持的!");
                    }
                }

                if (!String.IsNullOrEmpty(content))
                {
                    switch (bft)
                    {
                    case AqiConstant.BodyFormatType.Hex:
                        ap.pBody = Enumerable.Range(0, content.Length)
                                   .Where(x => x % 2 == 0)
                                   .Select(x => Convert.ToByte(content.Substring(x, 2), 16))
                                   .ToArray();
                        break;

                    case AqiConstant.BodyFormatType.Base64:
                        ap.pBody = Convert.FromBase64String(content);
                        break;

                    case AqiConstant.BodyFormatType.Text:
                        ap.pBody = Encoding.UTF8.GetBytes(content);
                        break;

                    case AqiConstant.BodyFormatType.NONE:
                    default:
                        break;
                    }
                }
            }

            //if(ap.Count == 0){
            //    ap = null;
            //}

            return(ap);
        }
예제 #4
0
        /// <summary>
        /// 读取参数列表
        ///     从 JSON 文件
        /// </summary>
        /// <param name="icp">ICacheParam</param>
        /// <param name="listProperty">(可选)属性列表</param>
        /// <returns></returns>
        public static List <AqiParam> CreateListFormJson(ICacheParam icp, params string[] listProperty)
        {
            List <AqiParam> listParam    = new List <AqiParam>();
            string          propertyPath = String.Join(".", listProperty);

            try
            {
                //JSON路径
                string jsonPath = icp.GetJsonFile();
                if (!File.Exists(jsonPath))
                {
                    return(listParam);
                }

                //读取JSON
                StreamReader sr       = new StreamReader(jsonPath);
                string       jsonText = sr.ReadToEnd();
                //转JSON Object
                JObject jo = JObject.Parse(jsonText);
                JToken  jt = jo.SelectToken(propertyPath);

                if (jt == null || !jt.HasValues)
                {
                    return(null);
                }
                else if (jt is JArray)
                {
                    //读取集合(任意个参数)
                    JEnumerable <JToken> je     = jt.Children();
                    AqiParam             baseAP = null;
                    foreach (JToken j in je)
                    {
                        AqiParam ap = createParamFormJsonObject(j as JObject, baseAP);
                        if (ap != null)
                        {
                            if (ap.IsTemplate)
                            {
                                baseAP = ap;
                            }
                            else
                            {
                                listParam.Add(ap);
                            }
                        }
                    }
                }
                else if (jt is JObject)
                {
                    //读取对象(仅一个参数)
                    AqiParam ap = createParamFormJsonObject(jt as JObject);
                    if (ap != null)
                    {
                        listParam.Add(ap);
                    }
                }
            }
            catch (System.Exception ex)
            {
                throw new ParamException("参数创建错误", ex);
            }

            return(listParam);
        }