Example #1
0
        public MessageEnvelope GetMessageInTimeIntervall(DateTime fromDate, DateTime?toDate = null)
        {
            log.Info("Incoming request to " + GetCurrentMethod());

            try
            {
                if (toDate == null)
                {
                    toDate = DateTime.MaxValue;
                }

                var result = new MessageEnvelope();
                result.Messages = new List <Message>();
                var uploadedMessages = new List <UploadedMessage>();
                var mTypes           = _messageTypeService.Get(m => m.Name == "PCM").ToList();

                uploadedMessages = _uploadedMessageService.GetMessagesInTimeIntervall(fromDate, toDate.Value, mTypes);

                foreach (var uploadedMessage in uploadedMessages)
                {
                    var messageToAdd = new Message();
                    messageToAdd.Id            = uploadedMessage.MessageID;
                    messageToAdd.FromOrgId     = uploadedMessage.FromOrg.UID;
                    messageToAdd.FromOrgName   = uploadedMessage.FromOrg.Name;
                    messageToAdd.FromServiceId = uploadedMessage.FromServiceId;
                    messageToAdd.MessageType   = uploadedMessage.MessageType.Name;
                    messageToAdd.ReceivedAt    = uploadedMessage.ReceiveTime;

                    if (messageToAdd.ReceivedAt.HasValue)
                    {
                        messageToAdd.ReceivedAt = DateTime.SpecifyKind(messageToAdd.ReceivedAt.Value, DateTimeKind.Utc);
                    }

                    var messageBody = Serialization.ByteArrayToString(uploadedMessage.Message);
                    messageToAdd.StmMessage       = new StmMessage(messageBody);
                    messageToAdd.CallbackEndpoint = uploadedMessage.CallbackEndpoint;

                    result.Messages.Add(messageToAdd);
                }

                result.NumberOfMessages          = uploadedMessages.Count;
                result.RemainingNumberOfMessages = _uploadedMessageService.GetNumberOfRemainingMessages(mTypes);

                return(result);
            }
            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);
            }
        }
