Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetContractTokens.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
16
18{
23 {
28 : base("Tokens/GetContractTokens",
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(GetContractTokens).Namespace + ".JSON.GetContractTokens.req");
35 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetContractTokens).Namespace + ".XML.GetContractTokens.req");
36
45 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
46 {
48
49 CaseInsensitiveString ContractId = (string)Parameters["PContractId"].AssociatedObjectValue;
50 int Offset = 0;
51 int MaxCount = int.MaxValue;
52 bool References = true;
53
54 if (Parameters.TryGetValue("POffset", out IElement E) && !(E is null) &&
55 E.AssociatedObjectValue is double d)
56 {
57 Offset = (int)d;
58 }
59
60 if (Parameters.TryGetValue("PMaxCount", out E) && !(E is null) &&
61 E.AssociatedObjectValue is double d2)
62 {
63 MaxCount = (int)d2;
64 }
65
66 if (Parameters.TryGetValue("PReferences", out E) && !(E is null) &&
67 E.AssociatedObjectValue is bool b)
68 {
69 References = b;
70 }
71
72 XmppAddress ContractAddr = new XmppAddress(ContractId);
73 if (!XmppServerModule.Legal.IsComponentDomain(ContractAddr.Domain, true))
74 throw new BadRequestException("Contract not hosted on this server.");
75
76 Contract Contract = await XmppServerModule.Legal.GetContract(ContractId)
77 ?? throw new NotFoundException("Contract not found.");
78
79 string BareJid = User.UserName + "@" + Gateway.Domain;
80
81 if (!await Contract.CanRead(new XmppAddress(BareJid), XmppServerModule.Server, XmppServerModule.Legal))
82 throw new ForbiddenException("Access to contract denied.");
83
84 string Xml;
85 IEnumerable<Token> Tokens = await Database.Find<Token>(Offset, MaxCount,
86 new FilterFieldEqualTo("CreationContract", ContractId), "-Created");
87
88 if (References)
89 {
90 List<TokenReference> TokenReferences = new List<TokenReference>();
91
92 foreach (Token Token in Tokens)
93 {
94 TokenReferences.Add(new TokenReference()
95 {
96 TokenId = Token.TokenId
97 });
98 }
99
100 Xml = NeuroFeaturesProcessor.SerializeTokenReferences(TokenReferences);
101 }
102 else
103 Xml = NeuroFeaturesProcessor.SerializeTokenReferences(Tokens);
104
105 XmlDocument Doc = new XmlDocument()
106 {
107 PreserveWhitespace = true
108 };
109 Doc.LoadXml(Xml);
110
111 await Response.Return(new NamedDictionary<string, object>("TokensResult", AgentNamespace)
112 {
113 { "Tokens", Doc }
114 });
115 }
116 }
117}
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 request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
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...
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
CaseInsensitiveString Domain
Domain
Definition: XmppAddress.cs:97
Represents a case-insensitive string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:247
This filter selects objects that have a named field equal to a given value.
Class managing a script expression.
Definition: Expression.cs:39
Marketplace processor, brokering sales of items via tenders and offers defined in smart contracts.
Contains a reference to a token, possibly on another neuron.
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
Gets tokens on the Neuron(R) created by a specific contract.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
GetContractTokens()
Gets tokens on the Neuron(R) created by a specific contract.
Service Module hosting the XMPP broker and its components.
Basic interface for all types of elements.
Definition: IElement.cs:20