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;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
14
16{
21 {
26 : base("Storage/SavePrivateXml",
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(SavePrivateXml).Namespace + ".JSON.SavePrivateXml.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SavePrivateXml).Namespace + ".XML.SavePrivateXml.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
46
47 XmlDocument Xml = (XmlDocument)Parameters["PXml"].AssociatedObjectValue;
48
49 if (Xml.DocumentElement is null)
50 throw new BadRequestException("XML is empty.");
51
52 string XmlStr = Xml.DocumentElement.OuterXml;
53
54 PersistedElement Element = await Database.FindFirstDeleteRest<PersistedElement>(new FilterAnd(
55 new FilterFieldEqualTo("Account", User.UserName),
56 new FilterFieldEqualTo("Namespace", Xml.DocumentElement.NamespaceURI),
57 new FilterFieldEqualTo("LocalName", Xml.DocumentElement.LocalName)));
58
59 if (Element is null)
60 {
61 DateTime TP = DateTime.UtcNow;
62
64 {
65 Account = User.UserName,
66 Created = TP,
67 Updated = TP,
68 LocalName = Xml.DocumentElement.LocalName,
69 Namespace = Xml.DocumentElement.NamespaceURI,
70 Xml = XmlStr
71 };
72
73 await Database.Insert(Element);
74 }
75 else if (Element.Xml != XmlStr)
76 {
77 Element.Xml = XmlStr;
78 Element.Updated = DateTime.UtcNow;
79
80 await Database.Update(Element);
81 }
82
83 await Response.Return(new NamedDictionary<string, object>("Stored", AgentNamespace)
84 {
85 { "created", Element.Created },
86 { "updated", Element.Updated }
87 });
88 }
89 }
90}
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
Task Return(Exception ex)
Returns an error to the client.
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:21
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
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.
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
Base class for all types of elements.
Definition: Element.cs:14
Class managing a script expression.
Definition: Expression.cs:41
Contains information about a broker account.
Definition: Account.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
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.