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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
10using Waher.Script;
13
15{
21 {
27 : base("Xmpp/InformationQuery",
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(InformationQuery).Namespace + ".JSON.InformationQuery.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(InformationQuery).Namespace + ".XML.InformationQuery.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 To = (string)Parameters["PTo"].AssociatedObjectValue;
54 string Type = (string)Parameters["PType"].AssociatedObjectValue;
55 XmlDocument Query = (XmlDocument)Parameters["PQuery"].AssociatedObjectValue;
56
57 if (string.IsNullOrEmpty(To))
58 throw new BadRequestException("Recipient not defined.");
59
60 if (!IsValidJID(To))
61 throw new BadRequestException("Invalid JID.");
62
63 if (Type != "get" && Type != "set")
64 throw new BadRequestException("Invalid Type.");
65
66 if (Query.DocumentElement is null)
67 throw new BadRequestException("Empty query.");
68
69 if (XmppServerModule.Server is null)
70 {
72
73 if (Client is null)
74 {
75 if (Types.TryGetModuleParameter("XMPP", out object Obj) && Obj is XmppClient Client2)
76 Client = Client2;
77 }
78
79 if (Client is null)
80 throw new ServiceUnavailableException("XMPP Client not available.");
81
82 await Client.SendIq(string.Empty, To, Query.DocumentElement.OuterXml, Type,
83 async (Sender, e) =>
84 {
86 {
87 { "ok", e.Ok },
88 { "Stanza", e.Response }
89 };
90
91 if (!e.Ok)
92 {
93 Result["errorCode"] = e.ErrorCode;
94 Result["errorText"] = e.ErrorText;
95 Result["errorType"] = e.ErrorType;
96 Result["ErrorElement"] = e.ErrorElement;
97 }
98
99 await Response.Return(Result);
100 }, null);
101 }
102 else
103 {
104 await XmppServerModule.Server.SendIqRequest(Type,
105 User.UserName + "@" + (Gateway.Domain?.Value ?? string.Empty),
106 To, string.Empty, Query.DocumentElement.OuterXml, async (Sender, e) =>
107 {
109 {
110 { "ok", e.Ok },
111 { "Stanza", e.Response }
112 };
113
114 if (!e.Ok)
115 {
116 Result["errorCode"] = e.ErrorCode;
117 Result["errorText"] = e.ErrorText;
118 Result["errorType"] = e.ErrorType;
119 Result["ErrorElement"] = e.ErrorElement;
120 }
121
122 await Response.Return(Result);
123 }, null);
124 }
125 }
126
127 }
128}
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
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 Return(object Object)
Returns an object to the client. This method can only be called once per response,...
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
Task< uint > SendIq(string Id, string To, string Xml, string Type, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Sends an IQ stanza.
Definition: XmppClient.cs:3855
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
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.