Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ProposeContract.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
10using Waher.Script;
11
13{
18 {
23 : base("/ProposeContract")
24 {
25 }
26
30 public override bool HandlesSubPaths => false;
31
35 public override bool UserSessions => true;
36
40 public bool AllowsPOST => true;
41
49 {
50 string Xml = null;
51
52 try
53 {
54 Gateway.AssertUserAuthenticated(Request, "Admin.Legal.ProposeContract");
55
56 if (Gateway.ContractsClient is null)
57 {
58 await Response.SendResponse(new NotSupportedException("Proposing new contracts not permitted. Broker does not support smart contracts."));
59 return;
60 }
61
62 if (!Request.HasData)
63 {
64 await Response.SendResponse(new BadRequestException("No data in post."));
65 return;
66 }
67
68 Variables PageVariables = Page.GetPageVariables(Request.Session, "/ProposeContract.md");
69 ContentResponse Posted = await Request.DecodeDataAsync();
70
71 if (Posted.HasError)
72 {
73 await Response.SendResponse(Posted.Error);
74 return;
75 }
76
77 if (Posted.Decoded is XmlDocument Doc)
78 {
80 if (ParsedContract?.Contract is null)
81 {
82 await Response.SendResponse(new BadRequestException("Unable to parse contract."));
83 return;
84 }
85
87 {
88 await Response.SendResponse(new ForbiddenException(Request, "Contract must not have a status section."));
89 return;
90 }
91
93 {
94 await Response.SendResponse(new BadRequestException("Contract parameter values not valid."));
95 return;
96 }
97
98 StringBuilder sb = new StringBuilder();
99
101
102 Doc = XML.ParseXml(Xml = sb.ToString(), true);
103
104 ParsedContract.Contract.ForMachines = Doc.DocumentElement;
105
106 PageVariables["Contract"] = ParsedContract.Contract;
107 }
108 else if (Posted.Decoded is bool Command)
109 {
110 if (!PageVariables.TryGetVariable("Contract", out Variable v) ||
111 !(v.ValueObject is Contract Contract))
112 {
113 await Response.SendResponse(new BadRequestException("No smart contract uploaded."));
114 return;
115 }
116
117 if (Command)
118 {
122
123 PageVariables["Contract"] = Contract;
124 }
125 else
126 PageVariables.Remove("Contract");
127 }
128 else
129 {
130 await Response.SendResponse(new UnsupportedMediaTypeException("Invalid type of posted data."));
131 return;
132 }
133 }
134 catch (XmlException ex)
135 {
136 await Response.SendResponse(XML.AnnotateException(ex, Xml));
137 }
138 catch (Exception ex)
139 {
140 await Response.SendResponse(ex);
141 }
142 }
143
144 }
145}
Contains information about a response to a content request.
Helps with common XML-related tasks.
Definition: XML.cs:21
static XmlException AnnotateException(XmlException ex)
Creates a new XML Exception object, with reference to the source XML file, for information.
Definition: XML.cs:1762
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 IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3868
static ContractsClient ContractsClient
XMPP Contracts Client, if such a compoent is available on the XMPP broker.
Definition: Gateway.cs:5299
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
ProposeContract()
Proposes a new smart contract
override bool UserSessions
If the resource uses user sessions.
bool AllowsPOST
If the POST method is allowed.
override bool HandlesSubPaths
If the resource handles sub-paths.
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:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
static Variables GetPageVariables(HttpRequest Request)
Gets the variable collection for the current page.
Definition: Page.cs:73
Decoded data posted to the resource
Definition: Posted.cs:12
Access to the current request.
Definition: Request.cs:12
Access to the current response.
Definition: Response.cs:12
The server is refusing to service the request because the entity of the request is in a format not su...
Contains the definition of a contract
Definition: Contract.cs:22
static Task< ParsedContract > Parse(XmlDocument Xml)
Validates a contract XML Document, and returns the contract definition in it.
Definition: Contract.cs:441
Parameter[] Parameters
Defined parameters for the smart contract.
Definition: Contract.cs:267
Duration? ArchiveRequired
Requied time to archive a signed smart contract, after it becomes obsolete.
Definition: Contract.cs:202
HumanReadableText[] ForHumans
Human-readable contents of the contract.
Definition: Contract.cs:300
Duration? ArchiveOptional
Optional time to archive a signed smart contract, after it becomes obsolete, and after its required a...
Definition: Contract.cs:211
bool CanActAsTemplate
If the contract can act as a template for other contracts.
Definition: Contract.cs:336
XmlElement ForMachines
Machine-readable contents of the contract.
Definition: Contract.cs:276
Role[] Roles
Roles defined in the smart contract.
Definition: Contract.cs:240
DateTime? SignAfter
Signatures will only be accepted after this point in time.
Definition: Contract.cs:166
ContractParts PartsMode
How parts are defined in the smart contract.
Definition: Contract.cs:249
static string NormalizeXml(string Xml)
Normalizes XML.
Definition: Contract.cs:1390
Part[] Parts
Defined parts for the smart contract.
Definition: Contract.cs:258
Duration? Duration
Duration of the contract. Is counted from the time it is signed by the required parties.
Definition: Contract.cs:193
DateTime? SignBefore
Signatures will only be accepted until this point in time.
Definition: Contract.cs:175
string Namespace
Namespace used when serializing the identity for signatures.
Definition: Contract.cs:102
ContractVisibility Visibility
Contrat Visibility
Definition: Contract.cs:184
Task< Contract > CreateContractAsync(XmlElement ForMachines, HumanReadableText[] ForHumans, Role[] Roles, Part[] Parts, Parameter[] Parameters, ContractVisibility Visibility, ContractParts PartsMode, Duration? Duration, Duration? ArchiveRequired, Duration? ArchiveOptional, DateTime? SignAfter, DateTime? SignBefore, bool CanActAsTemplate)
Creates a new contract.
Contains information about a parsed contract.
bool ParametersValid
If parameter values in the contract are valid.
bool HasStatus
If a status element was found.
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Definition: Variables.cs:56
virtual bool Remove(string VariableName)
Removes a varaiable from the collection.
Definition: Variables.cs:151
POST Interface for HTTP resources.
ContractParts
How the parts of the contract are defined.
Definition: Part.cs:9