/** * Returns notifications that belongs to subscribed topics. * * @param notifications notifications * @return notifications that belongs to subscribed topics */ public List<NotificationDto> filter(List<NotificationDto> notifications) { List<NotificationDto> list = new ArrayList<>(notifications.size()); for (NotificationDto nf : notifications) { if (subscriptionStates.containsKey(nf.getTopicId())) { list.add(nf); } else { LOG.trace("[{}][{}] Notification {} is no longer valid due to subscription state", endpointKey, actorKey, nf); } } return list; } }
/** * Sends a notification and increments a topic sequence number. * * @param dto notification * @return saved notification */ public NotificationDto saveNotificationAndIncTopicSecNum(NotificationDto dto) { NotificationDto notificationDto = null; Topic topic = topicDao.getNextSeqNumber(dto.getTopicId()); if (topic != null) { dto.setSecNum(topic.getSequenceNumber()); Notification savedDto = notificationDao.save(dto); notificationDto = savedDto != null ? savedDto.toDto() : null; } else { LOG.warn("Can't find topic by id."); } return notificationDto; }
/** * Broadcast to all endpoints. * * @param message the message */ private void broadcastToAllEndpoints(ThriftNotificationMessage message) { String notificationId = message.getNotification().getNotificationId(); NotificationDto notificationDto = notificationService.findNotificationById(notificationId); if (notificationDto == null) { LOG.warn("Can't find notification by id {}. Probably it has already expired!"); } else { notificationCache.put(notificationDto.getSecNum(), notificationDto); LOG.debug("[{}] Put notification to topic actor cache {}", notificationDto.getTopicId(), notificationDto); NotificationMessage notificationMessage = NotificationMessage.fromNotifications( Collections.singletonList(notificationDto)); for (ActorInfo endpoint : endpointSessions.values()) { if (isSchemaVersionMatch( notificationDto, endpoint.getSystemNfVersion(), endpoint.getUserNfVersion())) { endpoint.getActorRef().tell(notificationMessage, self()); } } } }
@Test public void testFindNotificationsByTopicIdAndVersionAndStartSecNum() { NotificationDto dto = generateNotificationsDto(null, null, 3, NotificationTypeDto.USER).get(0); String topicId = dto.getTopicId(); List<NotificationDto> notifications = notificationService.findNotificationsByTopicIdAndVersionAndStartSecNum(topicId, 0, 1, dto.getNfVersion()); Assert.assertFalse(notifications.isEmpty()); Assert.assertEquals(3, notifications.size()); }
@Test public void testFindNotificationsByTopicId() { NotificationDto notification = generateNotificationsDto(null, null, 1, null).get(0); Assert.assertNotNull(notification); NotificationDto found = notificationService.findNotificationsByTopicId(notification.getTopicId()).get(0); Assert.assertEquals(notification, found); }
UpdateNotificationDto<NotificationDto> updateNotificationDto = null; String schemaId = dto.getSchemaId(); String topicId = dto.getTopicId(); if (isNotBlank(schemaId) && isNotBlank(topicId)) { NotificationSchema schema = notificationSchemaDao.findById(schemaId);
/** * Create new instance of <code>CassandraNotification</code>. * * @param dto data transfer object contain data that * assign on fields of new instance */ public CassandraNotification(NotificationDto dto) { this.applicationId = dto.getApplicationId(); this.schemaId = dto.getSchemaId(); this.topicId = dto.getTopicId(); this.type = dto.getType(); this.nfVersion = dto.getNfVersion(); this.seqNum = dto.getSecNum(); this.lastModifyTime = dto.getLastTimeModify(); this.body = getByteBuffer(dto.getBody()); this.expiredAt = dto.getExpiredAt(); this.seqNum = dto.getSecNum(); this.id = dto.getId(); if (isBlank(id)) { generateId(); } }
NotificationDto notificationDto = dto.getNotificationDto(); String schemaId = notificationDto.getSchemaId(); String topicId = notificationDto.getTopicId(); if (isBlank(schemaId)) { throw new IncorrectParameterException("Invalid notification schema id: " + schemaId); NotificationDto savedDto = unicast.getNotificationDto(); updateNotificationDto.setAppId(savedDto.getApplicationId()); updateNotificationDto.setTopicId(savedDto.getTopicId()); updateNotificationDto.setPayload(unicast);
/** * Create new instance of <code>MongoNotification</code>. * @param dto data transfer object contain data that * assign on fields of new instance */ public MongoNotification(NotificationDto dto) { this.id = dto.getId(); this.applicationId = dto.getApplicationId(); this.schemaId = dto.getSchemaId(); this.topicId = dto.getTopicId(); this.nfVersion = dto.getNfVersion(); this.lastModifyTime = dto.getLastTimeModify(); this.type = dto.getType(); this.body = getArrayCopy(dto.getBody()); this.expiredAt = dto.getExpiredAt(); this.secNum = dto.getSecNum(); }
/** * Convert notification. * * @param notificationDto the notification dto * @return the notification */ private static Notification convertNotification(NotificationDto notificationDto) { Notification notification = new Notification(); notification.setBody(ByteBuffer.wrap(notificationDto.getBody())); notification.setTopicId(notificationDto.getTopicId()); switch (notificationDto.getType()) { case SYSTEM: notification.setType(NotificationType.SYSTEM); break; case USER: notification.setType(NotificationType.CUSTOM); break; default: break; } if (notificationDto.getSecNum() >= 0) { notification.setSeqNumber(notificationDto.getSecNum()); } else { // unicast notification notification.setUid(notificationDto.getId()); } return notification; }
/** * Create new instance of <code>CassandraTopicListEntry</code>. * * @param dto data transfer object contain data that assign on fields of new instance */ public CassandraEndpointNotification(EndpointNotificationDto dto) { this.endpointKeyHash = ByteBuffer.wrap(dto.getEndpointKeyHash()); NotificationDto notificationDto = dto.getNotificationDto(); if (notificationDto != null) { this.seqNum = notificationDto.getSecNum(); this.type = notificationDto.getType(); this.applicationId = notificationDto.getApplicationId(); this.schemaId = notificationDto.getSchemaId(); this.nfVersion = notificationDto.getNfVersion(); this.lastModifyTime = notificationDto.getLastTimeModify(); this.body = getByteBuffer(notificationDto.getBody()); this.expiredAt = notificationDto.getExpiredAt(); this.topicId = notificationDto.getTopicId(); } this.id = dto.getId() != null ? dto.getId() : generateId(); }
@Override public NotificationDto sendNotification(NotificationDto notification, byte[] body) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkExpiredDate(notification); notification.setBody(body); checkApplicationId(notification.getApplicationId()); TopicDto topic = controlService.getTopic(notification.getTopicId()); Utils.checkNotNull(topic); checkApplicationId(topic.getApplicationId()); return controlService.editNotification(notification); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public EndpointNotificationDto sendUnicastNotification(NotificationDto notification, String clientKeyHash, byte[] body) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkExpiredDate(notification); notification.setBody(body); checkApplicationId(notification.getApplicationId()); TopicDto topic = controlService.getTopic(notification.getTopicId()); Utils.checkNotNull(topic); checkApplicationId(topic.getApplicationId()); EndpointNotificationDto unicastNotification = new EndpointNotificationDto(); unicastNotification.setEndpointKeyHash( Base64.decode(clientKeyHash.getBytes(Charsets.UTF_8))); unicastNotification.setNotificationDto(notification); return controlService.editUnicastNotification(unicastNotification); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public void sendNotification(NotificationDto notification, RecordField notificationData) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkExpiredDate(notification); GenericRecord record = FormAvroConverter.createGenericRecordFromRecordField( notificationData); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>( record.getSchema()); byte[] body = converter.encodeToJsonBytes(record); notification.setBody(body); checkApplicationId(notification.getApplicationId()); TopicDto topic = controlService.getTopic(notification.getTopicId()); Utils.checkNotNull(topic); checkApplicationId(topic.getApplicationId()); controlService.editNotification(notification); } catch (Exception ex) { throw Utils.handleException(ex); } }