Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AddTextNote.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
12using Waher.Script;
18
20{
25 {
29 public AddTextNote()
30 : base("Tokens/AddTextNote",
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(AddTextNote).Namespace + ".JSON.AddTextNote.req");
37 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(AddTextNote).Namespace + ".XML.AddTextNote.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 string Note = (string)Parameters["PNote"].AssociatedObjectValue;
52 bool Personal = (bool)Parameters["PPersonal"].AssociatedObjectValue;
53 string BareJid = User.UserName + "@" + Gateway.Domain;
54
55 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true)
56 ?? throw new NotFoundException("Token not found.");
57
58 TokenNoteEvent Event;
59
60 if (Token.OwnerJid == BareJid)
61 {
62 Event = new NoteText()
63 {
64 ArchiveOptional = Token.ArchiveOptional,
65 ArchiveRequired = Token.ArchiveRequired,
66 Expires = Token.Expires,
67 Note = Note,
68 Personal = Personal,
69 Timestamp = DateTime.UtcNow,
70 TokenId = Token.TokenId
71 };
72 }
73 else
74 {
75 await AssertApprovedExternalSource(Request, Token, BareJid);
76
77 Event = new ExternalNoteText()
78 {
79 ArchiveOptional = Token.ArchiveOptional,
80 ArchiveRequired = Token.ArchiveRequired,
81 Expires = Token.Expires,
82 Note = Note,
83 Personal = Personal,
84 Timestamp = DateTime.UtcNow,
85 TokenId = Token.TokenId,
86 Source = BareJid
87 };
88 }
89
90 await Database.Insert(Event);
91 await StateMachineProcessor.EventGenerated(Token, Event);
92
93 StringBuilder Xml = new StringBuilder();
94 Event.Serialize(Xml);
95
96 XmlDocument Doc = XML.ParseXml(Xml.ToString(), true);
97
98 await Response.Return(new NamedDictionary<string, object>("NoteResult", AgentNamespace)
99 {
100 { "Note", Doc }
101 });
102 }
103
104 internal static async Task AssertApprovedExternalSource(HttpRequest Request, Token Token, string FromBareJid)
105 {
106 if (Token.IsExternalPart(FromBareJid))
107 return;
108
110 {
111 StateMachineProcessor.CacheRecord Machine = await StateMachineProcessor.GetStateMachine(Token, false)
112 ?? throw new NotFoundException("State-machine not found.");
113
114 if (Machine.CurrentState is null)
115 throw new ServiceUnavailableException("Current state of State-machine not available.");
116
117 if (Machine.CurrentState.HasEnded)
118 throw new ServiceUnavailableException("State-machine has ended.");
119
120 if (!Machine.CurrentState.ContainsSource(FromBareJid))
121 throw new ForbiddenException(Request, "You are not an approved external part for this token.");
122 }
123 else
124 throw new ForbiddenException(Request, "You are not an approved external part for this token.");
125 }
126
127 }
128}
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 class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:3087
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
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...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
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
A text note logged on the token from an external source.
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.
bool HasStateMachine
If the token has an associated state-machine.
Definition: Token.cs:1345
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
bool IsExternalPart(CaseInsensitiveString BareJid)
Checks if a JID is an external part in the creation of the contract.
Definition: Token.cs:1321
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: AddTextNote.cs:47