Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AddXmlNote.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
11using Waher.Script;
18
20{
25 {
29 public AddXmlNote()
30 : base("Tokens/AddXmlNote",
31 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
32 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
33 {
34 }
35
36 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(AddXmlNote).Namespace + ".JSON.AddXmlNote.req");
37 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(AddXmlNote).Namespace + ".XML.AddXmlNote.req");
38
47 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
48 {
50 string TokenId = (string)Parameters["PTokenId"].AssociatedObjectValue;
51 bool Personal = (bool)Parameters["PPersonal"].AssociatedObjectValue;
52 string BareJid = User.UserName + "@" + Gateway.Domain;
53
54 object Obj = Parameters["PNote"].AssociatedObjectValue;
55 string LocalName;
56 string Namespace;
57 XmlDocument Doc;
58
59 if (Obj is string Xml)
60 {
61 if (!XML.IsValidXml(Xml))
62 throw new BadRequestException("Invalid XML note.");
63
64 Doc = new XmlDocument()
65 {
66 PreserveWhitespace = true
67 };
68
69 Doc.LoadXml(Xml);
70
71 (string, Dictionary<string, ValidationSchema>) P = await XmppServerModule.Legal.ValidateContent(Doc);
72 if (!string.IsNullOrEmpty(P.Item1))
73 throw new BadRequestException(P.Item1);
74
75 LocalName = Doc.DocumentElement.LocalName;
76 Namespace = Doc.DocumentElement.NamespaceURI;
77 }
78 else if (Obj is XmlDocument Doc2)
79 {
80 Xml = Doc2.DocumentElement.OuterXml;
81 LocalName = Doc2.DocumentElement.LocalName;
82 Namespace = Doc2.DocumentElement.NamespaceURI;
83
84 (string, Dictionary<string, ValidationSchema>) P = await XmppServerModule.Legal.ValidateContent(Doc2);
85 if (!string.IsNullOrEmpty(P.Item1))
86 throw new BadRequestException(P.Item1);
87 }
88 else if (Obj is XmlElement E)
89 {
90 Xml = E.OuterXml;
91 LocalName = E.LocalName;
92 Namespace = E.NamespaceURI;
93
94 Doc = new XmlDocument()
95 {
96 PreserveWhitespace = true
97 };
98 Doc.LoadXml(Xml);
99
100 (string, Dictionary<string, ValidationSchema>) P = await XmppServerModule.Legal.ValidateContent(Doc);
101 if (!string.IsNullOrEmpty(P.Item1))
102 throw new BadRequestException("XML Note invalid: " + P.Item1);
103 }
104 else
105 throw new BadRequestException("Invalid XML note.");
106
107 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true)
108 ?? throw new NotFoundException("Token not found.");
109
110 TokenNoteEvent Event;
111
112 if (Token.OwnerJid == BareJid)
113 {
114 Event = new NoteXml()
115 {
116 ArchiveOptional = Token.ArchiveOptional,
117 ArchiveRequired = Token.ArchiveRequired,
118 Expires = Token.Expires,
119 Personal = Personal,
120 Timestamp = DateTime.UtcNow,
121 TokenId = Token.TokenId,
122 Note = Xml,
123 LocalName = LocalName,
124 Namespace = Namespace
125 };
126 }
127 else
128 {
129 await AddTextNote.AssertApprovedExternalSource(Token, BareJid);
130
131 Event = new ExternalNoteXml()
132 {
133 ArchiveOptional = Token.ArchiveOptional,
134 ArchiveRequired = Token.ArchiveRequired,
135 Expires = Token.Expires,
136 Personal = Personal,
137 Timestamp = DateTime.UtcNow,
138 TokenId = Token.TokenId,
139 Note = Xml,
140 LocalName = LocalName,
141 Namespace = Namespace,
142 Source = BareJid
143 };
144 }
145
146 await Database.Insert(Event);
147 await StateMachineProcessor.EventGenerated(Token, Event);
148
149 StringBuilder sb = new StringBuilder();
150 Event.Serialize(sb);
151
152 Doc = new XmlDocument()
153 {
154 PreserveWhitespace = true
155 };
156 Doc.LoadXml(Xml.ToString());
157
158 await Response.Return(new NamedDictionary<string, object>("NoteResult", AgentNamespace)
159 {
160 { "Note", Doc }
161 });
162 }
163 }
164}
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
Helps with common XML-related tasks.
Definition: XML.cs:19
static bool IsValidXml(string Xml)
Checks if a string is valid XML
Definition: XML.cs:1223
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:2354
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,...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:95
Class managing a script expression.
Definition: Expression.cs:39
An xml note logged on the token from an external source.
An xml note logged on the token.
Definition: NoteXml.cs:9
void Serialize(StringBuilder Xml)
Serializes the event to XML.
Definition: TokenEvent.cs:111
Abstract base class for token events containing notes made by the owner.
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
Duration? ArchiveOptional
Duration after which token expires, and the required archiving time, the token can optionally be arch...
Definition: Token.cs:382
CaseInsensitiveString OwnerJid
JID of Current owner of token
Definition: Token.cs:203
Duration? ArchiveRequired
Duration after which token expires, the token is required to be archived.
Definition: Token.cs:372
DateTime Expires
Expiry date of token.
Definition: Token.cs:362
CaseInsensitiveString TokenId
Token ID
Definition: Token.cs:139
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.
Definition: AddXmlNote.cs:47
Service Module hosting the XMPP broker and its components.