/// <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)
            {
                SendMessage(message, dataId, subscription.CallbackEndpoint, subscription.SubscriberIdentity);
            }
        }
        public virtual Models.ResponseObj SubscribeToVoyagePlan([FromUri] string callbackEndpoint, [FromUri] string uvid = null)
        {
            log.Info("Incoming request to " + GetCurrentMethod());
            bool accessToAnyUVID = false;

            var responseObj = new Models.ResponseObj("Success storing subscription request.");

            var paramList = new List <KeyValuePair <string, string> >();
            var param     = new KeyValuePair <string, string>("uvid", uvid);

            paramList.Add(param);
            param = new KeyValuePair <string, string>("callbackEndpoint", callbackEndpoint);
            paramList.Add(param);

            try
            {
                if (string.IsNullOrEmpty(callbackEndpoint))
                {
                    log.Debug("Callback endpoint address is empty");

                    throw CreateHttpResponseException(HttpStatusCode.BadRequest, "Required parameter CallbackEndpoint is missing.");
                }
                if (string.IsNullOrEmpty(InstanceContext.CallerOrgId))
                {
                    log.Debug("Calling organization identity missing in header.");

                    throw CreateHttpResponseException(HttpStatusCode.BadRequest, "Required header incomingOrganizationId is missing.");
                }

                _logEventService.LogInfo(EventNumber.VIS_subscribeToVoyagePlan_request, EventDataType.None, paramList, null);
                // Get identity ether from internal id talbe or from id registry
                var identity = _identityService.GetCallerIdentity();

                //Set data for notification
                var notification = new Common.Services.Internal.Interfaces.Notification();
                notification.FromOrgName        = identity.Name;
                notification.FromOrgId          = identity.UID;
                notification.FromServiceId      = InstanceContext.CallerServiceId;
                notification.NotificationType   = EnumNotificationType.UNAUTHORIZED_REQUEST;
                notification.Subject            = "New subscription request.";
                notification.NotificationSource = EnumNotificationSource.VIS;

                #region No UVID
                if (string.IsNullOrEmpty(uvid))
                {
                    var voyagePlans = _publishedMessageService.Get(x => (int)x.MessageStatus != 8).
                                      OrderByDescending(x => x.PublishTime).ToList();

                    if (voyagePlans != null && voyagePlans.Count() > 0)
                    {
                        foreach (var voyagePlan in voyagePlans)
                        {
                            var aclObj = _aclObjectService.Get(x => x.Subscriber.ID == identity.ID && x.MessageID == voyagePlan.MessageID);
                            if (aclObj == null || aclObj.Count() == 0)
                            {
                                _notificationService.Notify(notification);
                                _context.SaveChanges();
                            }
                            else
                            {
                                accessToAnyUVID = true;

                                //check if sub already exists
                                var sub = _subscriptionService.Get(x => x.CallbackEndpoint == callbackEndpoint && x.MessageID == voyagePlan.MessageID && x.SubscriberIdentity.ID == identity.ID).FirstOrDefault();
                                if (sub == null)
                                {
                                    SetSubscription(callbackEndpoint, voyagePlan.MessageID, identity, notification, paramList);
                                }
                            }
                        }
                    }
                    if (!accessToAnyUVID)
                    {
                        throw CreateHttpResponseException(HttpStatusCode.Forbidden, "Authorization failed: ACL");
                    }
                }
                #endregion

                #region UVID
                else
                {
                    var sub = _subscriptionService.Get(x => x.CallbackEndpoint == callbackEndpoint && x.MessageID == uvid && x.SubscriberIdentity.ID == identity.ID).FirstOrDefault();
                    if (sub == null)
                    {
                        //Check if uvid exist
                        var vpCheck = _publishedMessageService.Get(x => x.MessageID == uvid).FirstOrDefault();
                        if (vpCheck == null)
                        {
                            string msg = string.Format("Voyageplan with UVID {0} does not exist.", uvid);
                            log.Debug(msg);

                            _notificationService.Notify(notification);
                            _context.SaveChanges();

                            throw CreateHttpResponseException(HttpStatusCode.NotFound, msg);
                        }
                        // Check if identity has access to UVID
                        var aclId = _aclObjectService.Get(a => a.Subscriber.UID == InstanceContext.CallerOrgId && a.MessageID == uvid).FirstOrDefault();
                        if (aclId == null)
                        {
                            string msg = string.Format("Access to UVID {0} failed for user {1}", uvid, InstanceContext.CallerOrgId);
                            log.Debug(msg);

                            _notificationService.Notify(notification);
                            _context.SaveChanges();

                            throw CreateHttpResponseException(HttpStatusCode.Forbidden, msg);
                        }
                        SetSubscription(callbackEndpoint, uvid, identity, notification, paramList);
                    }
                }

                #endregion

                return(responseObj);
            }
            catch (HttpResponseException ex)
            {
                log.Error(ex.Message, ex);
                _logEventService.LogError(EventNumber.VIS_subscribeToVoyagePlan_request, EventType.Error_internal, paramList,
                                          JsonConvert.SerializeObject(ex.Response, Formatting.Indented));

                throw;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                _logEventService.LogError(EventNumber.VIS_subscribeToVoyagePlan_request, EventType.Error_internal, paramList,
                                          ex.Message);

                string msg = "VIS internal server error. " + ex.Message;
                throw CreateHttpResponseException(HttpStatusCode.InternalServerError, msg);;
            }
        }
        public ResponseObj RemoveAuthorizedIdentitites([FromUri] string dataId,
                                                       [FromBody] List <IdentityDescriptionObject> identityDescriptionObjects)
        {
            log.Info("Incoming request to " + GetCurrentMethod());

            if (dataId == null)
            {
                throw CreateHttpResponseException(HttpStatusCode.BadRequest, "Missing UVID");
            }

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

            if (identityDescriptionObjects == null || identityDescriptionObjects.Count == 0)
            {
                throw CreateHttpResponseException(HttpStatusCode.BadRequest, "No identities to remove");
            }

            try
            {
                foreach (var identityDescriptionObject in identityDescriptionObjects)
                {
                    // Get matching ACL-object from DB
                    var identityToDelete = _identityService.Get(i => i.UID == identityDescriptionObject.IdentityId).FirstOrDefault();
                    if (identityToDelete == null)
                    {
                        log.Info(string.Format("Identity {0} did not exist for messageId {1}.", identityDescriptionObject.IdentityId, dataId));
                        return(new ResponseObj(dataId));
                    }

                    var current = _aclObjectService.Get(x
                                                        => x.MessageID == dataId && x.Subscriber.ID == identityToDelete.ID, includeProperties: "Subscriber").FirstOrDefault();

                    // If it exists, delete it
                    if (current != null)
                    {
                        var subscribers = _subscriptionService.Get(s =>
                                                                   s.MessageID == dataId &&
                                                                   s.SubscriberIdentity.ID == current.Subscriber.ID);

                        _aclObjectService.Delete(current);
                        var msg = string.Format("ACL removed for identity {0} on messageId {1}.", identityDescriptionObject.IdentityId, dataId);
                        log.Info(msg);

                        // Remove subscriber
                        if (subscribers != null)
                        {
                            foreach (var subscriber in subscribers)
                            {
                                msg = string.Format("Subscriber {0} removed on messageId {1}", subscriber.SubscriberIdentity.UID, dataId);
                                _subscriptionService.Delete(subscriber);
                                log.Info(msg);
                            }
                        }
                    }
                }

                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);
            }
        }
        public ResponseObj RemovePublishedMessage([FromUri] string dataId)
        {
            log.Info("Incoming request to " + GetCurrentMethod());

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

            try
            {
                var message = _publishedMessageService.Get(x =>
                                                           x.MessageID == dataId).FirstOrDefault();

                if (message == null)
                {
                    var msg = string.Format("No message found with id {0}.", dataId);
                    log.Info(msg);
                    throw CreateHttpResponseException(HttpStatusCode.NotFound, msg);
                }
                else
                {
                    // Delete subscriptions
                    var subscriptions = _visSubscriptionService.Get(x => x.MessageID == dataId);
                    if (subscriptions != null)
                    {
                        foreach (var subscription in subscriptions)
                        {
                            _visSubscriptionService.Delete(subscription);
                        }
                    }

                    // Delete ACL
                    var acls = _aclObjectService.Get(x => x.MessageID == dataId);
                    if (acls != null)
                    {
                        foreach (var acl in acls)
                        {
                            _aclObjectService.Delete(acl);
                        }
                    }

                    // Delete message
                    _publishedMessageService.Delete(message);
                    var msg = string.Format("Published message with id {0} was removed.", dataId);
                    log.Info(msg);

                    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);
            }
        }
Ejemplo n.º 5
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);
            }
        }