internal void Configure(WSSource Source, WSJson json) { try { if (Source != null && json != null) { if (json is WSJProperty && ((WSJProperty)json).Value is WSJValue) { WSJValue vJson = (WSJValue)((WSJProperty)json).Value; switch (((WSJProperty)json).Key) { case "readaccess": byte readAccessValue = byte.TryParse(vJson.Value, out readAccessValue) ? readAccessValue : READ_ACCESS_MODE.ACCESS_LEVEL; READ_ACCESS_MODE = new WSAccessMode(readAccessValue, READ_ACCESS_MODE.OWNER_ACCESS_ALLOWED); break; case "writeaccess": byte writeAccessValue = byte.TryParse(vJson.Value, out writeAccessValue) ? writeAccessValue : WRITE_ACCESS_MODE.ACCESS_LEVEL; WRITE_ACCESS_MODE = new WSAccessMode(writeAccessValue, WRITE_ACCESS_MODE.OWNER_ACCESS_ALLOWED); break; case "skipempty": bool skipempty = vJson.Value.IsTrue() ? true : vJson.Value.IsFalse() ? false : SkipEmpty; SkipEmpty = skipempty; break; default: break; } } } } catch (Exception) { } }
public static WSJson ToJson(this string jText) { lock (ToJsonLock) { WSJson json = null; try { jText = jText == null ? null : jText.Trim(); if (!string.IsNullOrEmpty(jText)) { //WSJObject if ('{' == jText.FirstOrDefault() && '}' == jText.LastOrDefault()) { json = JsonConvert.DeserializeObject <WSJson>(jText, new WSFilterConverter()); } //WSJArray else if ('[' == jText.FirstOrDefault() && ']' == jText.LastOrDefault()) { WSJObject jObj = (WSJObject)JsonConvert.DeserializeObject <WSJson>("{json:" + jText + "}", new WSFilterConverter()); json = jObj.Value[0].Value; } //WSJValue else { json = new WSJValue(jText); } } } catch (Exception) { } return(json); } }
public bool ToWSJson(string invalue, out WSJson outvalue, dynamic DEFAULT = null) { outvalue = null; bool ok = false; try { if (!string.IsNullOrEmpty(invalue)) { try { outvalue = invalue.ToJson(); ok = outvalue != null; } catch (Exception) { } if (!ok) { outvalue = (DataType.IsSimple() || WSConstants.SPECIAL_CASES.Any(c => c.Match(invalue))) ? new WSJValue(invalue) : DataType.IsSameOrSubclassOf(typeof(WSEntity)) && (WSEntityFFilter.OPERATIONS.STATE_OPERATIONS.Any(c => c.Match(invalue))) ? new WSJValue(invalue) : DataType.IsCollectionOf <WSEntity>() && WSEntityListFFilter.OPERATIONS.STATE_OPERATIONS.Any(c => c.Match(invalue)) ? new WSJValue(invalue) : null; } ok = outvalue != null; } } catch (Exception) { } outvalue = ok ? outvalue : DEFAULT; return(ok); }
public static bool TryGetJson(this string jText, out WSJson json) { lock (TryGetJsonLock) { json = jText.ToJson(); return(json != null); } }
public override bool Match(WSJson json, out WSStatus status) { status = json != null && json is WSJValue && Value.Equals(((WSJValue)json).Value) ? WSStatus.SUCCESS_Copy() : WSStatus.ERROR_Copy(); if (status.CODE != WSStatus.SUCCESS.CODE) { status.AddNote($"Values not match:[{{current:{Value}}}<->{{original:{((WSJValue)json).Value}}}]"); return(false); } return(true); }
private WSJProperty getFieldJson(WSTableParam param, WSJson json) { WSJProperty fJson = null; if (json is WSJObject) { fJson = ((WSJObject)json).Value.FirstOrDefault(x => param.Match(x.Key)); } else if (json is WSJArray) { fJson = ((WSJArray)json).Value.OfType <WSJObject>().FirstOrDefault(x => param.Match(x.Value[0].Key)).Value[0]; } return(fJson); }
public override bool Match(WSJson json, out WSStatus status) { status = WSStatus.NONE_Copy(); try { if (json == null) { status = WSStatus.ERROR_Copy(); return(false); } else if (!(json is WSJObject)) { status = WSStatus.ERROR_Copy(); return(false); } else { WSJObject jObj = (WSJObject)json; IEnumerable <string> keys = Value.Select(v1 => v1.Key); IEnumerable <string> keys1 = jObj.Value.Select(v1 => v1.Key); if (keys1.Any(p1 => !keys.Any(p => p1.Equals(p)))) { status = WSStatus.ERROR_Copy(); status.AddNote($"An original service generates response with additional properties:[{keys1.Where(p1 => !keys.Any(p => p1.Equals(p))).Aggregate((a,b)=>a+","+b)}] which is not exists in current object."); } else if (keys.Count() != keys1.Count()) { status = WSStatus.ERROR_Copy(); status.AddNote($"The current service generates response with additional properties:[{keys.Where(p1 => !keys1.Any(p => p1.Equals(p))).Aggregate((a, b) => a + "," + b)}] which is not exists in original object."); } else { foreach (WSJProperty jProp1 in jObj.Value) { WSJProperty jProp = Value.FirstOrDefault(x => x.Key.Equals(jProp1.Key)); WSStatus pStatus = jProp.Match(jProp1, out pStatus) ? WSStatus.SUCCESS_Copy() : pStatus; status = pStatus; if (pStatus.CODE != WSStatus.SUCCESS.CODE) { return(false); } } return(!status.childs.Any(x => x.CODE != WSStatus.SUCCESS.CODE)); } } } catch (Exception) { } return(status.CODE == WSStatus.SUCCESS.CODE); }
public List <dynamic> read(MetaFunctions CFunc, WSJson json, WSDynamicEntity _entity = null, bool multydimentional = false) { List <dynamic> result = new List <dynamic>(); try { _entity = _entity == null ? this : _entity; if (json is WSJValue) { dynamic val = readPrimitive(CFunc, (WSJValue)json, _entity); if (val != null) { result.Add(val); } } else if (json is WSJArray) { foreach (WSJson item in ((WSJArray)json).Value) { List <dynamic> val = read(CFunc, item, _entity, multydimentional); if (multydimentional) { result.Add(val); } else { result.AddRange(val); } } } else if (json is WSJObject) { List <dynamic> val = readEntity(CFunc, ((WSJObject)json).Value[0], _entity, multydimentional); if (val != null && val.Any()) { if (multydimentional) { result.Add(val); } else { result.AddRange(val); } } } } catch (Exception e) { WSStatus status = WSStatus.NONE.clone(); CFunc.RegError(GetType(), e, ref status, $"read():357"); } return(result); }
private void proceedRootParam(WSJson param) { if (param is WSJValue) { proceedRootParam((WSJValue)param); } else if (param is WSJProperty) { proceedRootParam((WSJProperty)param); } else if (param is WSJObject) { proceedRootParam((WSJObject)param); } else if (param is WSJArray) { proceedRootParam((WSJArray)param); } }
private void proceedFieldFilter(WSJson json, ref WSFieldFilters filters) { if (json is WSJValue) { proceedFieldFilter((WSJValue)json, ref filters); } else if (json is WSJProperty) { proceedFieldFilter((WSJProperty)json, ref filters); } else if (json is WSJObject) { proceedFieldFilter((WSJObject)json, ref filters); } else if (json is WSJArray) { proceedFieldFilter((WSJArray)json, ref filters); } }
public void Merge(WSTableSource ext) { base.Merge(ext); AllowOwnerAccess = ext.AllowOwnerAccess; BaseFilter = ext.BaseFilter; DeletableFilter = ext.DeletableFilter; EditableFilter = ext.EditableFilter; CreatableFilter = ext.CreatableFilter; ReadableFilter = ext.ReadableFilter; foreach (WSTableParam orgParam in Params.OfType <WSTableParam>()) { WSTableParam extParam = ext.DBParams.FirstOrDefault(p => p.WSColumnRef.NAME.Equals(orgParam.WSColumnRef.NAME)); if (extParam != null) { orgParam.Merge(extParam); } } }
private List <WSMemberSchema> readFieldSchema(WSJson json, out bool replace)// json => WSJValue || WSJProperty || WSJObject { replace = true; if (json is WSJValue) { return(readFieldSchema(new WSJProperty(((WSJValue)json).Value, new WSJArray()), out replace)); } else if (json is WSJProperty) { return(readFieldSchema((WSJProperty)json, out replace)); } else if (json is WSJObject) { return(readFieldSchema(((WSJObject)json).Value[0], out replace)); } else if (json is WSJArray) { return(readFieldSchema(((WSJArray)json).Value[0], out replace)); } return(null); }
private WSJProperty mergeFilters(WSJson baseFilter, WSJProperty dynamicFilter) { WSJProperty json = null; if (baseFilter == null || !baseFilter.IsValid) { json = dynamicFilter; } else { List <WSJProperty> fProps = new List <WSJProperty>(); json = new WSJProperty(Source.NAME, new WSJObject(fProps)); foreach (WSTableParam param in Source.DBParams) { List <WSJProperty> mProps = new List <WSJProperty>(); WSJProperty bpjValue = getFieldJson(param, baseFilter); if (bpjValue != null && bpjValue.IsValid) { mProps.Add(bpjValue); } if (dynamicFilter != null && dynamicFilter.IsValid) { WSJProperty dpjValue = getFieldJson(param, dynamicFilter); if (dpjValue != null && dpjValue.IsValid) { mProps.Add(dpjValue); } } fProps.AddRange(mProps); } } return(json); }
public abstract WSFilter GetBaseFilter(WSRequest Request, Expression _member, int _level, WSJson BaseFilter = null);
public bool Match(WSJson json) { WSStatus status = WSStatus.NONE_Copy(); return(Match(json, out status)); }
public abstract bool Match(WSJson json, out WSStatus status);
public override WSFilter GetBaseFilter(WSRequest Request, Expression _member, int _level, WSJson BaseFilter = null) { return(null); }
private WSStatus WriteCollapsedValues(JsonWriter writer, JsonSerializer serializer, WSEntity entity, WSSource xSource, WSJson collapseOption, WSRequest Request, MetaFunctions CFunc) { WSStatus status = WSStatus.NONE_Copy(); try { /******************************************************************************* * * // TODO @ANDVO : 2016-02-15 : IMPORTANT!!! => Implement security check like this : * * WSStatus status = Validate(obj, xParam, writer, serializer, security, schema); * if (status.CODE == WSStatus.SUCCESS.CODE) * { */ if (entity != null && collapseOption != null && collapseOption.IsValid) { WSTableSource childSource = (WSTableSource)CFunc.GetSourceByType(entity.GetType()); object fieldValue = null; WSParam field = null; if (collapseOption is WSJValue) { string fieldName = ((WSJValue)collapseOption).Value; field = entity.GetParam(xSource, fieldName); fieldValue = getMemberValue(entity, field, CFunc); WSPrimitiveFieldSchema fieldSchema = new WSPrimitiveFieldSchema(CFunc, (WSTableParam)field, new WSJProperty(fieldName, new WSJArray()), /*((WSTableSource)entity.getSource())*/ childSource.BaseSchema); if (Validate(fieldValue, field, writer, serializer, childSource.BaseSchema, childSource, null, ref status, Request, CFunc)) { object _obj = null; serializer.Serialize(writer, field.TryReadPrimitiveWithDefault(fieldValue, string.Empty, out _obj) ? _obj : string.Empty); writer.Flush(); status = WSStatus.SUCCESS_Copy(); } } else if (collapseOption is WSJObject) { WSJProperty collapseSrc = ((WSJObject)collapseOption).Value.FirstOrDefault(); field = entity.GetParam(childSource, collapseSrc.Key); fieldValue = getMemberValue(entity, field, CFunc); if (Validate(fieldValue, field, writer, serializer, childSource.BaseSchema, childSource, null, ref status, Request, CFunc)) { if (fieldValue == null) { serializer.Serialize(writer, "NULL"); writer.Flush(); } else if (fieldValue is WSEntity) { WSTableSource fieldSource = (WSTableSource)CFunc.GetSourceByType(fieldValue.GetType()); status = WriteCollapsedValues(writer, serializer, (WSEntity)fieldValue, fieldSource, collapseSrc.Value, Request, CFunc); } else if (fieldValue.IsCollectionOf <WSEntity>()) { WSTableSource fieldSource = (WSTableSource)CFunc.GetSourceByType(fieldValue.GetType().GetEntityType()); if (!((IEnumerable <WSEntity>)fieldValue).Any()) { serializer.Serialize(writer, "NULL"); writer.Flush(); } else { foreach (WSEntity eItem in (IEnumerable <WSEntity>)fieldValue) { status.childs.Add(WriteCollapsedValues(writer, serializer, eItem, fieldSource, collapseSrc.Value, Request, CFunc)); } status = status.IsPositive ? WSStatus.SUCCESS_Copy() : WSStatus.ERROR_Copy(); } } } } } /*} * *******************************************************************************/ } catch (Exception e) { CFunc.RegError(GetType(), e, ref status); } return(status); }
public WSJOutput(WSJson _json, List <Type> _allowedValueTypes) { allowedValueTypes = _allowedValueTypes; json = _json; }
public override WSFilter GetBaseFilter(WSRequest Request, Expression _member, int _level, WSJson _BaseFilter = null) { WSJson BaseFilter = _BaseFilter == null ? Source.BaseFilter : _BaseFilter; WSCombineFilter filter = new WSCombineFilter(WSCombineFilter.SQLMode.AndAlso); if (filter != null && BaseFilter != null && BaseFilter.IsValid) { Expression member = null; filter = BaseFilter is WSJArray ? new WSCombineFilter(WSCombineFilter.SQLMode.OrElse) : filter; BaseFilter.apply(Request, Func); if (BaseFilter != null && BaseFilter.IsValid) { if (_member.Type == Source.ReturnType) { member = _member; } else if (Parent != null) { WSTableParam paramExt = (WSTableParam)Parent.Source.GetXParam(Name); if (paramExt != null) { member = Expression.Property(_member, paramExt.WSColumnRef.NAME); } } if (member != null) { if (BaseFilter is WSJValue) { if (((WSJValue)BaseFilter).Value.IsTrue() || ((WSJValue)BaseFilter).Value.IsFalse()) { WSFilter subFilter = ((WSJValue)BaseFilter).GetOptionFilter(Func, member, _level); if (subFilter != null) { filter.Add(subFilter); } } } else if (BaseFilter is WSJObject) { foreach (WSJProperty item in ((WSJObject)BaseFilter).Value) { bool replace = true; List <WSMemberSchema> _schemas = readFieldSchema(item, out replace); if (_schemas.Any()) { foreach (WSMemberSchema _schema in _schemas) { WSFilter subFilter = _schema.GetCustomFilter(member, _level); if (subFilter != null) { filter.Add(subFilter); } } } } } else if (BaseFilter is WSJArray) { foreach (WSJson item in ((WSJArray)BaseFilter).Value) { bool replace = true; List <WSMemberSchema> _schemas = readFieldSchema(item, out replace); if (_schemas.Any()) { foreach (WSMemberSchema _schema in _schemas) { WSFilter subFilter = _schema.GetCustomFilter(member, _level); if (subFilter != null) { filter.Add(subFilter); } } } else if (item is WSJValue && (((WSJValue)item).Value.IsTrue() || ((WSJValue)item).Value.IsFalse())) { WSFilter subFilter = ((WSJValue)item).GetOptionFilter(Func, member, _level); if (subFilter != null) { filter.Add(subFilter); } } } } } } } WSFilter result = filter != null && filter.IsValid ? filter.Reduce() : null; return(result); }
public new void ReadXmlAttributes(XmlReader reader) { base.ReadXmlAttributes(reader); #region DeleteFlag string _DeleteFlag = reader["deleteFlag"]; if (!string.IsNullOrEmpty(_DeleteFlag) && GetXParam(_DeleteFlag) != null) { DeleteFlag = (WSTableParam)GetXParam(_DeleteFlag); } #endregion #region IsReadableBySchema string _AttrIsReadableBySchema = reader["readable"]; if (!string.IsNullOrEmpty(_AttrIsReadableBySchema)) { if (_AttrIsReadableBySchema.IsTrue() || _AttrIsReadableBySchema.IsFalse()) { _IsReadableBySchema = _AttrIsReadableBySchema.IsTrue(); } else { _IsReadableBySchema = true; ReadableFilter = _AttrIsReadableBySchema.ToJson(); } } #endregion #region IsCreatableBySchema string _AttrIsCreatableBySchema = reader["creatable"]; if (!string.IsNullOrEmpty(_AttrIsCreatableBySchema)) { if (_AttrIsCreatableBySchema.IsTrue() || _AttrIsCreatableBySchema.IsFalse()) { _IsCreatableBySchema = _AttrIsCreatableBySchema.IsTrue(); } else { _IsCreatableBySchema = true; CreatableFilter = _AttrIsCreatableBySchema.ToJson(); } } #endregion #region IsEditableBySchema string _AttrIsEditableBySchema = reader["editable"]; if (!string.IsNullOrEmpty(_AttrIsEditableBySchema)) { if (_AttrIsEditableBySchema.IsTrue() || _AttrIsEditableBySchema.IsFalse()) { _IsEditableBySchema = _AttrIsEditableBySchema.IsTrue(); } else { _IsEditableBySchema = true; EditableFilter = _AttrIsEditableBySchema.ToJson(); } } #endregion #region IsDeletableBySchema string _AttrIsDeletableBySchema = reader["deletable"]; if (!string.IsNullOrEmpty(_AttrIsDeletableBySchema)) { if (_AttrIsDeletableBySchema.IsTrue() || _AttrIsDeletableBySchema.IsFalse()) { _IsDeletableBySchema = _AttrIsDeletableBySchema.IsTrue(); } else { _IsDeletableBySchema = true; DeletableFilter = _AttrIsDeletableBySchema.ToJson(); } } #endregion #region AllowOwnerAccess string _AllowOwnerAccess = reader["allowOwnerAccess"]; dynamic _dAllowOwnerAccess = AllowOwnerAccess; if (!string.IsNullOrEmpty(_AllowOwnerAccess) && typeof(bool).Read(_AllowOwnerAccess, out _dAllowOwnerAccess)) { AllowOwnerAccess = _dAllowOwnerAccess; } #endregion #region BaseFilter try { string _BaseFilterValue = reader["baseFilter"]; if (!string.IsNullOrEmpty(_BaseFilterValue)) { BaseFilter = _BaseFilterValue.ToJson(); } } catch (Exception e) { WSStatus status = WSStatusBase.NONE.clone(); CFunc.RegError(GetType(), e, ref status); } #endregion }
public WSCall(HttpContext _InContext) { try { if (_InContext != null) { INPUT = new Dictionary <string, string>(); #region READ POST PARAMETERS if (_InContext.Request.HttpMethod.Equals("POST")) { #region READ FORM PARAMETERS foreach (string fKey in _InContext.Request.Form.AllKeys) { if (!string.IsNullOrEmpty(fKey)) { string fValue = _InContext.Request.Form[fKey]; INPUT.Save(fKey, fValue); } } #endregion string[] allKeys = _InContext.Request.Params.AllKeys; IEnumerable <string> actualKeys = allKeys.Where(x => x != null && !WSConstants.STANDARD_ASP_URL_PARAMS.Select(p => p.ToLower()).Contains(x.ToLower())); string[] PKeys = actualKeys.ToArray(); foreach (string PKey in _InContext.Request.Params) { if (!string.IsNullOrEmpty(PKey)) { if (!WSConstants.STANDARD_ASP_URL_PARAMS.Any(x => x.Equals(PKey))) { bool isValid = true; string PVal = _InContext.Request.Params[PKey]; if (PKey.ToLower().Equals("url")) { string RawUrl = _InContext.Request.RawUrl; if (PVal.Equals(RawUrl)) { isValid = false; } else { PVal = PVal.Split(new char[] { ',' }).FirstOrDefault(x => !x.ToLower().Equals(RawUrl.ToLower())); isValid = !string.IsNullOrEmpty(PVal); } } if (isValid) { INPUT.Save(PKey, PVal, false); } } } else { string jValue = _InContext.Request.Params[PKey]; status.AddNote("POST: try saving empty key {" + PKey + ":" + jValue + "}", WSConstants.ACCESS_LEVEL.READ); if (!string.IsNullOrEmpty(jValue)) { WSJson json = jValue.ToJson(); if (json == null) { status.AddNote("POST:failed convert json {" + jValue + "}. Try autoresolve.", WSConstants.ACCESS_LEVEL.READ); json = ("{data:" + jValue + "}").ToJson(); json = json != null && (json is WSJObject) ? ((WSJObject)json).Value[0].Value : null; } if (json == null) { status.AddNote("POST:failed convert json {" + jValue + "}", WSConstants.ACCESS_LEVEL.READ); } else { if (json is WSJArray) { WSJArray jArray = (WSJArray)json; foreach (WSJson innerJson in jArray.Value) { if (innerJson is WSJProperty) { WSJProperty jProp = (WSJProperty)innerJson; INPUT.Save(jProp.Key, jProp.Value.ToString(), false); } } } else if (json is WSJObject) { foreach (WSJProperty jProp in ((WSJObject)json).Value) { string jVal = Newtonsoft.Json.JsonConvert.SerializeObject(jProp.Value, new WSFilterConverter()); INPUT.Save(jProp.Key, jVal, false); } } else if (json is WSJProperty) { WSJProperty jProp = (WSJProperty)json; string jVal = Newtonsoft.Json.JsonConvert.SerializeObject(jProp.Value, new WSFilterConverter()); INPUT.Save(jProp.Key, jVal, false); } } } } } } #endregion #region READ QUERY-STRING PARAMETERS foreach (var queryParam in _InContext.Request.QueryString.Keys) { if (queryParam != null) { string qKey = queryParam.ToString(); string qValue = _InContext.Request.QueryString[qKey]; INPUT.Save(qKey, qValue); } } #endregion #region READ ROUTE-DATA PARAM foreach (var urlParam in _InContext.Request.RequestContext.RouteData.Values) { if (!WSConstants.STANDARD_ASP_URL_PARAMS.Select(p => p.ToLower()).Contains(urlParam.Key.ToLower())) { string uKey = urlParam.Key; string uValue = urlParam.Value.ToString(); INPUT.Save(uKey, uValue); } } #endregion SessionID = INPUT.Any(x => WSConstants.PARAMS.SESSIONID.Match(x.Key)) ? INPUT.FirstOrDefault(x => WSConstants.PARAMS.SESSIONID.Match(x.Key)).Value : string.Empty; if (string.IsNullOrEmpty(SessionID)) { if (_InContext.Session != null) { SessionID = _InContext.Session.SessionID; } else { SessionID = _InContext.Request.Params["ASP.NET_SessionId"]; } } IsLocal = _InContext.Request == null || _InContext.Request.IsLocal; UserHostAddress = _InContext.Request.UserHostAddress; HttpMethod = _InContext.Request.HttpMethod; Url = _InContext.Request.Url; Files = _InContext.Request.Files; } } catch (Exception) { } }