Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SendXmlMessage.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
14
16{
21 {
26 : base("Xmpp/SendXmlMessage",
27 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
28 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
29 {
30 }
31
32 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(SendXmlMessage).Namespace + ".JSON.SendXmlMessage.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SendXmlMessage).Namespace + ".XML.SendXmlMessage.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
46
47 string To = (string)Parameters["PTo"].AssociatedObjectValue;
48
49 if (string.IsNullOrEmpty(To))
50 throw new BadRequestException("Missing recipient JID.");
51
52 if (!IsValidJID(To))
53 throw new BadRequestException("Recpient must be a Bare or Full JID.");
54
55 object Obj = Parameters["PXml"].AssociatedObjectValue;
56 string Xml;
57
58 if (Obj is string s)
59 {
60 if (string.IsNullOrEmpty(s))
61 throw new BadRequestException("Message is empty.");
62
63 if (!XML.IsValidXml(s, false, false, true, true, false, false))
64 throw new BadRequestException("Message is not valid XML.");
65
66 Xml = s;
67 }
68 else if (Obj is XmlDocument Doc)
69 Xml = Doc.DocumentElement.OuterXml;
70 else if (Obj is XmlElement E)
71 Xml = E.OuterXml;
72 else
73 throw new BadRequestException("Message is not valid XML.");
74
75 string Subject = (string)Parameters["PSubject"]?.AssociatedObjectValue;
76 string Language = (string)Parameters["PLanguage"]?.AssociatedObjectValue;
77 string Id = (string)Parameters["PId"]?.AssociatedObjectValue;
78
79 if (string.IsNullOrEmpty(Id))
80 Id = Guid.NewGuid().ToString();
81
82 bool Result;
83
84 if (XmppServerModule.Server is null)
85 {
87
88 Result = false;
89
90 if (Client is null)
91 {
92 if (Types.TryGetModuleParameter("XMPP", out Obj) && Obj is XmppClient Client2)
93 Client = Client2;
94 }
95
96 if (Client is null)
97 throw new ServiceUnavailableException("XMPP Client not available.");
98
99 await Client.SendMessage(QoSLevel.Unacknowledged, MessageType.Normal, Id, To, Xml,
100 string.Empty, string.Empty, Language, string.Empty, string.Empty, null, null);
101
102 Result = true;
103 }
104 else
105 {
106 Result = await XmppServerModule.Server.SendMessage(string.Empty, Id,
107 User.Account.UserName + "@" + XmppServerModule.Server.Domain, To,
108 Language, Xml);
109 }
110
111 await Response.Return(new NamedDictionary<string, object>("StanzaResult", AgentNamespace)
112 {
113 { "sent", Result },
114 { "id", Id }
115 });
116 }
117 }
118}
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 bool IsValidXml(string Xml)
Checks if a string is valid XML
Definition: XML.cs:1223
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
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 SendMessage(MessageType Type, string To, string CustomXml, string Body, string Subject, string Language, string ThreadId, string ParentThreadId)
Sends a simple chat message
Definition: XmppClient.cs:5395
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
CaseInsensitiveString UserName
User Name of account
Definition: Account.cs:99
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.
Sends an XML message to an XMPP recipient.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
SendXmlMessage()
Sends an XML message to an XMPP recipient.
Service Module hosting the XMPP broker and its components.
QoSLevel
Quality of Service Level for asynchronous messages. Support for QoS Levels must be supported by the r...
Definition: QoSLevel.cs:8
MessageType
Type of message received.
Definition: MessageType.cs:7