void HandleWriteData(StanzaIQ Request) { XElement WriteDataElement = Request.Payload.Elements().First(); try { List <ResourceAccess> LimitedAccess = GetAccessRights(Request.From, LWTSD.GetAccessToken(WriteDataElement)); int NrWrittenValues = 0; foreach (XElement WriteElement in WriteDataElement.Elements(LWTSD.Namespace + "write")) { ResourcePath Path = WriteElement.Attribute("resource-path").Value; if (!ResourceAccess.AllowsWrite(LimitedAccess, Path)) { throw new AccessViolation(); } if (!Resources.ContainsKey(Path)) { throw new InvalidOperationException("Path does not exist: " + Path); // todo: explicit type to make it possible to set correct error code } Resource Res = Resources[Path]; if (!Res.SupportsWrite) { throw new InvalidOperationException("Resource is not writeable: " + Path); // todo: explicit type to make it possible to set correct error code } Res.LoadFromWrite(WriteElement); NrWrittenValues++; } if (NrWrittenValues == 0) { throw new Exception("No values found"); } StanzaIQ Response = new StanzaIQ(StanzaIQ.StanzaIQType.Result); Response.ID = Request.ID; Response.To = Request.From; Uplink.SendStanza(Response); } catch (System.Exception ex) { StanzaIQ ErrorResponse = new StanzaIQ(StanzaIQ.StanzaIQType.Error); ErrorResponse.ID = Request.ID; ErrorResponse.To = Request.From; XElement ErroReasonElement = new XElement(LWTSD.Namespace + "errorreason"); ErroReasonElement.SetAttributeValue("reason", ErrorReason.InvalidData); ErroReasonElement.Value = ex.ToString(); ErrorResponse.Payload.Add(ErroReasonElement); Uplink.SendStanza(ErrorResponse); } }
void HandleSubscribe(StanzaIQ Request) { XElement SubscribeElement = Request.Payload.Elements().First(); try { string AccessToken = LWTSD.GetAccessToken(SubscribeElement); List <ResourceAccess> LimitedAccess = GetAccessRights(Request.From, AccessToken); Subscriber Subscription = new Subscriber(); Subscription.SubscriptionID = SubscribeElement.Attribute("subscriptionid").Value; Subscription.SubscriberAddress = Request.From; Subscription.AccessToken = AccessToken; foreach (XElement WriteElement in SubscribeElement.Elements(LWTSD.Namespace + "trigger")) { ResourcePath Path = WriteElement.Attribute("onresource").Value; if (!ResourceAccess.AllowsRead(LimitedAccess, Path)) { throw new AccessViolation(); } if (!Resources.ContainsKey(Path)) { throw new InvalidOperationException("Path does not exist: " + Path); // todo: explicit type to make it possible to set correct error code } Resource Res = Resources[Path]; if (!Res.SupportsRead) { throw new InvalidOperationException("Resource is not readable: " + Path); // todo: explicit type to make it possible to set correct error code } ResourceSubscription Trigger = new ResourceSubscription() { Path = Path }; Subscription.Triggers.Add(Trigger); } if (Subscription.Triggers.Count == 0) { throw new InvalidOperationException("No triggers"); } Subscribers[GetSafeSubscriptionID(Request.From, Subscription.SubscriptionID)] = Subscription; StanzaIQ Response = new StanzaIQ(StanzaIQ.StanzaIQType.Result); Response.ID = Request.ID; Response.To = Request.From; Uplink.SendStanza(Response); } catch (System.Exception ex) { StanzaIQ ErrorResponse = new StanzaIQ(StanzaIQ.StanzaIQType.Error); ErrorResponse.ID = Request.ID; ErrorResponse.To = Request.From; XElement ErroReasonElement = new XElement(LWTSD.Namespace + "errorreason"); ErroReasonElement.SetAttributeValue("reason", ErrorReason.InvalidData); ErroReasonElement.Value = ex.ToString(); ErrorResponse.Payload.Add(ErroReasonElement); Uplink.SendStanza(ErrorResponse); } }