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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
10using Waher.Script;
16
18{
23 {
27 public AddTextNote()
28 : base("Tokens/AddTextNote",
29 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
30 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
31 {
32 }
33
34 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(AddTextNote).Namespace + ".JSON.AddTextNote.req");
35 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(AddTextNote).Namespace + ".XML.AddTextNote.req");
36
45 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
46 {
48 string TokenId = (string)Parameters["PTokenId"].AssociatedObjectValue;
49 string Note = (string)Parameters["PNote"].AssociatedObjectValue;
50 bool Personal = (bool)Parameters["PPersonal"].AssociatedObjectValue;
51 string BareJid = User.UserName + "@" + Gateway.Domain;
52
53 Token Token = await NeuroFeaturesProcessor.GetToken(TokenId, true)
54 ?? throw new NotFoundException("Token not found.");
55
56 TokenNoteEvent Event;
57
58 if (Token.OwnerJid == BareJid)
59 {
60 Event = new NoteText()
61 {
62 ArchiveOptional = Token.ArchiveOptional,
63 ArchiveRequired = Token.ArchiveRequired,
64 Expires = Token.Expires,
65 Note = Note,
66 Personal = Personal,
67 Timestamp = DateTime.UtcNow,
68 TokenId = Token.TokenId
69 };
70 }
71 else
72 {
73 await AssertApprovedExternalSource(Token, BareJid);
74
75 Event = new ExternalNoteText()
76 {
77 ArchiveOptional = Token.ArchiveOptional,
78 ArchiveRequired = Token.ArchiveRequired,
79 Expires = Token.Expires,
80 Note = Note,
81 Personal = Personal,
82 Timestamp = DateTime.UtcNow,
83 TokenId = Token.TokenId,
84 Source = BareJid
85 };
86 }
87
88 await Database.Insert(Event);
89 await StateMachineProcessor.EventGenerated(Token, Event);
90
91 StringBuilder Xml = new StringBuilder();
92 Event.Serialize(Xml);
93
94 XmlDocument Doc = new XmlDocument()
95 {
96 PreserveWhitespace = true
97 };
98 Doc.LoadXml(Xml.ToString());
99
100 await Response.Return(new NamedDictionary<string, object>("NoteResult", AgentNamespace)
101 {
102 { "Note", Doc }
103 });
104 }
105
106 internal static async Task AssertApprovedExternalSource(Token Token, string FromBareJid)
107 {
108 if (Token.IsExternalPart(FromBareJid))
109 return;
110
112 {
113 StateMachineProcessor.CacheRecord Machine = await StateMachineProcessor.GetStateMachine(Token)
114 ?? throw new NotFoundException("State-machine not found.");
115
116 if (Machine.CurrentState is null)
117 throw new ServiceUnavailableException("Current state of State-machine not available.");
118
119 if (Machine.CurrentState.HasEnded)
120 throw new ServiceUnavailableException("State-machine has ended.");
121
122 if (!Machine.CurrentState.ContainsSource(FromBareJid))
123 throw new ForbiddenException("You are not an approved external part for this token.");
124 }
125 else
126 throw new ForbiddenException("You are not an approved external part for this token.");
127 }
128
129 }
130}
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
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:2354
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
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...
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: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
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:1257
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
bool IsExternalPart(CaseInsensitiveString BareJid)
Checks if a JID is an external part in the creation of the contract.
Definition: Token.cs:1233
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: AddTextNote.cs:45