Exemplo n.º 1
0
        public virtual IHttpActionResult BroadcastEvents(string zoneId = null, string contextId = null)
        {
            var  eventService    = Service as IEventService <TMultiple>;
            bool eventsSupported = (eventService != null);

            if (!eventsSupported)
            {
                return(BadRequest("Support for SIF Events has not been implemented."));
            }

            IHttpActionResult result;

            try
            {
                IRegistrationService registrationService = RegistrationManager.GetProviderRegistrationService(
                    ProviderSettings,
                    sessionService);

                if (registrationService is NoRegistrationService)
                {
                    result = BadRequest("SIF Events are only supported in a BROKERED environment.");
                }
                else
                {
                    IEventIterator <TMultiple> eventIterator = eventService.GetEventIterator(zoneId, contextId);

                    if (eventIterator == null)
                    {
                        result = BadRequest("SIF Events implementation is not valid.");
                    }
                    else
                    {
                        Model.Infrastructure.Environment environment = registrationService.Register();

                        // Retrieve the current Authorisation Token.
                        AuthorisationToken token = registrationService.AuthorisationToken;

                        // Retrieve the EventsConnector endpoint URL.
                        string url = EnvironmentUtils.ParseServiceUrl(
                            environment,
                            ServiceType.UTILITY,
                            InfrastructureServiceNames.eventsConnector);

                        while (eventIterator.HasNext())
                        {
                            SifEvent <TMultiple> sifEvent = eventIterator.GetNext();

                            var requestHeaders = new NameValueCollection()
                            {
                                { EventParameterType.eventAction.ToDescription(), sifEvent.EventAction.ToDescription() },
                                { EventParameterType.messageId.ToDescription(), sifEvent.Id.ToString() },
                                { EventParameterType.messageType.ToDescription(), "EVENT" },
                                { EventParameterType.serviceName.ToDescription(), $"{TypeName}s" }
                            };

                            switch (sifEvent.EventAction)
                            {
                            case EventAction.UPDATE_FULL:
                                requestHeaders.Add(EventParameterType.Replacement.ToDescription(), "FULL");
                                break;

                            case EventAction.UPDATE_PARTIAL:
                                requestHeaders.Add(EventParameterType.Replacement.ToDescription(), "PARTIAL");
                                break;
                            }

                            string requestBody = SerialiseEvents(sifEvent.SifObjects);
                            HttpUtils.PostRequest(
                                url,
                                token,
                                requestBody,
                                ProviderSettings.CompressPayload,
                                contentTypeOverride: ContentType.ToDescription(),
                                acceptOverride: Accept.ToDescription(),
                                requestHeaders: requestHeaders);
                        }

                        result = Ok();
                    }
                }
            }
            catch (Exception e)
            {
                result = InternalServerError(e);
            }

            return(result);
        }
Exemplo n.º 2
0
        public virtual IHttpActionResult BroadcastEvents(string zoneId = null, string contextId = null)
        {
            IEventService <TMultiple> eventService = service as IEventService <TMultiple>;
            bool eventsSupported = (eventService != null);

            if (!eventsSupported)
            {
                return(BadRequest("Support for SIF Events has not been implemented."));
            }

            IHttpActionResult result;

            try
            {
                IRegistrationService registrationService = RegistrationManager.ProviderRegistrationService;

                if (registrationService is NoRegistrationService)
                {
                    result = BadRequest("SIF Events are only supported in a BROKERED environment.");
                }
                else
                {
                    IEventIterator <TMultiple> eventIterator = eventService.GetEventIterator(zoneId, contextId);

                    if (eventIterator == null)
                    {
                        result = BadRequest("SIF Events implementation is not valid.");
                    }
                    else
                    {
                        // Retrieve the current Authorisation Token.
                        registrationService.Register();
                        AuthorisationToken token = registrationService.AuthorisationToken;

                        // Retrieve the EventsConnector endpoint URL.
                        Environment environmentTemplate = EnvironmentUtils.LoadFromSettings(SettingsManager.ProviderSettings);
                        string      storedSessionToken  = SessionsManager.ProviderSessionService.RetrieveSessionToken(
                            environmentTemplate.ApplicationInfo.ApplicationKey,
                            environmentTemplate.SolutionId,
                            environmentTemplate.UserToken,
                            environmentTemplate.InstanceId);
                        Environment environment = authService.GetEnvironmentBySessionToken(storedSessionToken);
                        string      url         = EnvironmentUtils.ParseServiceUrl(environment, ServiceType.UTILITY, InfrastructureServiceNames.eventsConnector) + "/" + TypeName + "s";

                        while (eventIterator.HasNext())
                        {
                            SifEvent <TMultiple> sifEvent = eventIterator.GetNext();

                            NameValueCollection headerFields = new NameValueCollection()
                            {
                                { HttpUtils.RequestHeader.eventAction.ToDescription(), sifEvent.EventAction.ToDescription() },
                                { HttpUtils.RequestHeader.messageId.ToDescription(), sifEvent.Id.ToString() },
                                { HttpUtils.RequestHeader.messageType.ToDescription(), "EVENT" },
                                { HttpUtils.RequestHeader.serviceName.ToDescription(), $"{TypeName}s" }
                            };

                            switch (sifEvent.EventAction)
                            {
                            case EventAction.UPDATE_FULL:
                                headerFields.Add(HttpUtils.RequestHeader.Replacement.ToDescription(), "FULL");
                                break;

                            case EventAction.UPDATE_PARTIAL:
                                headerFields.Add(HttpUtils.RequestHeader.Replacement.ToDescription(), "PARTIAL");
                                break;
                            }

                            string body = SerialiseEvents(sifEvent.SifObjects);
                            string xml  = HttpUtils.PostRequest(url, token, body, headerFields: headerFields);
                        }
                    }

                    result = Ok();
                }
            }
            catch (Exception e)
            {
                result = InternalServerError(e);
            }

            return(result);
        }