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);
}
}
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;
}
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));
}
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));
}
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));
}
Aggregations