/** * A simple validation util that checks if a token is present */ private static boolean hasTokenValue(Installation installation) { return installation.getDeviceToken() != null && !installation.getDeviceToken().isEmpty(); }
/** * 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"); } }
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);
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 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); }
@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); }
@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); } } }
@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); } }