/// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        /// <param name="dataId"></param>
        public void SendMessageToSubsribers(string message, string dataId)
        {
            //call proxy class to SSC -> callservice for each subscriber
            var subscriptions = _subscriberService.Get(x =>
                                                       x.MessageID == dataId && x.IsAuthorized, includeProperties: "SubscriberIdentity");

            if (subscriptions == null)
            {
                return;
            }

            foreach (var subscription in subscriptions)
            {
                // If no queueId setup new queue
                if (string.IsNullOrEmpty(subscription.QueueId))
                {
                    var queueId = SetupQueue(message, dataId, subscription.MbEndpoint, subscription.SubscriberIdentity);
                    subscription.QueueId = queueId;

                    _subscriberService.Update(subscription);
                }

                // Send PCM to AMSS service
                SendToAmssService(message, dataId, subscription.AmssEndpoint, subscription.SubscriberIdentity);
            }
        }
Example #2
0
        public ResponseObj AddSubscription([FromBody] List <SubscriptionObject> subscriptions, [FromUri] string dataId)
        {
            log.Info("Incoming request to " + GetCurrentMethod());

            if (string.IsNullOrEmpty(dataId))
            {
                throw CreateHttpResponseException(HttpStatusCode.BadRequest, "Missing required parameter dataID.");
            }

            var responseText = string.Empty;

            try
            {
                foreach (var subscription in subscriptions)
                {
                    var mbUri   = subscription.MbEndpointURL.ToString().ToLower();
                    var amssUri = subscription.AmssEndpointURL.ToString().ToLower();

                    var entity = _SpisSubscriptionService.Get(s =>
                                                              s.SubscriberIdentity.UID == subscription.IdentityId &&
                                                              s.MessageID == dataId &&
                                                              s.MbEndpoint.ToLower() == mbUri &&
                                                              s.AmssEndpoint.ToLower() == amssUri, includeProperties: "SubscriberIdentity, MessageType").FirstOrDefault();

                    if (entity == null)
                    {
                        responseText += string.Format("Subscription for dataId:{0} mb endpoint: {1} amss endpoint {2} was created\r\n", dataId, mbUri, amssUri);
                        var acl = _aCLObjectService.Get(i =>
                                                        i.MessageID == dataId &&
                                                        i.Subscriber.UID == subscription.IdentityId).FirstOrDefault();

                        if (acl == null)
                        {
                            log.Debug(string.Format("No access for identity {0}", subscription.IdentityId));
                            throw CreateHttpResponseException(HttpStatusCode.Forbidden, string.Format("No access for identity {0}", subscription.IdentityId));
                        }

                        entity                    = new SpisSubscription();
                        entity.MessageID          = dataId;
                        entity.IsAuthorized       = true;
                        entity.MbEndpoint         = subscription.MbEndpointURL.ToString();
                        entity.AmssEndpoint       = subscription.AmssEndpointURL.ToString();
                        entity.MessageType        = _messageTypeService.Get(x => x.Name.ToLower() == "rtz").FirstOrDefault();
                        entity.SubscriberIdentity = _identityService.Get(x => x.UID == subscription.IdentityId).FirstOrDefault();

                        _SpisSubscriptionService.Insert(entity);
                        _context.SaveChanges();
                    }
                    else
                    {
                        responseText += string.Format("Subscription for dataId:{0} mb endpoint: {1} amss endpoint {2} already exists\r\n", dataId, mbUri, amssUri);

                        if (entity.IsAuthorized == false)
                        {
                            entity.IsAuthorized = true;
                            _SpisSubscriptionService.Update(entity);
                        }
                    }

                    // Send message to PortCDM
                    var    message       = _publishedMessageService.Get(x => x.MessageID == dataId).FirstOrDefault();
                    string messageString = null;
                    if (message != null)
                    {
                        messageString = System.Text.Encoding.UTF8.GetString(message.Message);
                    }

                    entity.QueueId = _publishedMessageService.SendMessage(messageString,
                                                                          dataId, subscription.MbEndpointURL.ToString(),
                                                                          subscription.AmssEndpointURL.ToString(),
                                                                          new Identity {
                        UID  = subscription.IdentityId,
                        Name = subscription.IdentityName
                    });

                    responseText += string.Format("Port CDM queue was created with id: {0}\r\n", entity.QueueId);

                    _SpisSubscriptionService.Update(entity);
                    _context.SaveChanges();
                }

                log.Debug(responseText);
                return(new ResponseObj(dataId));
            }
            catch (HttpResponseException ex)
            {
                log.Error(ex.Message, ex);
                throw;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);

                string msg = "SPIS internal server error. " + ex.Message;
                throw CreateHttpResponseException(HttpStatusCode.InternalServerError, msg);
            }
        }