Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetRosterItem.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/GetRosterItem",
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(GetRosterItem).Namespace + ".JSON.GetRosterItem.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(GetRosterItem).Namespace + ".XML.GetRosterItem.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
46 string BareJid = (string)Parameters["PBareJid"].AssociatedObjectValue;
47
48 if (string.IsNullOrEmpty(BareJid))
49 throw new BadRequestException("Empty Bare JID.");
50
51 if (!IsValidBareJID(BareJid))
52 throw new BadRequestException("Invalid Bare JID.");
53
54 if (XmppServerModule.Server is null)
55 {
57
58 if (Client is null)
59 {
60 if (Types.TryGetModuleParameter("XMPP", out object Obj) && Obj is XmppClient Client2)
61 Client = Client2;
62 }
63
64 if (Client is null)
65 throw new ServiceUnavailableException("XMPP Client not available.");
66
67 Networking.XMPP.RosterItem Item = Client[BareJid]
68 ?? throw new NotFoundException("Roster item not found.");
69
70 await Response.Return(new NamedDictionary<string, object>("RosterItem", AgentNamespace)
71 {
72 { "bareJid", Item.BareJid },
73 { "pendingSubscription", Item.PendingSubscription == PendingSubscription.Subscribe },
74 { "status", Item.State },
75 { "name", Item.Name },
76 { "Groups", Item.Groups }
77 });
78 }
79 else
80 {
81 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
82 IRosterItem Item = await PersistenceLayer.GetRosterItem(User.UserName, BareJid)
83 ?? throw new NotFoundException("Roster item not found.");
84
85 await Response.Return(new NamedDictionary<string, object>("RosterItem", AgentNamespace)
86 {
87 { "bareJid", Item.BareJid.Value },
88 { "pendingSubscription", Item.PendingSubscription },
89 { "status", Item.Subscription },
90 { "name", Item.Name },
91 { "Groups", Item.Groups }
92 });
93 }
94 }
95 }
96}
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 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 has not found anything matching the Request-URI. No indication is given of whether the con...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
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
async Task< IRosterItem > GetRosterItem(CaseInsensitiveString UserName, CaseInsensitiveString Jid)
Gets a roster item for an account.
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 IsValidBareJID(string JID)
Checks if a string is a valid Bare JID.
Gets information about a specific item in the account's roster.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
GetRosterItem()
Gets information about a specific item in the account's roster.
Service Module hosting the XMPP broker and its components.
Interface for roster items.
Definition: IRosterItem.cs:43