Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SearchInVault.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
14
16{
21 {
26 : base("Storage/SearchInVault",
27 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
28 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
29 {
30 }
31
32 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(StoreInVault).Namespace + ".JSON.SearchInVault.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(StoreInVault).Namespace + ".XML.SearchInVault.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
46
47 string Type = (string)Parameters["PType"]?.AssociatedObjectValue;
48 string ClientId = (string)Parameters["PClientId"]?.AssociatedObjectValue;
49 bool Masked = (bool)(Parameters["PMasked"]?.AssociatedObjectValue ?? true);
50 int Offset = (int)(double)(Parameters["POffset"]?.AssociatedObjectValue ?? 0.0);
51 int MaxCount = (int)(double)(Parameters["PMaxCount"]?.AssociatedObjectValue ?? 100.0);
52 object[] TagNames = Parameters.ContainsKey("PTagName") ? (object[])Parameters["PTagName"].AssociatedObjectValue : Array.Empty<object>();
53 object[] TagValues = Parameters.ContainsKey("PTagValue") ? (object[])Parameters["PTagValue"].AssociatedObjectValue : Array.Empty<object>();
55
56 int i, c = TagNames?.Length ?? 0;
57 if ((TagValues?.Length ?? 0) != c)
58 throw new BadRequestException("Invalid Tags.");
59
60 for (i = 0; i < c; i++)
61 {
62 if (!(TagNames[i] is string TagName) || string.IsNullOrEmpty(TagName))
63 throw new BadRequestException("Invalid Tag Name.");
64
65 if (!(TagValues[i] is string TagValue))
66 throw new BadRequestException("Invalid Tag Value.");
67
68 Tags.Add(new KeyValuePair<string, string>(TagName, TagValue));
69 }
70
72 {
73 new FilterFieldEqualTo("Account", User.UserName)
74 };
75
76 if (!(Type is null))
77 Filters.Add(new FilterFieldEqualTo("Type", Type));
78
79 if (!(ClientId is null))
80 Filters.Add(new FilterFieldEqualTo("ClientId", ClientId));
81
82 if (c > 0)
83 {
84 KeyValuePair<string, string>[] TagsFilter = Tags.ToArray();
85
86 Filters.Add(new FilterCustom<VaultItem>((Item) =>
87 {
88 foreach (KeyValuePair<string, string> P in Tags)
89 {
90 bool Found = false;
91 bool Pass = false;
92
93 foreach (VaultTag Tag in Item.Tags)
94 {
95 if (Tag.Name == P.Key)
96 {
97 Found = true;
98 if (P.Value == Tag.Value || P.Value == Tag.MaskedValue)
99 Pass = true;
100 }
101 }
102
103 if (!Found || !Pass)
104 return false;
105 }
106
107 return true;
108 }));
109 }
110
111 IEnumerable<VaultItem> Items;
112
113 if (Filters.Count == 1)
114 Items = await Database.Find<VaultItem>(Offset, MaxCount, Filters[0]);
115 else
116 Items = await Database.Find<VaultItem>(Offset, MaxCount, new FilterAnd(Filters.ToArray()));
117
119
120 foreach (VaultItem Item in Items)
121 Result.Add(GetFromVault.EncodeVaultItem(Item, Masked));
122
123 await Response.Return(new NamedDictionary<string, object>("VaultItems", AgentNamespace)
124 {
125 { "ResultSet", Result.ToArray() }
126 });
127 }
128 }
129}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:238
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
Custom filter used to filter objects using an external expression.
Definition: FilterCustom.cs:10
This filter selects objects that have a named field equal to a given value.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
T[] ToArray()
Returns an array containing all elements of the collection.
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Class managing a script expression.
Definition: Expression.cs:41
Abstract base class for agent resources supporting the POST method.
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
Gets information from the Encrypted Vault.
Definition: GetFromVault.cs:21
Searches for information in the Encrypted Vault.
SearchInVault()
> Searches for information in the Encrypted Vault.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Stores information in the Encrypted Vault.
Definition: StoreInVault.cs:22
Contains information about an item in the Vault.
Definition: VaultItem.cs:16
Contains information about a tag in a vault item.
Definition: VaultTag.cs:7