3using System.Collections.Specialized;
4using System.Security.Cryptography.X509Certificates;
6using System.Threading.Tasks;
22 private static string defaultPurpose =
string.Empty;
24 private readonly Dictionary<string, TaskCompletionSource<LegalIdentityPetitionResponseEventArgs>> identityPetitions =
new Dictionary<string, TaskCompletionSource<LegalIdentityPetitionResponseEventArgs>>();
25 private readonly
object syncObj =
new object();
26 private bool handlerAdded =
false;
40 get => defaultPurpose;
43 if (
string.IsNullOrEmpty(value))
44 throw new ArgumentException(
"Default purpose cannot be empty.", nameof(
DefaultPurpose));
46 defaultPurpose = value;
63 if (
string.Compare(Uri.Scheme,
"iotid",
true) == 0)
82 if (this.handlerAdded)
84 Gateway.ContractsClient.PetitionedIdentityResponseReceived -= this.Client_PetitionedIdentityResponseReceived;
85 this.handlerAdded =
false;
98 public Task<ContentResponse>
GetAsync(Uri Uri, X509Certificate Certificate, EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
99 params KeyValuePair<string, string>[] Headers)
113 public async Task<ContentResponse>
GetAsync(Uri Uri, X509Certificate Certificate, EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
114 int TimeoutMs, params KeyValuePair<string, string>[] Headers)
118 return new ContentResponse(
new NotSupportedException(
"Contracts feature not supported or activated on this system."));
122 if (!this.handlerAdded)
124 Client.PetitionedIdentityResponseReceived += this.Client_PetitionedIdentityResponseReceived;
125 this.handlerAdded =
true;
129 string LegalId = Uri.AbsolutePath;
134 return new ContentResponse(
string.Empty, Identity, Array.Empty<
byte>());
138 TaskCompletionSource<LegalIdentityPetitionResponseEventArgs> Result =
new TaskCompletionSource<LegalIdentityPetitionResponseEventArgs>();
139 string PetitionId = Guid.NewGuid().ToString();
140 string Purpose =
null;
142 if (!
string.IsNullOrEmpty(Uri.Query))
144 NameValueCollection Parameters = HttpUtility.ParseQueryString(Uri.Query);
146 foreach (
string Parameter in Parameters.AllKeys)
158 if (
string.IsNullOrEmpty(Purpose))
159 Purpose =
"Processing referenced identity.";
163 this.identityPetitions[PetitionId] = Result;
170 Task
_ = Task.Delay(TimeoutMs).ContinueWith((T) =>
172 Result.TrySetException(
new TimeoutException(
"Response to petition not received within allotted time."));
173 return Task.CompletedTask;
180 this.identityPetitions.Remove(PetitionId);
196 if (this.identityPetitions.TryGetValue(e.
PetitionId, out TaskCompletionSource<LegalIdentityPetitionResponseEventArgs> Result))
197 Result.TrySetResult(e);
200 return Task.CompletedTask;
212 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator, params KeyValuePair<string, string>[] Headers)
227 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
TemporaryStream Destination, params KeyValuePair<string, string>[] Headers)
242 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
int TimeoutMs, params KeyValuePair<string, string>[] Headers)
244 return this.
GetTempStreamAsync(Uri, Certificate, RemoteCertificateValidator, TimeoutMs,
null, Headers);
258 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
int TimeoutMs,
TemporaryStream Destination,
259 params KeyValuePair<string, string>[] Headers)
261 ContentResponse Content = await this.
GetAsync(Uri, Certificate, RemoteCertificateValidator, TimeoutMs, Headers);
266 StringBuilder Xml =
new StringBuilder();
267 Identity.
Serialize(Xml,
true,
true,
true,
true,
true,
true,
true);
268 byte[] Bin = Encoding.UTF8.GetBytes(Xml.ToString());
269 bool DestinationCreated =
false;
271 if (Destination is
null)
274 DestinationCreated =
true;
279 await Destination.
WriteAsync(Bin, 0, Bin.Length);
283 if (DestinationCreated)
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Exception Error
Error response.
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.
Static class managing the runtime environment of the IoT Gateway.
static ContractsClient ContractsClient
XMPP Contracts Client, if such a compoent is available on the XMPP broker.
Adds support for legal identities, smart contracts and signatures to an XMPP client.
Task PetitionIdentityAsync(string LegalId, string PetitionId, string Purpose)
Sends a petition to the owner of a legal identity, to access the information in the identity....
Task< LegalIdentity > GetLegalIdentityAsync(string LegalIdentityId)
Gets legal identity registered with the account.
Event arguments for legal identity petition responses
string PetitionId
Petition ID
LegalIdentity RequestedIdentity
Requested identity, if accepted, null if rejected.
void Serialize(StringBuilder Xml, bool IncludeNamespace, bool IncludeIdAttribute, bool IncludeClientSignature, bool IncludeAttachments, bool IncludeStatus, bool IncludeServerSignature, bool IncludeAttachmentReferences)
Serializes the identity to XML
Abstract base class for contractual parameters
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override void Dispose(bool disposing)
Releases the unmanaged resources used by the System.IO.Stream and optionally releases the managed res...
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...
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).
async 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).
string[] UriSchemes
Supported URI schemes.
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).
void Dispose()
IDisposable.Dispose
LegalIdentityGetter()
Gets legal identities.
bool CanGet(Uri Uri, out Grade Grade)
If the getter is able to get a resource, given its URI.
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).
static string DefaultPurpose
Default purpose string.
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).
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).
Basic interface for Internet Content getters. A class implementing this interface and having a defaul...