Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InformationQuery.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
14
16{
22 {
28 : base("Xmpp/InformationQuery",
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(InformationQuery).Namespace + ".JSON.InformationQuery.req");
35 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(InformationQuery).Namespace + ".XML.InformationQuery.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 To = (string)Parameters["PTo"].AssociatedObjectValue;
55 string Type = (string)Parameters["PType"].AssociatedObjectValue;
56 XmlDocument Query = (XmlDocument)Parameters["PQuery"].AssociatedObjectValue;
57
58 if (string.IsNullOrEmpty(To))
59 throw new BadRequestException("Recipient not defined.");
60
61 if (!IsValidJID(To))
62 throw new BadRequestException("Invalid JID.");
63
64 if (Type != "get" && Type != "set")
65 throw new BadRequestException("Invalid Type.");
66
67 if (Query.DocumentElement is null)
68 throw new BadRequestException("Empty query.");
69
70 if (XmppServerModule.Server is null)
71 {
73
74 if (Client is null)
75 {
76 if (Types.TryGetModuleParameter("XMPP", out XmppClient Client2))
77 Client = Client2;
78 }
79
80 if (Client is null)
81 throw new ServiceUnavailableException("XMPP Client not available.");
82
83 await Client.SendIq(string.Empty, To, Query.DocumentElement.OuterXml, Type,
84 async (Sender, e) =>
85 {
87 {
88 { "ok", e.Ok },
89 { "Stanza", e.Response }
90 };
91
92 if (!e.Ok)
93 {
94 Result["errorCode"] = e.ErrorCode;
95 Result["errorText"] = e.ErrorText;
96 Result["errorType"] = e.ErrorType;
97 Result["ErrorElement"] = e.ErrorElement;
98 }
99
100 await Response.Return(Result);
101 }, null);
102 }
103 else
104 {
105 await XmppServerModule.Server.SendIqRequest(Type,
106 User.UserName + "@" + (Gateway.Domain?.Value ?? string.Empty),
107 To, string.Empty, Query.DocumentElement.OuterXml, false, async (Sender, e) =>
108 {
110 {
111 { "ok", e.Ok },
112 { "Stanza", e.Response }
113 };
114
115 if (!e.Ok)
116 {
117 Result["errorCode"] = e.ErrorCode;
118 Result["errorText"] = e.ErrorText;
119 Result["errorType"] = e.ErrorType;
120 Result["ErrorElement"] = e.ErrorElement;
121 }
122
123 await Response.Return(Result);
124 }, null);
125 }
126 }
127
128 }
129}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
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
Task Return(Exception ex)
Returns an error to the client.
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
Task< uint > SendIq(string Id, string To, string Xml, string Type, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Sends an IQ stanza.
Definition: XmppClient.cs:3892
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
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
Abstract base class for XMPP resources.
static bool IsValidJID(string JID)
Checks if a string is a valid Bare or Full JID.
Performs a presence probe on one of the contacts to which the account has an approved presence subscr...
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
InformationQuery()
Performs a presence probe on one of the contacts to which the account has an approved presence subscr...
override bool Synchronous
If the resource is synchronous (i.e. returns a response in the method handler), or if it is asynchron...
Service Module hosting the XMPP broker and its components.