use of com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradesModel in project ma-modules-public by infiniteautomation.
the class ModulesRestController method getUpgrades.
@ApiOperation(value = "Get Available Upgrades", notes = "Check the store for Upgrades")
@RequestMapping(method = RequestMethod.GET, value = "/upgrades-available")
public ModuleUpgradesModel getUpgrades(@AuthenticationPrincipal PermissionHolder user) {
permissionService.ensureAdminRole(user);
// Do the check
try {
JsonValue jsonResponse = null;
// This is handled oddly in the ModulesDwr so check it here for validation
String baseUrl = Common.envProps.getString("store.url");
if (!StringUtils.isEmpty(baseUrl)) {
jsonResponse = service.getAvailableUpgrades();
} else {
ProcessResult result = new ProcessResult();
result.addGenericMessage("modules.versionCheck.storeNotSet");
throw new ValidationFailedRestException(result);
}
if (jsonResponse == null) {
// Indicates that the store url is not set, which we check for above so this really means the response was a null JSON value
throw new BadRequestException(new TranslatableMessage("modules.versionCheck.storeResponseEmpty"));
} else if (jsonResponse instanceof JsonString) {
throw new ServerErrorException(new TranslatableMessage("common.default", jsonResponse.toString()));
} else {
List<ModuleUpgradeModel> upgrades = new ArrayList<>();
List<ModuleUpgradeModel> newInstalls = new ArrayList<>();
List<ModuleModel> unavailableModules = new ArrayList<>();
ModuleUpgradesModel model = new ModuleUpgradesModel(upgrades, newInstalls);
JsonObject root = jsonResponse.toJsonObject();
JsonValue jsonUpgrades = root.get("upgrades");
JsonArray jsonUpgradesArray = jsonUpgrades.toJsonArray();
Iterator<JsonValue> it = jsonUpgradesArray.iterator();
while (it.hasNext()) {
JsonValue v = it.next();
if (v.getJsonValue("name") == null) {
it.remove();
continue;
}
String name = v.getJsonValue("name").toString();
Module module = ModuleRegistry.getModule(name);
if (module == null) {
it.remove();
continue;
}
upgrades.add(new ModuleUpgradeModel(module, v));
}
JsonValue jsonInstalls = root.get("newInstalls");
JsonArray jsonInstallsArray = jsonInstalls.toJsonArray();
for (JsonValue v : jsonInstallsArray) {
newInstalls.add(new ModuleUpgradeModel(v));
}
// Extract any unavailable modules
if (root.containsKey("unavailableModules")) {
JsonValue jsonUnavailableModules = root.get("unavailableModules");
JsonArray jsonUnavailableModulesArray = jsonUnavailableModules.toJsonArray();
List<Module> modules = ModuleRegistry.getModules();
for (JsonValue v : jsonUnavailableModulesArray) {
Module unavailable = null;
String moduleName = v.toString();
for (Module module : modules) {
if (StringUtils.equals(module.getName(), moduleName)) {
unavailable = module;
break;
}
}
if (unavailable == null) {
// Didn't find it? Store must be wrong?
LOG.warn("Store reported unavailable module " + moduleName + " but it isn't installed.");
} else {
unavailableModules.add(new ModuleModel(unavailable));
}
}
}
model.setUnavailableModules(unavailableModules);
return model;
}
} catch (SocketTimeoutException e) {
throw new ServerErrorException(new TranslatableMessage("rest.error.requestTimeout", Common.envProps.getString("store.url")));
} catch (UnknownHostException e) {
throw new ServerErrorException(new TranslatableMessage("rest.error.unknownHost", Common.envProps.getString("store.url")));
} catch (IOException | JsonException | HttpException e) {
throw new ServerErrorException(e);
}
}
Aggregations