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;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
12using Waher.Script;
19
21{
26 {
30 public AddXmlNote()
31 : base("Tokens/AddXmlNote",
32 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
33 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
34 {
35 }
36
37 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(AddXmlNote).Namespace + ".JSON.AddXmlNote.req");
38 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(AddXmlNote).Namespace + ".XML.AddXmlNote.req");
39
48 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
49 {
51 string TokenId = (string)Parameters["PTokenId"].AssociatedObjectValue;
52 bool Personal = (bool)Parameters["PPersonal"].AssociatedObjectValue;
53 string BareJid = User.UserName + "@" + Gateway.Domain;
54
55 object Obj = Parameters["PNote"].AssociatedObjectValue;
56 string LocalName;
57 string Namespace;
58 XmlDocument Doc;
59
60 if (Obj is string Xml)
61 {
62 if (!XML.IsValidXml(Xml))
63 throw new BadRequestException("Invalid XML note.");
64
65 Doc = XML.ParseXml(Xml, true);
66
67 (string, Dictionary<string, ValidationSchema>) P = await XmppServerModule.Legal.ValidateContent(Doc, null);
68 if (!string.IsNullOrEmpty(P.Item1))
69 throw new BadRequestException(P.Item1);
70
71 LocalName = Doc.DocumentElement.LocalName;
72 Namespace = Doc.DocumentElement.NamespaceURI;
73 }
74 else if (Obj is XmlDocument Doc2)
75 {
76 Xml = Doc2.DocumentElement.OuterXml;
77 LocalName = Doc2.DocumentElement.LocalName;
78 Namespace = Doc2.DocumentElement.NamespaceURI;
79
80 (string, Dictionary<string, ValidationSchema>) P = await XmppServerModule.Legal.ValidateContent(Doc2, null);
81 if (!string.IsNullOrEmpty(P.Item1))
82 throw new BadRequestException(P.Item1);
83 }
84 else if (Obj is XmlElement E)
85 {
86 Xml = E.OuterXml;
87 LocalName = E.LocalName;
88 Namespace = E.NamespaceURI;
89
90 Doc = XML.ParseXml(Xml, true);
91
92 (string, Dictionary<string, ValidationSchema>) P = await XmppServerModule.Legal.ValidateContent(Doc, null);
93 if (!string.IsNullOrEmpty(P.Item1))
94 throw new BadRequestException("XML Note invalid: " + P.Item1);
95 }
96 else
97 throw new BadRequestException("Invalid XML note.");
98
99 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true)
100 ?? throw new NotFoundException("Token not found.");
101
102 TokenNoteEvent Event;
103
104 if (Token.OwnerJid == BareJid)
105 {
106 Event = new NoteXml()
107 {
108 ArchiveOptional = Token.ArchiveOptional,
109 ArchiveRequired = Token.ArchiveRequired,
110 Expires = Token.Expires,
111 Personal = Personal,
112 Timestamp = DateTime.UtcNow,
113 TokenId = Token.TokenId,
114 Note = Xml,
115 LocalName = LocalName,
116 Namespace = Namespace
117 };
118 }
119 else
120 {
121 await AddTextNote.AssertApprovedExternalSource(Request, Token, BareJid);
122
123 Event = new ExternalNoteXml()
124 {
125 ArchiveOptional = Token.ArchiveOptional,
126 ArchiveRequired = Token.ArchiveRequired,
127 Expires = Token.Expires,
128 Personal = Personal,
129 Timestamp = DateTime.UtcNow,
130 TokenId = Token.TokenId,
131 Note = Xml,
132 LocalName = LocalName,
133 Namespace = Namespace,
134 Source = BareJid
135 };
136 }
137
138 await Database.Insert(Event);
139 await StateMachineProcessor.EventGenerated(Token, Event);
140
141 StringBuilder sb = new StringBuilder();
142 Event.Serialize(sb);
143
144 Doc = XML.ParseXml(Xml.ToString(), true);
145
146 await Response.Return(new NamedDictionary<string, object>("NoteResult", AgentNamespace)
147 {
148 { "Note", Doc }
149 });
150 }
151 }
152}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Helps with common XML-related tasks.
Definition: XML.cs:21
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
static bool IsValidXml(string Xml)
Checks if a string is valid XML
Definition: XML.cs:1397
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:3087
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.
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:21
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
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
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:422
CaseInsensitiveString OwnerJid
JID of Current owner of token
Definition: Token.cs:216
Duration? ArchiveRequired
Duration after which token expires, the token is required to be archived.
Definition: Token.cs:412
DateTime Expires
Expiry date of token.
Definition: Token.cs:402
CaseInsensitiveString TokenId
Token ID
Definition: Token.cs:145
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:48
Service Module hosting the XMPP broker and its components.