Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SavePrivateXml.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
10using Waher.Script;
13
15{
20 {
25 : base("Storage/SavePrivateXml",
26 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
27 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
28 {
29 }
30
31 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(SavePrivateXml).Namespace + ".JSON.SavePrivateXml.req");
32 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SavePrivateXml).Namespace + ".XML.SavePrivateXml.req");
33
42 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
43 {
45
46 XmlDocument Xml = (XmlDocument)Parameters["PXml"].AssociatedObjectValue;
47
48 if (Xml.DocumentElement is null)
49 throw new BadRequestException("XML is empty.");
50
51 string XmlStr = Xml.DocumentElement.OuterXml;
52
53 PersistedElement Element = await Database.FindFirstDeleteRest<PersistedElement>(new FilterAnd(
54 new FilterFieldEqualTo("Account", User.UserName),
55 new FilterFieldEqualTo("Namespace", Xml.DocumentElement.NamespaceURI),
56 new FilterFieldEqualTo("LocalName", Xml.DocumentElement.LocalName)));
57
58 if (Element is null)
59 {
60 DateTime TP = DateTime.UtcNow;
61
63 {
64 Account = User.UserName,
65 Created = TP,
66 Updated = TP,
67 LocalName = Xml.DocumentElement.LocalName,
68 Namespace = Xml.DocumentElement.NamespaceURI,
69 Xml = XmlStr
70 };
71
72 await Database.Insert(Element);
73 }
74 else if (Element.Xml != XmlStr)
75 {
76 Element.Xml = XmlStr;
77 Element.Updated = DateTime.UtcNow;
78
79 await Database.Update(Element);
80 }
81
82 await Response.Return(new NamedDictionary<string, object>("Stored", AgentNamespace)
83 {
84 { "created", Element.Created },
85 { "updated", Element.Updated }
86 });
87 }
88 }
89}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:96
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:18
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
Contains information about one persisted XML element.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:95
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
Base class for all types of elements.
Definition: Element.cs:13
Class managing a script expression.
Definition: Expression.cs:39
Contains information about a broker account.
Definition: Account.cs:28
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
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.