@Override public NotificationDto toDto() { NotificationDto dto = new NotificationDto(); dto.setId(id); dto.setApplicationId(applicationId); dto.setSchemaId(schemaId); dto.setTopicId(topicId); dto.setLastTimeModify(lastModifyTime); dto.setNfVersion(nfVersion); dto.setType(type); dto.setBody(getArrayCopy(body)); dto.setExpiredAt(expiredAt); dto.setSecNum(secNum); return dto; } }
@Override public UpdateNotificationDto<NotificationDto> saveNotification(NotificationDto dto) { validateObject(dto, "Can't save notification. Invalid notification object"); dto.setId(null); UpdateNotificationDto<NotificationDto> updateNotificationDto = null; String schemaId = dto.getSchemaId(); String topicId = dto.getTopicId(); if (isNotBlank(schemaId) && isNotBlank(topicId)) { NotificationSchema schema = notificationSchemaDao.findById(schemaId); if (schema != null) { dto.setNfVersion(schema.getVersion()); dto.setApplicationId(schema.getApplicationId()); dto.setType(schema.getType()); } else { throw new DatabaseProcessingException("Can't find notification schema by id " + schemaId); dto.setBody(serializeNotificationBody(dto, schema)); } catch (IOException ex) { LOG.error("Can't serialize notification body using schema. ", ex); Date expiredAt = dto.getExpiredAt(); dto.setExpiredAt(expiredAt != null ? expiredAt : new Date(currentTime + ttl)); dto.setLastTimeModify(new Date(currentTime)); NotificationDto notificationDto = saveNotificationAndIncTopicSecNum(dto); if (notificationDto != null) { updateNotificationDto = new UpdateNotificationDto<NotificationDto>(); updateNotificationDto.setAppId(notificationDto.getApplicationId()); updateNotificationDto.setTopicId(topicId); updateNotificationDto.setPayload(notificationDto);
/** * 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(); }
@Override protected NotificationDto newEntity() { NotificationDto dto = new NotificationDto(); dto.setApplicationId(applicationId); dto.setTopicId(topicId); dto.setType(NotificationTypeDto.USER); return dto; }
NotificationSchemaDto schema; for (int i = 0; i < count; i++) { notification = new NotificationDto(); if (isBlank(schemaId)) { schema = generateNotificationSchemaDto(null, NotificationTypeDto.USER); schema = notificationService.findNotificationSchemaById(schemaId); notification.setApplicationId(schema.getApplicationId()); notification.setSchemaId(schema.getId()); if (isBlank(topicId)) { topicId = generateTopicDto(schema.getApplicationId(), null).getId(); notification.setTopicId(topicId); notification.setType(type != null ? type : NotificationTypeDto.USER); byte[] body = null; try { Assert.fail(e.getMessage()); notification.setBody(body); UpdateNotificationDto<NotificationDto> update = notificationService.saveNotification(notification); notifications.add(update.getPayload());
NotificationDto dto = new NotificationDto(); dto.setSchemaId(savedSchema.getId()); dto.setTopicId(topicDto.getId()); dto.setBody("{\"notificationBody\":\"dummy\", \"systemNotificationParam1\":42, \"systemNotificationParam2\":43}".getBytes()); dto.setExpiredAt(new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10L))); String nid = up.getPayload().getId(); Assert.assertNotNull(nid); try {
@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); } }
/** * 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; }
@Test(expected = NumberFormatException.class) public void testSaveNotificationWithIncorrectIds() { NotificationDto notification = new NotificationDto(); notification.setSchemaId(new ObjectId().toString()); notification.setTopicId(new ObjectId().toString()); notificationService.saveNotification(notification); } }
@Override protected void onSave() { entity.setSchemaId(detailsView.getNotificationSchemaInfo().getValue().getId()); entity.setExpiredAt(detailsView.getExpiredAt().getValue()); }
@Test public void testFindNotificationSchemasByAppIdAndType() { NotificationDto dto = generateNotificationsDto(null, null, 1, null).get(0); generateNotificationSchemaDto(dto.getApplicationId(), NotificationTypeDto.SYSTEM); List<NotificationSchemaDto> schemas = notificationService.findNotificationSchemasByAppIdAndType(dto.getApplicationId(), NotificationTypeDto.SYSTEM); Assert.assertEquals(1, schemas.size()); schemas = notificationService.findNotificationSchemasByAppIdAndType(dto.getApplicationId(), NotificationTypeDto.USER); Assert.assertEquals(2, schemas.size()); }
@Test public void testRemoveById() { NotificationDto notification = generateNotificationsDto(null, null, 1, null).get(0); Assert.assertNotNull(notification.getId()); notificationDao.removeById(notification.getId()); Notification found = notificationDao.findById(notification.getId()); Assert.assertNull(found); }
/** * 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; }
@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()); }
/** * 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()); } } } }
/** * 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; } }
@Test(expected = IncorrectParameterException.class) public void testSaveInvalidNotification() { NotificationDto notification = new NotificationDto(); notificationService.saveNotification(notification); }
protected EndpointNotificationDto generateUnicastNotificationDto(String schemaId, String topicId, byte[] keyHash) { EndpointNotificationDto endpointNotification = new EndpointNotificationDto(); endpointNotification.setEndpointKeyHash(keyHash); NotificationDto notification = new NotificationDto(); NotificationSchemaDto schema; if (isBlank(schemaId)) { schema = generateNotificationSchemaDto(null, NotificationTypeDto.USER); } else { schema = notificationService.findNotificationSchemaById(schemaId); } if (isBlank(topicId)) { topicId = generateTopicDto(schema.getApplicationId(), null).getId(); } notification.setTopicId(topicId); notification.setApplicationId(schema.getApplicationId()); notification.setSchemaId(schema.getId()); notification.setType(NotificationTypeDto.USER); byte[] body = null; try { body = readSchemaFileAsString("dao/schema/testBaseData.json").getBytes(Charset.forName("UTF-8")); } catch (IOException e) { e.printStackTrace(); Assert.fail(e.getMessage()); } notification.setBody(body); endpointNotification.setNotificationDto(notification); UpdateNotificationDto<EndpointNotificationDto> update = notificationService.saveUnicastNotification(endpointNotification); return update.getPayload(); }
@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); } }