Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetIdentity.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
15
17{
22 {
26 public GetIdentity()
27 : base("Legal/GetIdentity",
28 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
29 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
30 {
31 }
32
33 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetIdentity).Namespace + ".JSON.GetIdentity.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetIdentity).Namespace + ".XML.GetIdentity.req");
35
40 public override bool Synchronous => false;
41
50 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
51 {
53 string LegalId = (string)Parameters["PLegalId"].AssociatedObjectValue;
54 int i = LegalId.IndexOf('@');
55 if (i < 0)
56 throw new BadRequestException("Invalid Legal ID.");
57
58 string LegalDomain = LegalId.Substring(i + 1);
59 string LegalNamespace = LegalComponent.NamespaceLegalIdentity(NamespaceSet.Current);
60
61 if (XmppServerModule.Server is null)
62 {
64
65 if (Client is null)
66 {
67 if (Types.TryGetModuleParameter("XMPP", out object Obj) && Obj is XmppClient Client2)
68 Client = Client2;
69 }
70
71 if (Client is null)
72 throw new ServiceUnavailableException("XMPP Client not available.");
73
74 await Client.SendIqGet(LegalDomain, "<getLegalIdentity id=\"" + XML.Encode(LegalId) + "\" xmlns=\"" +
75 LegalNamespace + "\"/>", async (Sender, e) =>
76 {
77 XmlElement E;
78
79 if (e.Ok && !((E = e.FirstElement) is null) && E.LocalName == "identity")
80 {
81 await Response.Return(new NamedDictionary<string, object>("IdentityResponse", AgentNamespace)
82 {
83 { "Identity", E }
84 });
85 }
86 else
87 await Response.SendResponse(ToHttpException(e.StanzaError) ?? new ServiceUnavailableException("Unable to get identity."));
88
89 }, null);
90 }
91 else
92 {
93 await XmppServerModule.Server.SendIqRequest("get",
94 User.UserName + "@" + (Gateway.Domain?.Value ?? string.Empty),
95 LegalDomain, string.Empty, "<getLegalIdentity id=\"" + XML.Encode(LegalId) + "\" xmlns=\"" +
96 LegalNamespace + "\"/>", async (Sender, e) =>
97 {
98 XmlElement E;
99
100 if (e.Ok && !((E = e.FirstElement) is null) && E.LocalName == "identity")
101 {
102 await Response.Return(new NamedDictionary<string, object>("IdentityResponse", AgentNamespace)
103 {
104 { "Identity", E }
105 });
106 }
107 else
108 await Response.SendResponse(ToHttpException(XmppClient.GetExceptionObject(e.ErrorElement)) ?? new ServiceUnavailableException("Unable to get identity."));
109
110 }, null);
111 }
112 }
113
114 }
115}
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
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:2354
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:3187
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:18
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...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
static XmppException GetExceptionObject(XmlElement StanzaElement)
Gets an XMPP Exception object corresponding to its XML definition.
Definition: XmppClient.cs:3301
Task< uint > SendIqGet(string To, string Xml, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Sends an IQ Get request.
Definition: XmppClient.cs:3559
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:583
Class managing a script expression.
Definition: Expression.cs:39
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
static Exception ToHttpException(XmppException ex)
Tries to convert an XMPP Exception to an HTTP Exception.
Service Module hosting the XMPP broker and its components.
NamespaceSet
Namespace versions
Definition: NamespaceSet.cs:7