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.Xml;
3using System.Threading.Tasks;
6using Waher.Script;
8using System.Text;
10
12{
17 {
22 : base("/ProposeContract")
23 {
24 }
25
29 public override bool HandlesSubPaths => false;
30
34 public override bool UserSessions => true;
35
39 public bool AllowsPOST => true;
40
47 public async Task POST(HttpRequest Request, HttpResponse Response)
48 {
49 string Xml = null;
50
51 try
52 {
53 Gateway.AssertUserAuthenticated(Request, "Admin.Legal.ProposeContract");
54
55 if (Gateway.ContractsClient is null)
56 throw new NotSupportedException("Proposing new contracts not permitted. Broker does not support smart contracts.");
57
58 if (!Request.HasData)
59 throw new BadRequestException("No data in post.");
60
61 Variables PageVariables = Page.GetPageVariables(Request.Session, "/ProposeContract.md");
62 object Posted = await Request.DecodeDataAsync();
63
64 if (Posted is XmlDocument Doc)
65 {
67 ?? throw new BadRequestException("Unable to parse contract.");
68
70 throw new ForbiddenException("Contract must not have a status section.");
71
73 throw new BadRequestException("Contract parameter values not valid.");
74
75 StringBuilder sb = new StringBuilder();
76
78
79 Doc = new XmlDocument()
80 {
81 PreserveWhitespace = true
82 };
83 Doc.LoadXml(Xml = sb.ToString());
84
85 ParsedContract.Contract.ForMachines = Doc.DocumentElement;
86
87 PageVariables["Contract"] = ParsedContract.Contract;
88 }
89 else if (Posted is bool Command)
90 {
91 if (!PageVariables.TryGetVariable("Contract", out Variable v) ||
92 !(v.ValueObject is Contract Contract))
93 {
94 throw new BadRequestException("No smart contract uploaded.");
95 }
96
97 if (Command)
98 {
102
103 PageVariables["Contract"] = Contract;
104 }
105 else
106 PageVariables.Remove("Contract");
107 }
108 else
109 throw new UnsupportedMediaTypeException("Invalid type of posted data.");
110 }
111 catch (XmlException ex)
112 {
113 await Response.SendResponse(XML.AnnotateException(ex, Xml));
114 }
115 catch (Exception ex)
116 {
117 await Response.SendResponse(ex);
118 }
119 }
120
121 }
122}
Helps with common XML-related tasks.
Definition: XML.cs:19
static XmlException AnnotateException(XmlException ex)
Creates a new XML Exception object, with reference to the source XML file, for information.
Definition: XML.cs:1588
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
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:3041
static ContractsClient ContractsClient
XMPP Contracts Client, if such a compoent is available on the XMPP broker.
Definition: Gateway.cs:4375
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:18
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
Variables Session
Contains session states, if the resource requires sessions, or null otherwise.
Definition: HttpRequest.cs:164
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
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:67
Decoded data posted to the resource
Definition: Posted.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:397
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:1301
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:52
virtual bool Remove(string VariableName)
Removes a varaiable from the collection.
Definition: Variables.cs:147
POST Interface for HTTP resources.
ContractParts
How the parts of the contract are defined.
Definition: Part.cs:9