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); }
private VisSubscription ConvertToEntity(SubscriptionObject subscriptionObject, string dataId) { var to = new VisSubscription(); to.MessageID = dataId; to.IsAuthorized = true; to.CallbackEndpoint = subscriptionObject.EndpointURL.ToString(); to.MessageType = _messageTypeService.Get(x => x.Name.ToLower() == "rtz").FirstOrDefault(); to.SubscriberIdentity = _identityService.Get(x => x.UID == subscriptionObject.IdentityId).FirstOrDefault(); return(to); }
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); } }
public virtual ResponseObj UploadPCMMessage([FromBody] string pcmMessageObject, [FromUri] string deliveryAckEndPoint = null) { log.Info("Incoming request to " + GetCurrentMethod()); var messageType = _messageTypeService.Get(x => x.Name.ToLower() == "pcm").First(); var parser = new PcmParser(pcmMessageObject); string dataId = parser.PcmMessageId; var errorMsgResponse = string.Empty; var request = Request; var headers = request.Headers; if (string.IsNullOrEmpty(pcmMessageObject)) { log.Debug("PCM Message is empty"); errorMsgResponse += "Required parameter pcmMessageObject is missing." + Environment.NewLine; throw CreateHttpResponseException(HttpStatusCode.BadRequest, "Required parameter pcmMessageObject is missing."); } var param = new KeyValuePair <string, string>("deliveryAckEndPoint", deliveryAckEndPoint); var paramList = new List <KeyValuePair <string, string> >(); paramList.Add(param); try { if (string.IsNullOrEmpty(InstanceContext.CallerOrgId)) { log.Debug("Calling organization identity missing in header."); errorMsgResponse += "Calling organization identity missing in header." + Environment.NewLine; throw CreateHttpResponseException(HttpStatusCode.BadRequest, "Required header incomingOrganizationId is missing."); } //Write to log table _logEventService.LogInfo(EventNumber.SPIS_uploadPCM_request, EventDataType.PCM, paramList, pcmMessageObject); // Get identity ether from internal id talbe or from id registry var identity = _identityService.GetCallerIdentity(); var result = new UploadedMessage(); result.AckDelivered = false; result.DeliveryAckEndpoint = deliveryAckEndPoint; result.DeliveryAckReqested = string.IsNullOrEmpty(deliveryAckEndPoint) ? false : true; result.FetchedByShip = false; result.FetchTime = null; result.FromOrg = identity; result.FromServiceId = InstanceContext.CallerServiceId; result.Message = Serialization.StrToByteArray(pcmMessageObject); result.MessageType = messageType; result.Notified = false; result.ReceiveTime = DateTime.UtcNow; result.MessageID = dataId; //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.MESSAGE_WAITING; notification.Subject = "PCM message uploaded."; notification.NotificationSource = EnumNotificationSource.SPIS; result.Notified = _notificationService.Notify(notification); _uploadedMessageService.InsertPCM(result); var responsObj = new ResponseObj("Success store message"); //Save to DB _context.SaveChanges(); _logEventService.LogSuccess(EventNumber.SPIS_uploadPCM_response, EventDataType.None, paramList, JsonConvert.SerializeObject(responsObj, Formatting.Indented)); return(responsObj); } catch (HttpResponseException ex) { var responseString = JsonConvert.SerializeObject(ex.Response, Formatting.Indented); log.Error(ex.Message, ex); _logEventService.LogError(EventNumber.SPIS_uploadPCM_request, EventType.Error_internal, paramList, responseString); throw; } catch (DbEntityValidationException dbEx) { var response = request.CreateResponse(HttpStatusCode.InternalServerError); StringBuilder sb = new StringBuilder(); foreach (var item in dbEx.EntityValidationErrors) { sb.Append(item + " errors: "); foreach (var i in item.ValidationErrors) { sb.Append(i.PropertyName + " : " + i.ErrorMessage); } sb.Append(Environment.NewLine); } string msg = "SPIS internal server error. " + dbEx.Message; _logEventService.LogError(EventNumber.SPIS_uploadPCM_request, EventType.Error_internal, paramList, dbEx.Message); log.ErrorFormat("Validation errors: {0}", sb.ToString()); throw CreateHttpResponseException(HttpStatusCode.InternalServerError, sb.ToString()); } catch (Exception ex) { log.Error(ex.Message, ex); _logEventService.LogError(EventNumber.SPIS_uploadPCM_request, EventType.Error_internal, paramList, ex.Message); string msg = "SPIS internal server error. " + ex.Message; throw CreateHttpResponseException(HttpStatusCode.InternalServerError, msg); } }
public ResponseObj PublishMessage([FromUri] string dataId, [FromUri] string messageType, [FromBody] string message) { try { // Check incoming args if (!FormatValidation.IsValidUvid(dataId)) { throw CreateHttpResponseException(HttpStatusCode.BadRequest, string.Format("Invalid UVID format.")); } if (string.IsNullOrEmpty(dataId)) { throw CreateHttpResponseException(HttpStatusCode.BadRequest, string.Format("Missing required parameter dataId.")); } if (string.IsNullOrEmpty(messageType)) { throw CreateHttpResponseException(HttpStatusCode.BadRequest, string.Format("Missing required parameter messageType.")); } if (string.IsNullOrEmpty(message)) { throw CreateHttpResponseException(HttpStatusCode.BadRequest, string.Format("Missing required parameter message.")); } // Check if correct messageType e.g. only RTZ accepted var msgType = _messageTypeService.Get(m => m.Name == messageType).FirstOrDefault(); if (msgType == null || string.IsNullOrEmpty(msgType.Name) || msgType.Name.ToLower() != "rtz") { throw CreateHttpResponseException(HttpStatusCode.BadRequest, string.Format("Unsupported or unknown message type.")); } var messageToUpdate = _publishedMessageService.Get(m => m.MessageID == dataId).FirstOrDefault(); var responsObj = new ResponseObj(dataId); CheckRouteStatus(message, dataId); if (messageToUpdate == null) //New message { var newMessage = new PublishedRtzMessage(); newMessage.Message = Serialization.StrToByteArray(message); newMessage.MessageID = dataId; newMessage.MessageType = msgType; newMessage.PublishTime = DateTime.UtcNow; _publishedMessageService.Insert(newMessage); } else { messageToUpdate.Message = Serialization.StrToByteArray(message); messageToUpdate.PublishTime = DateTime.UtcNow; _publishedMessageService.Update(messageToUpdate); } //Notify subscribers if messageStatus not inactive var parser = RtzParserFactory.Create(message); string routeInfo = parser.RouteInfo; string routeStatus = parser.RouteStatus; if (!string.IsNullOrEmpty(routeInfo) && !string.IsNullOrEmpty(routeStatus)) { _publishedMessageService.SendMessageToSubsribers(message, dataId); } SetLastInteractionTime(); // Save to DB _context.SaveChanges(); return(responsObj); } catch (HttpResponseException ex) { log.Error(ex.Message, ex); throw; } catch (Exception ex) { log.Error(ex.Message, ex); var errorMessage = "VIS internal server error. " + ex.Message; throw CreateHttpResponseException(HttpStatusCode.InternalServerError, errorMessage); } }
public ResponseObj PublishMessage([FromUri] string dataId, [FromUri] string messageType, [FromBody] string message) { log.Info("Incoming request to " + GetCurrentMethod()); //_logEventService.Init(EventDataType.PCM); var paramList = new List <KeyValuePair <string, string> >(); var param = new KeyValuePair <string, string>("dataId", dataId); paramList.Add(param); param = new KeyValuePair <string, string>("messageType", messageType); paramList.Add(param); _logEventService.LogInfo(EventNumber.SPIS_stateUpdate_request, EventDataType.PCM, paramList, message); try { // Check incoming args if (string.IsNullOrEmpty(dataId)) { throw CreateHttpResponseException(HttpStatusCode.BadRequest, string.Format("Missing required parameter dataId.")); } if (string.IsNullOrEmpty(messageType)) { throw CreateHttpResponseException(HttpStatusCode.BadRequest, string.Format("Missing required parameter messageType.")); } if (string.IsNullOrEmpty(message)) { throw CreateHttpResponseException(HttpStatusCode.BadRequest, string.Format("Missing required parameter message.")); } // Check if correct messageType e.g. only PCM accepted var msgType = _messageTypeService.Get(m => m.Name == messageType).FirstOrDefault(); if (msgType == null || string.IsNullOrEmpty(msgType.Name) || msgType.Name.ToLower() != "pcm") { throw CreateHttpResponseException(HttpStatusCode.BadRequest, string.Format("Unsupported or unknown message type.")); } var messageToUpdate = _publishedMessageService.Get(m => m.MessageID == dataId).FirstOrDefault(); var responsObj = new ResponseObj(dataId); if (messageToUpdate == null) //New message { var newMessage = new PublishedPcmMessage(); newMessage.Message = Serialization.StrToByteArray(message); newMessage.MessageID = dataId; newMessage.MessageType = msgType; newMessage.PublishTime = DateTime.UtcNow; _publishedMessageService.Insert(newMessage); } else { messageToUpdate.Message = Serialization.StrToByteArray(message); messageToUpdate.MessageLastUpdateTime = DateTime.UtcNow; _publishedMessageService.Update(messageToUpdate); } _publishedMessageService.SendMessageToSubsribers(message, dataId); // Save to DB _context.SaveChanges(); var responseString = JsonConvert.SerializeObject(responsObj, Formatting.Indented); _logEventService.LogSuccess(EventNumber.VIS_publishMessage, EventDataType.None, null, responseString); return(responsObj); } catch (HttpResponseException ex) { log.Error(ex.Message, ex); _logEventService.LogError(EventNumber.SPIS_stateUpdate_request, EventType.Error_internal, paramList, ex.Response.ToString()); throw; } catch (Exception ex) { log.Error(ex.Message, ex); var errorMessage = "SPIS internal server error. " + ex.Message; _logEventService.LogError(EventNumber.SPIS_stateUpdate_request, EventType.Error_internal, paramList, ex.Message); throw CreateHttpResponseException(HttpStatusCode.InternalServerError, errorMessage); } }
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); } }