Search in sources :

Example 1 with ModuleUpgradeModel

use of com.infiniteautomation.mango.rest.latest.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")
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);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) UnknownHostException(java.net.UnknownHostException) ValidationFailedRestException(com.infiniteautomation.mango.rest.latest.exception.ValidationFailedRestException) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString) IOException(java.io.IOException) ModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel) CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) JsonArray(com.serotonin.json.type.JsonArray) ModuleUpgradeModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradeModel) SocketTimeoutException(java.net.SocketTimeoutException) ModuleUpgradesModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradesModel) Iterator(java.util.Iterator) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) HttpException(org.apache.http.HttpException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) JsonString(com.serotonin.json.type.JsonString) ServerErrorException(com.infiniteautomation.mango.rest.latest.exception.ServerErrorException) Module(com.serotonin.m2m2.module.Module) CoreModule(com.serotonin.m2m2.module.ModuleRegistry.CoreModule) InvalidModule(com.infiniteautomation.mango.rest.latest.model.modules.UpgradeUploadResult.InvalidModule) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)1 ServerErrorException (com.infiniteautomation.mango.rest.latest.exception.ServerErrorException)1 ValidationFailedRestException (com.infiniteautomation.mango.rest.latest.exception.ValidationFailedRestException)1 CoreModuleModel (com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel)1 ModuleModel (com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel)1 ModuleUpgradeModel (com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradeModel)1 ModuleUpgradesModel (com.infiniteautomation.mango.rest.latest.model.modules.ModuleUpgradesModel)1 InvalidModule (com.infiniteautomation.mango.rest.latest.model.modules.UpgradeUploadResult.InvalidModule)1 JsonException (com.serotonin.json.JsonException)1 JsonArray (com.serotonin.json.type.JsonArray)1 JsonObject (com.serotonin.json.type.JsonObject)1 JsonString (com.serotonin.json.type.JsonString)1 JsonValue (com.serotonin.json.type.JsonValue)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 Module (com.serotonin.m2m2.module.Module)1 CoreModule (com.serotonin.m2m2.module.ModuleRegistry.CoreModule)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1