Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ContractSchemasGetter.cs
1using System;
3using System.Security.Cryptography.X509Certificates;
4using System.Threading.Tasks;
5using Waher.Content;
11
13{
18 {
23 {
24 }
25
29 public string[] UriSchemes => new string[] { "urn" };
30
37 public bool CanGet(Uri Uri, out Grade Grade)
38 {
39 if (string.IsNullOrEmpty(UriToResource(Uri)))
40 {
41 Grade = Grade.NotAtAll;
42 return false;
43 }
44 else
45 {
46 Grade = Grade.Excellent;
47 return true;
48 }
49 }
50
51 private static string UriToResource(Uri Uri)
52 {
53 string SchemaName;
54
55 switch (Uri.OriginalString)
56 {
57 case "urn:nf:iot:e2e:1.0":
58 SchemaName = "E2E.xsd";
59 break;
60
61 case "urn:nf:iot:leg:id:1.0":
62 SchemaName = "LegalIdentities.xsd";
63 break;
64
65 case "urn:nf:iot:p2p:1.0":
66 SchemaName = "P2P.xsd";
67 break;
68
69 case "urn:nf:iot:leg:sc:1.0":
70 SchemaName = "SmartContracts.xsd";
71 break;
72
73 default:
74 return null;
75 }
76
77 return typeof(ContractsClient).Namespace + ".Schema." + SchemaName;
78 }
79
88 public Task<ContentResponse> GetAsync(Uri Uri, X509Certificate Certificate, EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
89 params KeyValuePair<string, string>[] Headers)
90 {
91 string SchemaName = UriToResource(Uri);
92 if (string.IsNullOrEmpty(SchemaName))
93 return Task.FromResult(new ContentResponse(new Exception("URI not recognized.")));
94
95 return Task.FromResult(new ContentResponse(XmlCodec.SchemaContentType, XSL.LoadSchema(SchemaName), Array.Empty<byte>()));
96 }
97
107 public Task<ContentResponse> GetAsync(Uri Uri, X509Certificate Certificate, EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
108 int TimeoutMs, params KeyValuePair<string, string>[] Headers)
109 {
110 return this.GetAsync(Uri, Certificate, RemoteCertificateValidator, Headers);
111 }
112
121 public Task<ContentStreamResponse> GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
122 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator, params KeyValuePair<string, string>[] Headers)
123 {
124 return this.GetTempStreamAsync(Uri, Certificate, RemoteCertificateValidator, InternetContent.DefaultTimeout, Headers);
125 }
126
136 public Task<ContentStreamResponse> GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
137 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator, TemporaryStream Destination, params KeyValuePair<string, string>[] Headers)
138 {
139 return this.GetTempStreamAsync(Uri, Certificate, RemoteCertificateValidator, InternetContent.DefaultTimeout, Destination, Headers);
140 }
141
151 public Task<ContentStreamResponse> GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
152 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator, int TimeoutMs, params KeyValuePair<string, string>[] Headers)
153 {
154 return this.GetTempStreamAsync(Uri, Certificate, RemoteCertificateValidator, TimeoutMs, null, Headers);
155 }
156
167 public async Task<ContentStreamResponse> GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
168 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator, int TimeoutMs, TemporaryStream Destination,
169 params KeyValuePair<string, string>[] Headers)
170 {
171 string SchemaName = UriToResource(Uri);
172 if (string.IsNullOrEmpty(SchemaName))
173 return new ContentStreamResponse(new Exception("URI not recognized."));
174
175 byte[] Bin = Resources.LoadResource(SchemaName);
176
177 Destination ??= new TemporaryStream();
178 await Destination.WriteAsync(Bin, 0, Bin.Length);
179
180 return new ContentStreamResponse(XmlCodec.DefaultContentType, Destination);
181 }
182
189 public bool CanHead(Uri Uri, out Grade Grade)
190 {
191 Grade = Grade.NotAtAll;
192 return false;
193 }
194
203 public Task<ContentResponse> HeadAsync(Uri Uri, X509Certificate Certificate, EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
204 params KeyValuePair<string, string>[] Headers)
205 {
206 return this.GetAsync(Uri, Certificate, RemoteCertificateValidator, Headers);
207 }
208
218 public Task<ContentResponse> HeadAsync(Uri Uri, X509Certificate Certificate, EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
219 int TimeoutMs, params KeyValuePair<string, string>[] Headers)
220 {
221 return this.GetAsync(Uri, Certificate, RemoteCertificateValidator, TimeoutMs, Headers);
222 }
223 }
224}
Contains information about a response to a content request.
Contains information about a stream response to a content request.
Static class managing encoding and decoding of internet content.
static int DefaultTimeout
Default timeout of internet access methods, in milliseconds.
XML encoder/decoder.
Definition: XmlCodec.cs:19
const string DefaultContentType
Default content type for XML documents.
Definition: XmlCodec.cs:30
const string SchemaContentType
Default content type for XML schema documents.
Definition: XmlCodec.cs:35
Static class managing loading of XSL resources stored as embedded resources or in content files.
Definition: XSL.cs:16
static XmlSchema LoadSchema(string ResourceName)
Loads an XML schema from an embedded resource.
Definition: XSL.cs:24
bool CanGet(Uri Uri, out Grade Grade)
If the getter is able to get a resource, given its URI.
Task< ContentResponse > HeadAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets the headers of a resource, using a Uniform Resource Identifier (or Locator).
Task< ContentResponse > HeadAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets the headers of a resource, using a Uniform Resource Identifier (or Locator).
async Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, int TimeoutMs, TemporaryStream Destination, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
Task< ContentResponse > GetAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
bool CanHead(Uri Uri, out Grade Grade)
If the getter is able to get headers of a resource, given its URI.
ContractSchemasGetter()
Getter for well-known contract schemas.
Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
Task< ContentResponse > GetAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, TemporaryStream Destination, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static byte[] LoadResource(string ResourceName)
Loads a resource from an embedded resource.
Definition: Resources.cs:20
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within...
Basic interface for Internet Content getters. A class implementing this interface and having a defaul...
Basic interface for Internet Content headers. A class implementing this interface and having a defaul...
Grade
Grade enumeration
Definition: Grade.cs:7