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;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
12using Waher.Script;
16
18{
23 {
27 public GetIdentity()
28 : base("Legal/GetIdentity",
29 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
30 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
31 {
32 }
33
34 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(GetIdentity).Namespace + ".JSON.GetIdentity.req");
35 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetIdentity).Namespace + ".XML.GetIdentity.req");
36
41 public override bool Synchronous => false;
42
51 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
52 {
54 string LegalId = (string)Parameters["PLegalId"].AssociatedObjectValue;
55 int i = LegalId.IndexOf('@');
56 if (i < 0)
57 throw new BadRequestException("Invalid Legal ID.");
58
59 string LegalDomain = LegalId[(i + 1)..];
60 string LegalNamespace = LegalComponent.NamespaceLegalIdentity(NamespaceSet.Current);
61
62 if (XmppServerModule.Server is null)
63 {
65
66 if (Client is null)
67 {
68 if (Types.TryGetModuleParameter("XMPP", out XmppClient Client2))
69 Client = Client2;
70 }
71
72 if (Client is null)
73 throw new ServiceUnavailableException("XMPP Client not available.");
74
75 await Client.SendIqGet(LegalDomain, "<getLegalIdentity id=\"" + XML.Encode(LegalId) + "\" xmlns=\"" +
76 LegalNamespace + "\"/>", async (Sender, e) =>
77 {
78 XmlElement E;
79
80 if (e.Ok && !((E = e.FirstElement) is null) && E.LocalName == "identity")
81 {
82 await Response.Return(new NamedDictionary<string, object>("IdentityResponse", AgentNamespace)
83 {
84 { "Identity", E }
85 });
86 }
87 else
88 await Response.SendResponse(ToHttpException(Request, e.StanzaError) ?? new ServiceUnavailableException("Unable to get identity."));
89
90 }, null);
91 }
92 else
93 {
94 await XmppServerModule.Server.SendIqRequest("get",
95 User.UserName + "@" + (Gateway.Domain?.Value ?? string.Empty),
96 LegalDomain, string.Empty, "<getLegalIdentity id=\"" + XML.Encode(LegalId) + "\" xmlns=\"" +
97 LegalNamespace + "\"/>", true, async (Sender, e) =>
98 {
99 XmlElement E;
100
101 if (e.Ok && !((E = e.FirstElement) is null) && E.LocalName == "identity")
102 {
103 await Response.Return(new NamedDictionary<string, object>("IdentityResponse", AgentNamespace)
104 {
105 { "Identity", E }
106 });
107 }
108 else
109 await Response.SendResponse(ToHttpException(Request, XmppClient.GetExceptionObject(e.ErrorElement)) ?? new ServiceUnavailableException("Unable to get identity."));
110
111 }, null);
112 }
113 }
114
115 }
116}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:3087
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:4038
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:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
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:58
static XmppException GetExceptionObject(XmlElement StanzaElement)
Gets an XMPP Exception object corresponding to its XML definition.
Definition: XmppClient.cs:3340
Task< uint > SendIqGet(string To, string Xml, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Sends an IQ Get request.
Definition: XmppClient.cs:3598
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:607
Class managing a script expression.
Definition: Expression.cs:41
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(HttpRequest Request, 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