Example #2
0
        public void TestRTZMessageWithOkRouteStatus()
        {
            byte[] msg         = GetMessageRaw(@"STMMessageSamples\msg1_RouteStatus_7.rtz");
            var    msgString   = Serialization.ByteArrayToString(msg);
            var    parser      = RtzParserFactory.Create(msgString);
            string routeStatus = parser.RouteStatus;

            Assert.AreEqual <string>("7", routeStatus);
        }
        private void Validate(PublishedPcmMessage entity)
        {
            var msg = Serialization.ByteArrayToString(entity.Message);

            var validator = new StmSchemaValidator();

            //validate the XML against its XSD
            validator.ValidatePCMMessageXML(msg);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        public void InsertPCM(UploadedMessage message)
        {
            var msg       = Serialization.ByteArrayToString(message.Message);
            var validator = new StmSchemaValidator();

            validator.ValidatePCMMessageXML(msg);

            base.Insert(message);
        }
Example #5
0
        public void TestRTZMessageMissingRouteStatus()
        {
            byte[] msg         = GetMessageRaw(@"STMMessageSamples\msg1_missingRouteStatus.rtz");
            var    msgString   = Serialization.ByteArrayToString(msg);
            var    parser      = RtzParserFactory.Create(msgString);
            string routeStatus = parser.RouteStatus;

            Assert.AreEqual(routeStatus, string.Empty);
        }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entity"></param>
        public void InsertArea(UploadedMessage entity)
        {
            var msg       = Serialization.ByteArrayToString(entity.Message);
            var validator = new StmSchemaValidator();

            validator.ValidateAreaMessageXML(msg);

            base.Insert(entity);
        }
        private List <Models.VoyagePlan> ConvertToContract(List <PublishedRtzMessage> from)
        {
            List <Models.VoyagePlan> to = new List <Models.VoyagePlan>();

            foreach (var msg in from)
            {
                var msgString        = Serialization.ByteArrayToString(msg.Message);
                Models.VoyagePlan vp = new Models.VoyagePlan(msgString);
                to.Add(vp);
            }
            return(to);
        }
Example #8
0
        public void Rtz10Parser()
        {
            byte[] msg       = GetMessageRaw(@"STMMessageSamples\RTZ v1.0 STM several extensions-1.rtz");
            var    msgString = Serialization.ByteArrayToString(msg);

            var validator = new StmSchemaValidator();

            validator.ValidateRTZMessageXML(msgString);

            var parser = RtzParserFactory.Create(msgString);
            var status = parser.RouteStatus;
        }
Example #9
0
        public void TestRTZ_2_0()
        {
            byte[] msg       = GetMessageRaw(@"STMMessageSamples\RTZ v1.0 STM several extensions-1.rtz");
            var    msgString = Serialization.ByteArrayToString(msg);

            var    parser = RtzParserFactory.Create(msgString);
            string uvid   = parser.VesselVoyage;

            string routeStatus = parser.RouteStatus;

            //Assert.AreEqual("urn:mrn:stm:voyage:id:wallenius:0001", uvid);
        }
Example #10
0
        public void S124Parser()
        {
            byte[] msg       = GetMessageRaw(@"STMMessageSamples\S124_01.gml");
            var    msgString = Serialization.ByteArrayToString(msg);

            var validator = new StmSchemaValidator();

            validator.ValidateAreaMessageXML(msgString);

            var parser = new S124Parser(msgString);
            var id     = parser.AreaMessageId;
        }
Example #11
0
        public void TxtParser()
        {
            byte[] msg       = GetMessageRaw(@"STMMessageSamples\textMessage with area-1.xml");
            var    msgString = Serialization.ByteArrayToString(msg);

            var validator = new StmSchemaValidator();

            validator.ValidateTextMessageXML(msgString);

            var parser = new TxtParser(msgString);
            var id     = parser.TextMessageId;
        }
Example #12
0
        public void Rtz11Parser()
        {
            byte[] msg       = GetMessageRaw(@"STMMessageSamples\rtz_route_stm_1_1_28032017.rtz");
            var    msgString = Serialization.ByteArrayToString(msg);

            var validator = new StmSchemaValidator();

            validator.ValidateRTZMessageXML(msgString);

            var parser = new Rtz11Parser(msgString);
            var status = parser.RouteStatus;
        }
        private void Validate(PublishedRtzMessage entity)
        {
            var msg = Serialization.ByteArrayToString(entity.Message);

            var validator = new StmSchemaValidator();

            //validate the XML against its XSD
            validator.ValidateRTZMessageXML(msg);

            //validate the message content according to our business rules
            validator.ValidateRTZMessage(msg);

            //Validate UVID between header and RTZ
            validator.ValidateUVID(entity.MessageID, msg);
        }
Example #14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entity"></param>
        public void InsertRTZ(UploadedMessage entity)
        {
            var msg       = Serialization.ByteArrayToString(entity.Message);
            var validator = new StmSchemaValidator();

            //validate the XML against its XSD
            validator.ValidateRTZMessageXML(msg);

            //validate the message content according to our business rules
            validator.ValidateRTZMessage(msg);

            //Validate UVID between header and RTZ
            validator.ValidateUVID(entity.MessageID, msg);

            base.Insert(entity);
        }
Example #15
0
        public void TestRTZMessageInincosistentUVID()
        {
            byte[] msg       = GetMessageRaw(@"STMMessageSamples\msg1_invalidUVID.rtz");
            var    msgString = Serialization.ByteArrayToString(msg);
            var    parser    = RtzParserFactory.Create(msgString);
            string uvid      = parser.VesselVoyage;
            var    validator = new StmSchemaValidator();

            try
            {
                validator.ValidateUVID("urn:mrn:stm:voyage:id:vis1:0001", msgString);
            }
            catch (StmSchemaValidationException ex)
            {
                Assert.IsNotNull(ex);
            }
        }
        private void Populate(PublishedRtzMessage entity, bool ignoreStatusInMessage = false)
        {
            var msg = Serialization.ByteArrayToString(entity.Message);

            var parser            = RtzParserFactory.Create(msg);
            var routeStatusString = parser.RouteStatus;

            RouteStatus routeStatus = RouteStatus.Unknown;
            int         statusId;

            if (int.TryParse(routeStatusString, out statusId))
            {
                routeStatus = (RouteStatus)statusId;
            }

            if (!ignoreStatusInMessage)
            {
                entity.MessageStatus = (RouteStatus)statusId;
            }

            DateTime?validityPeriodStart = parser.ValidityPeriodStart;

            if (validityPeriodStart == DateTime.MinValue)
            {
                entity.MessageValidFrom = null;
            }
            else
            {
                entity.MessageValidFrom = validityPeriodStart;
            }
            DateTime?validityPeriodStop = parser.ValidityPeriodStop;

            if (validityPeriodStop == DateTime.MinValue)
            {
                entity.MessageValidTo = null;
            }
            else
            {
                entity.MessageValidTo = validityPeriodStop;
            }
        }
Example #17
0
        private void SubmitMessageToVIS(byte[] msg)
        {
            PublishedRtzMessageService service = new PublishedRtzMessageService(_dbContext, _logContext);

            var messageType = _dbContext.MessageType.Where(m => m.Name == "RTZ").FirstOrDefault();
            //var parsedMsg = Serialization.Deserialize<Route>(Serialization.ByteArrayToString(msg));

            var         parser      = RtzParserFactory.Create(Serialization.ByteArrayToString(msg));
            string      routeStatus = parser.RouteStatus;
            RouteStatus status;

            if (string.IsNullOrEmpty(routeStatus))
            {
                status = RouteStatus.Unknown;
            }
            else
            {
                status = (RouteStatus)Enum.Parse(typeof(RouteStatus), routeStatus);
            }
            var entityToInsert = new PublishedRtzMessage
            {
                Message               = msg,
                MessageID             = "urn:mrn:stm:voyage:id:vis1:0001",
                MessageLastUpdateTime = DateTime.UtcNow,
                MessageStatus         = status,
                MessageType           = messageType,
                MessageValidFrom      = DateTime.UtcNow.AddDays(-3),
                MessageValidTo        = DateTime.UtcNow.AddDays(3),
                PublishTime           = DateTime.UtcNow.AddHours(-3)
            };

            try
            {
                service.Insert(entityToInsert);
                _dbContext.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
        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);
        }
Example #19
0
        public MessageEnvelope GetMessage(string limitQuery = null)
        {
            log.Info("Incoming request to " + GetCurrentMethod());

            int limitNumberOfMessages = 0;

            if (int.TryParse(limitQuery, out limitNumberOfMessages))
            {
                log.Info(string.Format("LimitQuery parsed successfully to {0}.", limitNumberOfMessages));
            }

            try
            {
                var result = new MessageEnvelope();
                result.Messages = new List <Message>();
                var uploadedMessages = new List <UploadedMessage>();
                var mTypes           = _messageTypeService.Get(m => m.Name == "PCM").ToList();

                if (limitNumberOfMessages > 0)
                {
                    uploadedMessages = _uploadedMessageService.GetMessagesByLimitNumber(limitNumberOfMessages, mTypes);
                }
                else
                {
                    uploadedMessages = _uploadedMessageService.GetAllUnFetchedMessages(mTypes);
                }

                foreach (var uploadedMessage in uploadedMessages)
                {
                    uploadedMessage.FetchedByShip = true;
                    uploadedMessage.FetchTime     = DateTime.UtcNow;
                    _uploadedMessageService.Update(uploadedMessage);

                    var messageToAdd = new Message();
                    messageToAdd.FromOrgId     = uploadedMessage.FromOrg.UID;
                    messageToAdd.FromOrgName   = uploadedMessage.FromOrg.Name;
                    messageToAdd.FromServiceId = uploadedMessage.FromServiceId;
                    messageToAdd.MessageType   = uploadedMessage.MessageType.Name;
                    messageToAdd.ReceivedAt    = uploadedMessage.ReceiveTime;
                    var messageBody = Serialization.ByteArrayToString(uploadedMessage.Message);
                    messageToAdd.StmMessage       = new StmMessage(messageBody);
                    messageToAdd.Id               = string.Empty;
                    messageToAdd.CallbackEndpoint = uploadedMessage.CallbackEndpoint;

                    result.Messages.Add(messageToAdd);
                }
                _context.SaveChanges();

                result.NumberOfMessages          = uploadedMessages.Count;
                result.RemainingNumberOfMessages = _uploadedMessageService.GetNumberOfRemainingMessages(mTypes);

                _uploadedMessageService.SendAck(uploadedMessages);

                _context.SaveChanges();
                return(result);
            }
            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);
            }
        }