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;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
12using Waher.Script;
15
17{
22 {
27 : base("Xmpp/SendXmlMessage",
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(SendXmlMessage).Namespace + ".JSON.SendXmlMessage.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SendXmlMessage).Namespace + ".XML.SendXmlMessage.req");
35
44 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
45 {
47
48 string To = (string)Parameters["PTo"].AssociatedObjectValue;
49
50 if (string.IsNullOrEmpty(To))
51 throw new BadRequestException("Missing recipient JID.");
52
53 if (!IsValidJID(To))
54 throw new BadRequestException("Recpient must be a Bare or Full JID.");
55
56 object Obj = Parameters["PXml"].AssociatedObjectValue;
57 string Xml;
58
59 if (Obj is string s)
60 {
61 if (string.IsNullOrEmpty(s))
62 throw new BadRequestException("Message is empty.");
63
64 if (!XML.IsValidXml(s, false, false, true, true, false, false))
65 throw new BadRequestException("Message is not valid XML.");
66
67 Xml = s;
68 }
69 else if (Obj is XmlDocument Doc)
70 Xml = Doc.DocumentElement.OuterXml;
71 else if (Obj is XmlElement E)
72 Xml = E.OuterXml;
73 else
74 throw new BadRequestException("Message is not valid XML.");
75
76 string Subject = (string)Parameters["PSubject"]?.AssociatedObjectValue;
77 string Language = (string)Parameters["PLanguage"]?.AssociatedObjectValue;
78 string Id = (string)Parameters["PId"]?.AssociatedObjectValue;
79
80 if (string.IsNullOrEmpty(Id))
81 Id = Guid.NewGuid().ToString();
82
83 bool Result;
84
85 if (XmppServerModule.Server is null)
86 {
88
89 Result = false;
90
91 if (Client is null)
92 {
93 if (Types.TryGetModuleParameter("XMPP", out XmppClient Client2))
94 Client = Client2;
95 }
96
97 if (Client is null)
98 throw new ServiceUnavailableException("XMPP Client not available.");
99
100 await Client.SendMessage(QoSLevel.Unacknowledged, MessageType.Normal, Id, To, Xml,
101 string.Empty, string.Empty, Language, string.Empty, string.Empty, null, null);
102
103 Result = true;
104 }
105 else
106 {
107 Result = await XmppServerModule.Server.SendMessage(string.Empty, Id,
108 User.Account.UserName + "@" + XmppServerModule.Server.Domain, To,
109 Language, Xml);
110 }
111
112 await Response.Return(new NamedDictionary<string, object>("StanzaResult", AgentNamespace)
113 {
114 { "sent", Result },
115 { "id", Id }
116 });
117 }
118 }
119}
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 bool IsValidXml(string Xml)
Checks if a string is valid XML
Definition: XML.cs:1397
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
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 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:5447
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
CaseInsensitiveString UserName
User Name of account
Definition: Account.cs:141
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