private GetVoyagePlanResponse GetPublishedVoyagePlans(Identity identity, List <KeyValuePair <string, string> > paramList,
                                                              string uvid = null, int?routeStatusInt = null)
        {
            bool accessToAnyUVID = false;

            var result = new Models.GetVoyagePlanResponse(DateTime.UtcNow);

            result.VoyagePlans = new List <Models.VoyagePlan>();
            List <PublishedRtzMessage> publishedVoyagePlans = null;

            //Get all published voyageplans based on parameter values
            if (uvid == null && routeStatusInt == null)
            {
                publishedVoyagePlans = _publishedMessageService.Get(x => (int)x.MessageStatus != 8).
                                       OrderByDescending(x => x.PublishTime).ToList();
            }
            else if (uvid != null && routeStatusInt == null)
            {
                publishedVoyagePlans = _publishedMessageService.Get(x => x.MessageID == uvid &&
                                                                    (int)x.MessageStatus != 8).
                                       OrderByDescending(x => x.PublishTime).ToList();
            }
            else if (uvid == null && routeStatusInt != null)
            {
                publishedVoyagePlans = _publishedMessageService.Get(x => (int)x.MessageStatus != 8 &&
                                                                    (int)x.MessageStatus == routeStatusInt).
                                       OrderByDescending(x => x.PublishTime).ToList();
            }
            else
            {
                publishedVoyagePlans = _publishedMessageService.Get(x => x.MessageID == uvid &&
                                                                    (int)x.MessageStatus == routeStatusInt).
                                       OrderByDescending(x => x.PublishTime).ToList();
            }

            //Need to loop in order to distinguish the VP's with no access from the ones with access
            if (publishedVoyagePlans != null && publishedVoyagePlans.Count() > 0)
            {
                foreach (var publishedVoyagePlan in publishedVoyagePlans)
                {
                    // Now look up if orgId is authorized to this voyageplan
                    var aclObject = _aclObjectService.Get(x => x.Subscriber.ID == identity.ID && x.MessageID == publishedVoyagePlan.MessageID);
                    if (aclObject == null || aclObject.Count() == 0)
                    {
                        //No access to this one, send notification to STM module
                        var msg = "Authorization failed: ACL";
                        log.Debug(msg);

                        //Notify STM Module
                        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            = string.Format("Access denied for identity {0}.", identity.Name);
                        notification.NotificationSource = EnumNotificationSource.VIS;

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

                        // Log error
                        _logEventService.LogError(EventNumber.VIS_getVoyagePlan_request, EventType.Error_authorization,
                                                  paramList, InstanceContext.CallerServiceId);
                        _context.SaveChanges();
                    }
                    else
                    {
                        accessToAnyUVID = true;
                        //Add it to response object
                        var rtzString = Serialization.ByteArrayToString(publishedVoyagePlan.Message);
                        var vp        = new Models.VoyagePlan(rtzString);
                        result.VoyagePlans.Add(vp);
                    }
                }
            }
            else
            {
                //We didn't find any voyageplans i.e. return not found
                throw CreateHttpResponseException(HttpStatusCode.NotFound, "Voyageplans not found");
            }

            //Final check to verify that we did return at least one VP
            if (!accessToAnyUVID)
            {
                throw CreateHttpResponseException(HttpStatusCode.Forbidden, "Authorization failed: ACL");
            }
            return(result);
        }
        public ResponseObj AuthorizeIdentities([FromUri] string dataId,
                                               [FromBody] List <IdentityDescriptionObject> identityDescriptionObjects)
        {
            log.Info("Incoming request to " + GetCurrentMethod());

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

            try
            {
                foreach (var identityObject in identityDescriptionObjects)
                {
                    // CHeck if the identity already exists in the local identity table
                    var identity = _identityService.Get(x => x.UID == identityObject.IdentityId).FirstOrDefault();

                    // If not add new identity
                    if (identity == null)
                    {
                        identity = new Identity
                        {
                            UID  = identityObject.IdentityId,
                            Name = identityObject.IdentityName
                        };

                        _identityService.Insert(identity);
                    }
                    ;

                    // Get ACL for identity and messageID
                    var current = _aclObjectService.Get(x
                                                        => x.MessageID == dataId && x.Subscriber.ID == identity.ID).FirstOrDefault();

                    // If not already exists add new ACL
                    if (current == null)
                    {
                        var acl = new ACLObject();
                        acl.MessageID      = dataId;
                        acl.LastUpdateTime = DateTime.UtcNow;
                        acl.Subscriber     = identity;
                        _aclObjectService.Insert(acl);
                    }

                    log.Info(string.Format("ACL added for identity {0} on messageId {1}", identity.Name, dataId));
                }

                SetLastInteractionTime();

                // Save to DB
                _context.SaveChanges();

                // Create response
                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);
            }
        }
Esempio n. 3
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);
            }
        }
        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);
            }
        }
Esempio 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 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);
            }
        }