private void SetSubscription(string callbackEndpoint, string uvid, Identity identity,
                                     Common.Services.Internal.Interfaces.Notification notification, List <KeyValuePair <string, string> > paramList)
        {
            VisSubscription newSub = new VisSubscription();

            newSub.CallbackEndpoint          = callbackEndpoint;
            newSub.MessageID                 = uvid;
            newSub.MessageType               = _messageTypeService.Get(x => x.Name.ToLower() == "rtz").First();
            newSub.SubscriberIdentity        = identity;
            newSub.TimeOfSubscriptionRequest = DateTime.UtcNow;
            newSub.IsAuthorized              = true;
            _subscriptionService.Insert(newSub);

            // Send message to new subscriber
            var message = _publishedMessageService.Get(x => x.MessageID == uvid).FirstOrDefault();

            if (message != null)
            {
                _publishedMessageService.SendMessage(System.Text.Encoding.Default.GetString(message.Message),
                                                     uvid, newSub.CallbackEndpoint,
                                                     new Identity {
                    Name = identity.Name, UID = identity.UID
                });
            }

            // Save to DB
            _context.SaveChanges();

            _logEventService.LogSuccess(EventNumber.VIS_subscribeToVoyagePlan_response, EventDataType.Other,
                                        paramList,
                                        newSub.MessageID);
        }
示例#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 UVID.");
            }

            if (!FormatValidation.IsValidUvid(dataId))
            {
                throw CreateHttpResponseException(HttpStatusCode.BadRequest, "Invalid UVID format");
            }

            try
            {
                foreach (var subscription in subscriptions)
                {
                    var uri = subscription.EndpointURL.ToString().ToLower();

                    var sub = _subscriptionService.Get(s =>
                                                       s.SubscriberIdentity.UID == subscription.IdentityId &&
                                                       s.MessageID == dataId &&
                                                       s.CallbackEndpoint.ToLower() == uri, includeProperties: "SubscriberIdentity, MessageType").FirstOrDefault();

                    if (sub == null)
                    {
                        var acl = _aCLObjectService.Get(i =>
                                                        i.MessageID == dataId &&
                                                        i.Subscriber.UID == subscription.IdentityId).FirstOrDefault();

                        if (acl != null)
                        {
                            _subscriptionService.Insert(ConvertToEntity(subscription, dataId));
                        }
                        else
                        {
                            log.Debug(string.Format("No access for identity {0}", subscription.IdentityId));
                        }
                    }
                    else if (sub.IsAuthorized == false)
                    {
                        sub.IsAuthorized = true;
                    }

                    // Send message to new subscriber
                    var message = _publishedMessageService.Get(x => x.MessageID == dataId).FirstOrDefault();
                    if (message != null)
                    {
                        _publishedMessageService.SendMessage(System.Text.Encoding.Default.GetString(message.Message), dataId, subscription.EndpointURL.ToString(), new Identity {
                            Name = subscription.IdentityName, UID = subscription.IdentityId
                        });
                    }
                }

                SetLastInteractionTime();
                _context.SaveChanges();

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

                throw;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);

                string msg = "VIS internal server error. " + ex.Message;

                throw CreateHttpResponseException(HttpStatusCode.InternalServerError, msg);
            }
        }