From 582634ff30ab4a5461b71a7518ad1f0b64dd24b5 Mon Sep 17 00:00:00 2001 From: Roger Zander Date: Wed, 7 Feb 2018 23:23:18 +0100 Subject: [PATCH] New: $exclude option on query and queryall $exclude to remove JSONPath from result --- source/jaindb/Controllers/HomeController.cs | 4 +- source/jaindb/jaindb.cs | 272 +++++++++++++------- source/jaindb/jaindb.csproj | 4 +- source/jaindb/wwwroot/inventory.ps1 | 6 +- 4 files changed, 184 insertions(+), 102 deletions(-) diff --git a/source/jaindb/Controllers/HomeController.cs b/source/jaindb/Controllers/HomeController.cs index 0b767ee..a506edf 100644 --- a/source/jaindb/Controllers/HomeController.cs +++ b/source/jaindb/Controllers/HomeController.cs @@ -221,7 +221,7 @@ public JArray Query() //string sUri = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(Request); var query = QueryHelpers.ParseQuery(sQuery); - return jDB.query(string.Join(",", query.Where(t => string.IsNullOrEmpty(t.Value)).Select(t => t.Key).ToList()), query.FirstOrDefault(t => t.Key.ToLower() == "$select").Value); + return jDB.query(string.Join(";", query.Where(t => string.IsNullOrEmpty(t.Value)).Select(t => t.Key).ToList()), query.FirstOrDefault(t => t.Key.ToLower() == "$select").Value, query.FirstOrDefault(t => t.Key.ToLower() == "$exclude").Value); } return null; } @@ -238,7 +238,7 @@ public JArray QueryAll() //string sUri = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(Request); var query = QueryHelpers.ParseQuery(sQuery); - return jDB.queryAll(string.Join(",", query.Where(t => string.IsNullOrEmpty(t.Value)).Select(t => t.Key).ToList()), query.FirstOrDefault(t => t.Key.ToLower() == "$select").Value); + return jDB.queryAll(string.Join(";", query.Where(t => string.IsNullOrEmpty(t.Value)).Select(t => t.Key).ToList()), query.FirstOrDefault(t => t.Key.ToLower() == "$select").Value, query.FirstOrDefault(t => t.Key.ToLower() == "$exclude").Value); } return null; } diff --git a/source/jaindb/jaindb.cs b/source/jaindb/jaindb.cs index 8908dd8..2aab37d 100644 --- a/source/jaindb/jaindb.cs +++ b/source/jaindb/jaindb.cs @@ -326,15 +326,27 @@ public static bool WriteHash(string Hash, string Data, string Collection) { foreach (var oSubSub in oSub.Values()) { - if (oSubSub.ToString() != sID) + try { - string sDir = "wwwroot\\" + "_Key" + "\\" + oSub.Name.ToLower().TrimStart('#'); - if (!Directory.Exists(sDir)) - Directory.CreateDirectory(sDir); + if (oSubSub.ToString() != sID) + { + string sDir = "wwwroot\\" + "_Key" + "\\" + oSub.Name.ToLower().TrimStart('#'); - File.WriteAllText(sDir + "\\" + oSubSub.ToString() + ".json", sID); + //Remove invalid Characters in Path + foreach (var sChar in Path.GetInvalidPathChars()) + { + sDir = sDir.Replace(sChar.ToString(), ""); + } + + if (!Directory.Exists(sDir)) + Directory.CreateDirectory(sDir); + + File.WriteAllText(sDir + "\\" + oSubSub.ToString() + ".json", sID); + } } + catch { } } + } else { @@ -342,11 +354,15 @@ public static bool WriteHash(string Hash, string Data, string Collection) { if (oSub.Value.ToString() != sID) { - string sDir = "wwwroot\\" + "_Key" + "\\" + oSub.Name.ToLower().TrimStart('#'); - if (!Directory.Exists(sDir)) - Directory.CreateDirectory(sDir); + try + { + string sDir = "wwwroot\\" + "_Key" + "\\" + oSub.Name.ToLower().TrimStart('#'); + if (!Directory.Exists(sDir)) + Directory.CreateDirectory(sDir); - File.WriteAllText(sDir + "\\" + (string)oSub.Value + ".json", sID); + File.WriteAllText(sDir + "\\" + (string)oSub.Value + ".json", sID); + } + catch { } } } } @@ -440,7 +456,14 @@ public static string ReadHash(string Hash, string Collection) if (UseFileStore) { - sResult = File.ReadAllText("wwwroot\\" + Collection + "\\" + Hash + ".json"); + string Coll2 = Collection; + //Remove invalid Characters in Path + foreach (var sChar in Path.GetInvalidPathChars()) + { + Coll2 = Coll2.Replace(sChar.ToString(), ""); + } + + sResult = File.ReadAllText("wwwroot\\" + Coll2 + "\\" + Hash + ".json"); //Cache result in Memory if (!string.IsNullOrEmpty(sResult)) @@ -1111,10 +1134,17 @@ public static List search(string searchkey, string KeyID = "#id") return lNames.Union(lNames).ToList(); } - public static JArray query(string paths, string select) + public static JArray query(string paths, string select, string exclude) { paths = System.Net.WebUtility.UrlDecode(paths); select = System.Net.WebUtility.UrlDecode(select); + exclude = System.Net.WebUtility.UrlDecode(exclude); + List lExclude = new List(); + + if(!string.IsNullOrEmpty(exclude)) + { + lExclude = exclude.Split(";").ToList(); + } if (string.IsNullOrEmpty(select)) select = "#id"; //,#Name,_inventoryDate @@ -1131,66 +1161,84 @@ public static JArray query(string paths, string select) var jObj = GetFull(sHash); JObject oRes = new JObject(); - foreach (string sAttrib in select.Split(',')) + foreach (string sAttrib in select.Split(';')) { //var jVal = jObj[sAttrib]; var jVal = jObj.SelectToken(sAttrib); if (jVal != null) + { oRes.Add(sAttrib.Trim(), jVal); + } } - foreach (string path in paths.Split(';')) + if (!string.IsNullOrEmpty(paths)) //only return defined objects, if empty all object will return { - try + foreach (string path in paths.Split(';')) { - var oToks = jObj.SelectTokens(path.Trim(), false); - - if (oToks.Count() == 0) + try { - oRes = new JObject(); //remove selected attributes as we do not have any vresults from jsonpath - continue; - } + var oToks = jObj.SelectTokens(path.Trim(), false); - foreach (JToken oTok in oToks) - { - try + if (oToks.Count() == 0) + { + oRes = new JObject(); //remove selected attributes as we do not have any vresults from jsonpath + continue; + } + + foreach (JToken oTok in oToks) { - if (oTok.Type == JTokenType.Object) + try { - oRes.Merge(oTok); - //oRes.Add(jObj[select.Split(',')[0]].ToString(), oTok); - continue; + if (oTok.Type == JTokenType.Object) + { + oRes.Merge(oTok); + //oRes.Add(jObj[select.Split(',')[0]].ToString(), oTok); + continue; + } + if (oTok.Type == JTokenType.Array) + { + oRes.Add(new JProperty(path, oTok)); + } + if (oTok.Type == JTokenType.Property) + oRes.Add(oTok.Parent); + + if (oTok.Type == JTokenType.String) + oRes.Add(oTok.Path, oTok.ToString()); + + if (oTok.Type == JTokenType.Date) + oRes.Add(oTok.Parent); } - if (oTok.Type == JTokenType.Array) + catch (Exception ex) { - oRes.Add(new JProperty(path, oTok)); + Debug.WriteLine("Error Query_5: " + ex.Message.ToString()); } - if (oTok.Type == JTokenType.Property) - oRes.Add(oTok.Parent); - - if (oTok.Type == JTokenType.String) - oRes.Add(oTok.Path, oTok.ToString()); - if (oTok.Type == JTokenType.Date) - oRes.Add(oTok.Parent); - } - catch(Exception ex) - { - Debug.WriteLine("Error Query_5: " + ex.Message.ToString()); } + /*if (oToks.Count() == 0) + oRes = new JObject(); */ + } + catch (Exception ex) + { + Debug.WriteLine("Error Query_5: " + ex.Message.ToString()); } - - /*if (oToks.Count() == 0) - oRes = new JObject(); */ - } - catch(Exception ex) - { - Debug.WriteLine("Error Query_5: " + ex.Message.ToString()); } } + + + if (oRes.HasValues) { + //Remove excluded Properties + foreach (string sExclude in lExclude) + { + foreach (var oRem in oRes.SelectTokens(sExclude, false).ToList()) + { + oRem.Parent.Remove(); + //oRes.Remove(oRem.Path); + } + } + aRes.Add(oRes); //lRes.Add(i.ToString(), oRes); //i++; @@ -1206,10 +1254,17 @@ public static JArray query(string paths, string select) } - public static JArray queryAll(string paths, string select) + public static JArray queryAll(string paths, string select, string exclude) { paths = System.Net.WebUtility.UrlDecode(paths); select = System.Net.WebUtility.UrlDecode(select); + exclude = System.Net.WebUtility.UrlDecode(exclude); + List lExclude = new List(); + + if (!string.IsNullOrEmpty(exclude)) + { + lExclude = exclude.Split(";").ToList(); + } if (string.IsNullOrEmpty(select)) select = "#id"; //,#Name,_inventoryDate @@ -1230,40 +1285,44 @@ public static JArray queryAll(string paths, string select) { oRes.Add(sAttrib.Trim(), jObj[sAttrib]); } - foreach (string path in paths.Split(',')) + + if (!string.IsNullOrEmpty(paths)) //only return defined objects, if empty all object will return { - try + foreach (string path in paths.Split(',')) { - var oToks = jObj.SelectTokens(path.Trim(), false); - foreach (JToken oTok in oToks) + try { - if (oTok.Type == JTokenType.Object) - { - oRes.Merge(oTok); - //oRes.Add(jObj[select.Split(',')[0]].ToString(), oTok); - continue; - } - if (oTok.Type == JTokenType.Array) + var oToks = jObj.SelectTokens(path.Trim(), false); + foreach (JToken oTok in oToks) { - oRes.Add(new JProperty(path, oTok)); - } - if (oTok.Type == JTokenType.Property) - oRes.Add(oTok.Parent); + if (oTok.Type == JTokenType.Object) + { + oRes.Merge(oTok); + //oRes.Add(jObj[select.Split(',')[0]].ToString(), oTok); + continue; + } + if (oTok.Type == JTokenType.Array) + { + oRes.Add(new JProperty(path, oTok)); + } + if (oTok.Type == JTokenType.Property) + oRes.Add(oTok.Parent); - if (oTok.Type == JTokenType.String) - oRes.Add(oTok.Parent); + if (oTok.Type == JTokenType.String) + oRes.Add(oTok.Parent); - if (oTok.Type == JTokenType.Date) - oRes.Add(oTok.Parent); + if (oTok.Type == JTokenType.Date) + oRes.Add(oTok.Parent); + } + if (oToks.Count() == 0) + oRes = null; } - if (oToks.Count() == 0) - oRes = null; + catch { } } - catch { } } - if (oRes != null) + if (oRes.HasValues) { string sHa = CalculateHash(oRes.ToString(Formatting.None)); if (!lHashes.Contains(sHa)) @@ -1271,6 +1330,16 @@ public static JArray queryAll(string paths, string select) aRes.Add(oRes); lHashes.Add(sHa); } + + //Remove excluded Properties + foreach (string sExclude in lExclude) + { + foreach (var oRem in oRes.SelectTokens(sExclude, false).ToList()) + { + oRem.Parent.Remove(); + //oRes.Remove(oRem.Path); + } + } } } @@ -1284,45 +1353,48 @@ public static JArray queryAll(string paths, string select) JObject jObj = GetRaw(File.ReadAllText(oFile.FullName), paths); JObject oRes = new JObject(); - foreach (string sAttrib in select.Split(',')) + foreach (string sAttrib in select.Split(';')) { oRes.Add(sAttrib.Trim(), jObj[sAttrib]); } - foreach (string path in paths.Split(',')) + if (!string.IsNullOrEmpty(paths)) //only return defined objects, if empty all object will return { - try + foreach (string path in paths.Split(';')) { - var oToks = jObj.SelectTokens(path.Trim(), false); - foreach (JToken oTok in oToks) + try { - if (oTok.Type == JTokenType.Object) + var oToks = jObj.SelectTokens(path.Trim(), false); + foreach (JToken oTok in oToks) { - oRes.Merge(oTok); - //oRes.Add(jObj[select.Split(',')[0]].ToString(), oTok); - continue; - } - if (oTok.Type == JTokenType.Array) - { - oRes.Add(new JProperty(path, oTok)); - } - if (oTok.Type == JTokenType.Property) - oRes.Add(oTok.Parent); + if (oTok.Type == JTokenType.Object) + { + oRes.Merge(oTok); + //oRes.Add(jObj[select.Split(',')[0]].ToString(), oTok); + continue; + } + if (oTok.Type == JTokenType.Array) + { + oRes.Add(new JProperty(path, oTok)); + } + if (oTok.Type == JTokenType.Property) + oRes.Add(oTok.Parent); - if (oTok.Type == JTokenType.String) - oRes.Add(oTok.Parent); + if (oTok.Type == JTokenType.String) + oRes.Add(oTok.Parent); - if (oTok.Type == JTokenType.Date) - oRes.Add(oTok.Parent); + if (oTok.Type == JTokenType.Date) + oRes.Add(oTok.Parent); + } + if (oToks.Count() == 0) + oRes = null; } - if (oToks.Count() == 0) - oRes = null; + catch { } } - catch { } } - if (oRes != null) + if (oRes.HasValues) { string sHa = CalculateHash(oRes.ToString(Formatting.None)); if (!lHashes.Contains(sHa)) @@ -1330,6 +1402,16 @@ public static JArray queryAll(string paths, string select) aRes.Add(oRes); lHashes.Add(sHa); } + + //Remove excluded Properties + foreach (string sExclude in lExclude) + { + foreach (var oRem in oRes.SelectTokens(sExclude, false).ToList()) + { + oRem.Parent.Remove(); + //oRes.Remove(oRem.Path); + } + } } } diff --git a/source/jaindb/jaindb.csproj b/source/jaindb/jaindb.csproj index 19a9768..af4ff1c 100644 --- a/source/jaindb/jaindb.csproj +++ b/source/jaindb/jaindb.csproj @@ -13,8 +13,8 @@ https://github.com/rzander/jaindb blockchain json 0.9.0 - 0.9.0.19 - 0.9.0.19 + 0.9.0.21 + 0.9.0.21 jaindb.Program fcfd6c0a-e53c-46cb-8a9d-b786c0579861 diff --git a/source/jaindb/wwwroot/inventory.ps1 b/source/jaindb/wwwroot/inventory.ps1 index d710713..e967bab 100644 --- a/source/jaindb/wwwroot/inventory.ps1 +++ b/source/jaindb/wwwroot/inventory.ps1 @@ -200,8 +200,8 @@ $object | Add-Member -MemberType NoteProperty -Name "LocalAdmins" -Value ($locAd $locGroup = Get-LocalGroup | Select-Object Description, Name, PrincipalSource, ObjectClass | Sort-Object -Property Name $object | Add-Member -MemberType NoteProperty -Name "LocalGroups" -Value ($locGroup) -$FWRules = Get-NetFirewallRule | Select-Object DisplayName,Description,DisplayGroup,Group,Enabled,Profile,Platform,Direction,Action,EdgeTraversalPolicy,LooseSourceMapping,LocalOnlyMapping,Owner,PrimaryStatus,Status,EnforcementStatus,PolicyStoreSource,PolicyStoreSourceType | Sort-Object -Property DisplayName -$object | Add-Member -MemberType NoteProperty -Name "FirewallRules" -Value ($FWRules) +#$FWRules = Get-NetFirewallRule | Select-Object DisplayName,Description,DisplayGroup,Group,Enabled,Profile,Platform,Direction,Action,EdgeTraversalPolicy,LooseSourceMapping,LocalOnlyMapping,Owner,PrimaryStatus,Status,EnforcementStatus,PolicyStoreSource,PolicyStoreSourceType | Sort-Object -Property DisplayName +#$object | Add-Member -MemberType NoteProperty -Name "FirewallRules" -Value ($FWRules) #Windows Universal Apps #$Appx = Get-AppxPackage | Select-Object Name, Publisher, Architecture, Version, PackageFullName, IsFramework, PackageFamilyName, PublisherId, IsResourcePackage, IsBundle, IsDevelopmentMode, IsPartiallyStaged, SignatureKind, Status | Sort-Object -Property Name @@ -231,5 +231,5 @@ $id = $object."#id" $con = $object | ConvertTo-Json -Compress Write-Host "Device ID: $($id)" -Invoke-RestMethod -Uri "%LocalURL%/upload/$($id)" -Method Post -Body $con -ContentType "application/json; charset=utf-8" +Invoke-RestMethod -Uri "%LocalURL%:%WebPort%/upload/$($id)" -Method Post -Body $con -ContentType "application/json; charset=utf-8"