Ejemplo n.º 1
0
        public ResponseObj RemoveSubscriptions([FromUri] string dataId, [FromBody] List <SubscriptionObject> subscriptionObjects)
        {
            log.Info("Incoming request to " + GetCurrentMethod());

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

            try
            {
                foreach (var subscriptionObject in subscriptionObjects)
                {
                    var identityToDelete = _identityService.Get(i => i.UID == subscriptionObject.IdentityId).FirstOrDefault();
                    if (identityToDelete == null)
                    {
                        log.Info(string.Format("Identity {0} did not exist for messageId {1}.", subscriptionObject.IdentityId, dataId));
                        return(new ResponseObj(dataId));
                    }

                    var url        = subscriptionObject.EndpointURL.ToString().ToLower();
                    var currentSub = _subscriptionService.Get(s => s.MessageID == dataId &&
                                                              s.SubscriberIdentity.UID == subscriptionObject.IdentityId &&
                                                              s.CallbackEndpoint.ToLower() == url).FirstOrDefault();

                    if (currentSub != null)
                    {
                        //Delete it
                        _subscriptionService.Delete(currentSub);
                        var msg = string.Format("Subscription removed for identity {0} on messageId {1}.", subscriptionObject.IdentityId, 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);
            }
        }
        public virtual Models.ResponseObj RemoveVoyagePlanSubscription([FromUri] string callbackEndpoint, [FromUri] string uvid = null)
        {
            log.Info("Incoming request to " + GetCurrentMethod());

            var request = Request;
            var headers = request.Headers;

            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);

            // First, validate that we have mandatory in-parameters
            if (uvid != null && !FormatValidation.IsValidUvid(uvid))
            {
                throw CreateHttpResponseException(HttpStatusCode.BadRequest, "Invalid UVID format");
            }

            if (string.IsNullOrEmpty(callbackEndpoint))
            {
                log.Debug("Callback endpoint address is empty");
                throw CreateHttpResponseException(HttpStatusCode.BadRequest, "Required parameter CallbackEndpoint is missing.");
            }

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

                // Write to log table
                _logEventService.LogInfo(EventNumber.VIS_removeSubscribeToVoyagePlan_request, EventDataType.None, paramList, null);

                // Get identity ether from internal id talbe or from id registry
                var identity = _identityService.GetCallerIdentity();

                var subscriptionsToDelete = new List <VisSubscription>();

                if (uvid == null)
                {
                    subscriptionsToDelete = _subscriptionService.Get(s => s.SubscriberIdentity.ID == identity.ID &&
                                                                     s.CallbackEndpoint == callbackEndpoint).ToList();
                }
                else
                {
                    subscriptionsToDelete = _subscriptionService.Get(s => s.SubscriberIdentity.ID == identity.ID &&
                                                                     s.MessageID == uvid &&
                                                                     s.CallbackEndpoint == callbackEndpoint).ToList();
                }

                if (subscriptionsToDelete != null &&
                    subscriptionsToDelete.Count > 0)
                {
                    foreach (var subscription in subscriptionsToDelete)
                    {
                        _subscriptionService.Delete(subscription.ID);

                        //Save to DB
                        _context.SaveChanges();
                    }
                }
                else
                {
                    string msg = string.Format("Subscription not found for UVID {0} and user {1} with endpoint {2}", uvid, InstanceContext.CallerOrgId, callbackEndpoint);
                    log.Debug(msg);

                    throw CreateHttpResponseException(HttpStatusCode.NotFound, msg);
                }

                var responseObj = new ResponseObj("Success delete subscription(s).");
                _logEventService.LogSuccess(EventNumber.VIS_removeSubscribeToVoyagePlan_response, EventDataType.Other, paramList,
                                            JsonConvert.SerializeObject(responseObj, Formatting.Indented));

                return(responseObj);
            }
            catch (HttpResponseException ex)
            {
                log.Error(ex.Message, ex);
                _logEventService.LogError(EventNumber.VIS_removeSubscribeToVoyagePlan_request, EventType.Error_internal, paramList,
                                          JsonConvert.SerializeObject(ex.Response, Formatting.Indented));
                throw;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                _logEventService.LogError(EventNumber.VIS_removeSubscribeToVoyagePlan_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);
            }
        }