use of com.serotonin.m2m2.web.mvc.rest.v1.model.modules.ModuleUpgradeModel 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", produces = { "application/json" })
public ResponseEntity<ModuleUpgradesModel> getUpgrades(HttpServletRequest request) {
RestProcessResult<ModuleUpgradesModel> result = new RestProcessResult<ModuleUpgradesModel>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
if (Permissions.hasAdmin(user)) {
// Do the check
try {
JsonValue jsonResponse = ModulesDwr.getAvailableUpgrades();
if (jsonResponse instanceof JsonString) {
result.addRestMessage(getInternalServerErrorMessage(jsonResponse.toString()));
} else {
List<ModuleUpgradeModel> upgrades = new ArrayList<>();
List<ModuleUpgradeModel> newInstalls = 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 = "core".equals(name) ? ModuleRegistry.getCoreModule() : 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));
}
return result.createResponseEntity(model);
}
} catch (SocketTimeoutException e) {
result.addRestMessage(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("rest.error.requestTimeout", Common.envProps.getString("store.url")));
} catch (UnknownHostException e) {
result.addRestMessage(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("rest.error.unknownHost", Common.envProps.getString("store.url")));
} catch (Exception e) {
result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
}
} else {
result.addRestMessage(this.getUnauthorizedMessage());
}
}
return result.createResponseEntity();
}
Aggregations