@Override public void updateInstallation(Installation installationToUpdate, Installation postedInstallation) { // copy the "updateable" values: mergeCategories(installationToUpdate, postedInstallation.getCategories()); installationToUpdate.setDeviceToken(postedInstallation.getDeviceToken()); installationToUpdate.setAlias(postedInstallation.getAlias()); installationToUpdate.setDeviceType(postedInstallation.getDeviceType()); installationToUpdate.setOperatingSystem(postedInstallation .getOperatingSystem()); installationToUpdate.setOsVersion(postedInstallation.getOsVersion()); installationToUpdate.setEnabled(postedInstallation.isEnabled()); installationToUpdate.setPlatform(postedInstallation.getPlatform()); // update it: updateInstallation(installationToUpdate); // unsubscribe Android devices from topics that device should no longer be subscribed to if (installationToUpdate.getVariant().getType() == VariantType.ANDROID) { unsubscribeOldTopics(installationToUpdate); } }
@Override public boolean isValid(Installation installation, ConstraintValidatorContext context) { final String deviceToken = installation.getDeviceToken(); if (installation.getVariant() == null || installation.getVariant().getType() == null || deviceToken == null) { return false; } final VariantType type = installation.getVariant().getType(); return isValidDeviceTokenForVariant(deviceToken, type); }
private void storeInstallationAndSetReferences(Variant variant, Installation entity) { // ensure lower case for iOS if (variant.getType() == VariantType.IOS) { entity.setDeviceToken(entity.getDeviceToken().toLowerCase()); } // set reference entity.setVariant(variant); // update attached categories mergeCategories(entity, entity.getCategories()); // store Installation entity installationDao.create(entity); } }
@Override @Asynchronous public void addInstallation(Variant variant, Installation entity) { // does it already exist ? Installation installation = this.findInstallationForVariantByDeviceToken(variant.getVariantID(), entity.getDeviceToken()); // Needed for the Admin UI Only. Help for setting up Routes entity.setPlatform(variant.getType().getTypeName()); // new device/client ? if (installation == null) { logger.trace("Performing new device/client registration"); // store the installation: storeInstallationAndSetReferences(variant, entity); } else { // We only update the metadata, if the device is enabled: if (installation.isEnabled()) { logger.trace("Updating received metadata for an 'enabled' installation"); // fix variant property of installation object installation.setVariant(variant); // update the entity: this.updateInstallation(installation, entity); } } }
/** * A simple validation util that checks if a token is present */ private static boolean hasTokenValue(Installation installation) { return installation.getDeviceToken() != null && !installation.getDeviceToken().isEmpty(); }
@Override @Asynchronous public void unsubscribeOldTopics(Installation installation) { FCMTopicManager topicManager = new FCMTopicManager((AndroidVariant) installation.getVariant()); Set<String> oldCategories = topicManager.getSubscribedCategories(installation); // Remove current categories from the set of old ones oldCategories.removeAll(convertToNames(installation.getCategories())); // Remove global variant topic because we don't want to unsubscribe it oldCategories.remove(installation.getVariant().getVariantID()); for (String categoryName : oldCategories) { topicManager.unsubscribe(installation, categoryName); } }
installation.setDeviceToken(canonicalRegId);
/** * When an installation is created or updated, the categories are passed without IDs. * This method solve this issue by checking for existing categories and updating them (otherwise it would * persist a new object). * @param entity to merge the categories for * @param categoriesToMerge are the categories to merge with the existing one */ private void mergeCategories(Installation entity, Set<Category> categoriesToMerge) { if (entity.getCategories() != null) { final List<String> categoryNames = convertToNames(categoriesToMerge); final List<Category> existingCategoriesFromDB = categoryDao.findByNames(categoryNames); // Replace json dematerialised categories with their persistent counter parts (see Category.equals), // by remove existing/persistent categories from the new collection, and adding them back in (with their PK). categoriesToMerge.removeAll(existingCategoriesFromDB); categoriesToMerge.addAll(existingCategoriesFromDB); // and apply the passed in ones. entity.setCategories(categoriesToMerge); } }
@Override public boolean isValid(Installation installation, ConstraintValidatorContext context) { final String deviceToken = installation.getDeviceToken(); if (installation.getVariant() == null || installation.getVariant().getType() == null || deviceToken == null) { return false; } final VariantType type = installation.getVariant().getType(); return isValidDeviceTokenForVariant(deviceToken, type); }
/** * Unsubscribes device from single category(topic) * * @param installation Installation object containing correct variant property of AndroidVariant class * @param categoryToUnsubscribe category(topic) that device should be unsubscribed from */ public void unsubscribe(Installation installation, String categoryToUnsubscribe) { String url = ""; try { url = IID_URL + installation.getDeviceToken() + "/rel/topics/" + URLEncoder.encode(categoryToUnsubscribe, StandardCharsets.UTF_8.toString()); } catch (UnsupportedEncodingException e1) { // } try { delete(url); } catch (IOException e) { logger.debug("Unregistering device from topic was unsuccessfull"); } }
installation.setDeviceToken(canonicalRegId);
public Set<String> getSubscribedCategories(Installation installation) { String url = IID_URL + "info/" + installation.getDeviceToken() + "?details=true"; String deviceInfo; try { deviceInfo = get(url); } catch (IOException e) { logger.debug("Couldn't get list of subscribed topics from Instance ID service."); return Collections.emptySet(); } JSONParser parser = new JSONParser(); JSONObject info; try { info = (JSONObject) parser.parse(deviceInfo); } catch (ParseException e) { logger.debug("Couldn't parse list of subscribed topics from Instance ID service."); return Collections.emptySet(); } JSONObject rel = (JSONObject) info.get("rel"); if (rel == null){ logger.debug("Couldn't parse rel object Instance ID service."); return Collections.emptySet(); } JSONObject topics = (JSONObject) rel.get("topics"); return topics.keySet(); }
if (!existingTokens.contains(current.getDeviceToken()) && hasTokenValue(current)) { logger.trace("Importing device with token: {}", current.getDeviceToken()); existingTokens.add(current.getDeviceToken()); logger.trace("Device with token '{}' already exists. Ignoring it ", current.getDeviceToken());
final String deviceToken = entity.getDeviceToken(); if (deviceToken == null || !DeviceTokenValidator.isValidDeviceTokenForVariant(deviceToken, variant.getType())) { logger.trace("Invalid device token was delivered: {} for variant type: {}", deviceToken, variant.getType()); if (!oldToken.isEmpty() && !oldToken.equals(entity.getDeviceToken())) { logger.info("Deleting old device token {}", oldToken); clientInstallationService.removeInstallationForVariantByDeviceToken(variant.getVariantID(), oldToken);