Search in sources :

Example 6 with ServerErrorException

use of com.infiniteautomation.mango.rest.latest.exception.ServerErrorException 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)

Example 7 with ServerErrorException

use of com.infiniteautomation.mango.rest.latest.exception.ServerErrorException in project ma-modules-public by infiniteautomation.

the class ServerRestController method getNetworkInterfaces.

@ApiOperation(value = "List network interfaces", notes = "Requires global data source permission")
@RequestMapping(method = { RequestMethod.GET }, value = "/network-interfaces")
@PreAuthorize("isGrantedPermission('permissionDatasource')")
public List<NetworkInterfaceModel> getNetworkInterfaces(@RequestParam(value = "includeLoopback", required = false, defaultValue = "false") boolean includeLoopback, @RequestParam(value = "includeDefault", required = false, defaultValue = "false") boolean includeDefault, @AuthenticationPrincipal PermissionHolder user) {
    List<NetworkInterfaceModel> models = new ArrayList<>();
    if (includeDefault) {
        NetworkInterfaceModel model = new NetworkInterfaceModel();
        model.setHostAddress("0.0.0.0");
        model.setInterfaceName("");
        models.add(model);
    }
    try {
        for (NICInfo ni : HostUtils.getLocalInet4Addresses(includeLoopback)) {
            NetworkInterfaceModel model = new NetworkInterfaceModel();
            model.setHostAddress(ni.getInetAddress().getHostAddress());
            model.setInterfaceName(ni.getInterfaceName());
            models.add(model);
        }
    } catch (SocketException e) {
        throw new ServerErrorException(new TranslatableMessage("common.default", e.getMessage()), e);
    }
    return models;
}
Also used : SocketException(java.net.SocketException) NetworkInterfaceModel(com.infiniteautomation.mango.rest.latest.model.server.NetworkInterfaceModel) ArrayList(java.util.ArrayList) ServerErrorException(com.infiniteautomation.mango.rest.latest.exception.ServerErrorException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) NICInfo(com.serotonin.m2m2.util.HostUtils.NICInfo) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with ServerErrorException

use of com.infiniteautomation.mango.rest.latest.exception.ServerErrorException in project ma-modules-public by infiniteautomation.

the class RestModelMapper method mapWithView.

public <T> MappingJacksonValue mapWithView(Object vo, Class<T> modelClass, PermissionHolder user) {
    Objects.requireNonNull(vo);
    Objects.requireNonNull(modelClass);
    for (RestModelMapping<?, ?> mapping : mappings) {
        if (mapping.supports(vo.getClass(), modelClass)) {
            @SuppressWarnings("unchecked") T result = (T) mapping.map(vo, user, this);
            if (result != null) {
                MappingJacksonValue mappingValue = new MappingJacksonValue(result);
                mappingValue.setSerializationView(mapping.view(vo, user));
                return mappingValue;
            }
        }
    }
    throw new ServerErrorException(new TranslatableMessage("rest.missingModelMapping", vo.getClass(), modelClass));
}
Also used : MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) ServerErrorException(com.infiniteautomation.mango.rest.latest.exception.ServerErrorException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 9 with ServerErrorException

use of com.infiniteautomation.mango.rest.latest.exception.ServerErrorException in project ma-modules-public by infiniteautomation.

the class RestModelMapper method map.

public <T> T map(Object vo, Class<T> modelClass, PermissionHolder user) {
    Objects.requireNonNull(vo);
    Objects.requireNonNull(modelClass);
    for (RestModelMapping<?, ?> mapping : mappings) {
        if (mapping.supports(vo.getClass(), modelClass)) {
            @SuppressWarnings("unchecked") T result = (T) mapping.map(vo, user, this);
            if (result != null) {
                return result;
            }
        }
    }
    throw new ServerErrorException(new TranslatableMessage("rest.missingModelMapping", vo.getClass(), modelClass));
}
Also used : ServerErrorException(com.infiniteautomation.mango.rest.latest.exception.ServerErrorException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 10 with ServerErrorException

use of com.infiniteautomation.mango.rest.latest.exception.ServerErrorException in project ma-modules-public by infiniteautomation.

the class RestModelMapper method unMap.

public <T> T unMap(Object model, Class<T> voClass, PermissionHolder user) {
    Objects.requireNonNull(model);
    Objects.requireNonNull(voClass);
    for (RestModelMapping<?, ?> mapping : mappings) {
        if (mapping.unmapSupports(model.getClass(), voClass)) {
            @SuppressWarnings("unchecked") T result = (T) mapping.unmap(model, user, this);
            if (result != null) {
                return result;
            }
        }
    }
    throw new ServerErrorException(new TranslatableMessage("rest.missingModelMapping", model.getClass(), voClass));
}
Also used : ServerErrorException(com.infiniteautomation.mango.rest.latest.exception.ServerErrorException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Aggregations

ServerErrorException (com.infiniteautomation.mango.rest.latest.exception.ServerErrorException)10 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)10 ApiOperation (io.swagger.annotations.ApiOperation)3 ArrayList (java.util.ArrayList)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 AbstractRestException (com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)2 BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)2 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)2 ASTNode (net.jazdw.rql.parser.ASTNode)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ConditionSortLimit (com.infiniteautomation.mango.db.query.ConditionSortLimit)1 VirtualSerialPortConfig (com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfig)1 GenericRestException (com.infiniteautomation.mango.rest.latest.exception.GenericRestException)1 NotFoundRestException (com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException)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