Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Fix for manager app config to allow empty properties include field (#1556)

Rich Turner886a30343e26
diff --git a/manager/src/main/java/org/openremote/manager/app/ConfigurationResourceImpl.java b/manager/src/main/java/org/openremote/manager/app/ConfigurationResourceImpl.java
index 9e77cc048..22884ec33 100644
--- a/manager/src/main/java/org/openremote/manager/app/ConfigurationResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/app/ConfigurationResourceImpl.java
@@ -29,6 +29,7 @@ import org.openremote.manager.web.ManagerWebResource;
import org.openremote.model.manager.ConfigurationResource;
import org.openremote.model.file.FileInfo;
import org.openremote.model.http.RequestParams;
+import org.openremote.model.manager.ManagerAppConfig;
import java.io.*;
import java.nio.file.Files;
@@ -47,7 +48,7 @@ public class ConfigurationResourceImpl extends ManagerWebResource implements Con
}
@Override
- public ObjectNode update(RequestParams requestParams, ObjectNode managerConfiguration) {
+ public ManagerAppConfig update(RequestParams requestParams, ManagerAppConfig managerConfiguration) {
try {
this.configurationService.saveManagerConfig(managerConfiguration);
} catch (Exception e) {
@@ -76,7 +77,7 @@ public class ConfigurationResourceImpl extends ManagerWebResource implements Con
}
@Override
- public ObjectNode getManagerConfig() {
+ public ManagerAppConfig getManagerConfig() {
return configurationService.getManagerConfig();
}
diff --git a/manager/src/main/java/org/openremote/manager/app/ConfigurationService.java b/manager/src/main/java/org/openremote/manager/app/ConfigurationService.java
index 73864b351..f0e562cc8 100644
--- a/manager/src/main/java/org/openremote/manager/app/ConfigurationService.java
+++ b/manager/src/main/java/org/openremote/manager/app/ConfigurationService.java
@@ -29,6 +29,9 @@ import org.openremote.manager.web.ManagerWebService;
import org.openremote.model.Container;
import org.openremote.model.ContainerService;
import org.openremote.model.file.FileInfo;
+import org.openremote.model.manager.ManagerAppConfig;
+import org.openremote.model.manager.ManagerAppRealmConfig;
+import org.openremote.model.util.TextUtil;
import org.openremote.model.util.ValueUtil;
import java.io.File;
@@ -37,10 +40,8 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
@@ -66,6 +67,7 @@ public class ConfigurationService implements ContainerService {
protected Path mapTilesPath;
protected Path mapSettingsPath;
protected Path managerConfigPath;
+ protected AtomicReference<ManagerAppConfig> managerAppConfig;
@Override
public void init(Container container) throws Exception {
@@ -162,31 +164,40 @@ public class ConfigurationService implements ContainerService {
return mapTilesPath;
}
- public ObjectNode getManagerConfig() {
+ public ManagerAppConfig getManagerConfig() {
+ if (managerAppConfig != null) {
+ return managerAppConfig.get();
+ }
if (managerConfigPath == null) {
return null;
}
try {
- return (ObjectNode) ValueUtil.JSON.readTree(managerConfigPath.toFile());
+ String managerConfigStr = Files.readString(managerConfigPath, StandardCharsets.UTF_8);
+ return ValueUtil.parse(managerConfigStr, ManagerAppConfig.class).orElse(null);
} catch (Exception e) {
LOG.severe("Could not read manager_config.json from " + managerConfigPath);
return null;
}
}
- public void saveManagerConfig(ObjectNode managerConfiguration) throws Exception {
+ public void saveManagerConfig(ManagerAppConfig managerAppConfig) throws Exception {
LOG.log(Level.INFO, "Saving manager_config.json to: " + getPersistedManagerConfigPath());
try {
// Check references to images
- managerConfiguration = checkAndFixImageReferences(managerConfiguration);
+ managerAppConfig = checkAndFixImageReferences(managerAppConfig);
Path p = getPersistedManagerConfigPath();
File file = p.toAbsolutePath().toFile();
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
- Files.writeString(p, ValueUtil.JSON.writeValueAsString(managerConfiguration), StandardCharsets.UTF_8);
+ Files.writeString(p, ValueUtil.JSON.writeValueAsString(managerAppConfig), StandardCharsets.UTF_8);
// Ensure managerConfigPath is now pointing to persistence path
managerConfigPath = p;
+ if (this.managerAppConfig == null) {
+ this.managerAppConfig = new AtomicReference<>(managerAppConfig);
+ } else {
+ this.managerAppConfig.set(managerAppConfig);
+ }
} catch (Exception exception) {
String msg = "Error saving manager_config.json: msg=" + exception.getMessage();
LOG.log(Level.WARNING, msg);
@@ -256,41 +267,49 @@ public class ConfigurationService implements ContainerService {
return this.persistenceService.resolvePath("manager").resolve("images");
}
- protected ObjectNode checkAndFixImageReferences(ObjectNode managerConfig) {
+ protected ManagerAppConfig checkAndFixImageReferences(ManagerAppConfig managerAppConfig) {
- List<String> imageTypes = List.of("logo", "logoMobile", "favicon");
if (!getPersistedManagerConfigImagePath().toFile().exists()) getPersistedManagerConfigImagePath().toFile().mkdirs();
- for (Iterator<Map.Entry<String, JsonNode>> it = managerConfig.get("realms").fields(); it.hasNext(); ) {
- Map.Entry<String, JsonNode> kvp = it.next();
- String realmName = kvp.getKey();
- JsonNode realm = kvp.getValue();
- for (String type : imageTypes) {
- String image = realm.get(type) != null ? realm.get(type).asText() : "";
- if (!image.isBlank()) {
- image = image.replace("/api/master/configuration/manager/image/", "");
- image = image.replace("images/", "");
- image = image.charAt(0) == '/' ? image.substring(1) : image;
- Path imagePath = Path.of(image);
- Path persistedImagePath = getPersistedManagerConfigImagePath().resolve(imagePath).toAbsolutePath();
- Path path = pathPublicRoot.resolve("images").resolve(imagePath).toAbsolutePath();
-
- if (!Files.isRegularFile(persistedImagePath)) {
- if (Files.isRegularFile(path)) {
- try {
- // Image doesn't exist in persisted path but does exist in doc root so copy it
- Files.copy(path, persistedImagePath);
- // Change the reference in the config to the typical API-like reference:
- ((ObjectNode) managerConfig.get("realms").get(realmName)).put(type, "/api/master/configuration/manager/image/" + imagePath);
- } catch (Exception e) {
- LOG.warning("Error occurred whilst copying manager config image to persisted path: " + imagePath);
- }
- } else {
- LOG.warning("manager_config.json image reference doesn't exist: " + imagePath);
- }
+ if (managerAppConfig.getRealms() != null && !managerAppConfig.getRealms().isEmpty()) {
+ managerAppConfig.getRealms().values().forEach(managerAppRealmConfig -> {
+ if (!TextUtil.isNullOrEmpty(managerAppRealmConfig.getLogo())) {
+ managerAppRealmConfig.setLogo(fixImageRef(managerAppRealmConfig.getLogo()));
+ }
+ if (!TextUtil.isNullOrEmpty(managerAppRealmConfig.getLogoMobile())) {
+ managerAppRealmConfig.setLogo(fixImageRef(managerAppRealmConfig.getLogoMobile()));
+ }
+ if (!TextUtil.isNullOrEmpty(managerAppRealmConfig.getFavicon())) {
+ managerAppRealmConfig.setLogo(fixImageRef(managerAppRealmConfig.getFavicon()));
+ }
+ });
+ }
+ return managerAppConfig;
+ }
+
+ protected String fixImageRef(String image) {
+ if (!image.isBlank()) {
+ image = image.replace("/api/master/configuration/manager/image/", "");
+ image = image.replace("images/", "");
+ image = image.charAt(0) == '/' ? image.substring(1) : image;
+ Path imagePath = Path.of(image);
+ Path persistedImagePath = getPersistedManagerConfigImagePath().resolve(imagePath).toAbsolutePath();
+ Path path = pathPublicRoot.resolve("images").resolve(imagePath).toAbsolutePath();
+
+ if (!Files.isRegularFile(persistedImagePath)) {
+ if (Files.isRegularFile(path)) {
+ try {
+ // Image doesn't exist in persisted path but does exist in doc root so copy it
+ Files.copy(path, persistedImagePath);
+ // Change the reference in the config to the typical API-like reference:
+ image = "/api/master/configuration/manager/image/" + imagePath;
+ } catch (Exception e) {
+ LOG.warning("Error occurred whilst copying manager config image to persisted path: " + imagePath);
}
+ } else {
+ LOG.warning("manager_config.json image reference doesn't exist: " + imagePath);
}
}
}
- return managerConfig;
+ return image;
}
}
diff --git a/manager/src/main/java/org/openremote/manager/system/StatusResourceImpl.java b/manager/src/main/java/org/openremote/manager/system/StatusResourceImpl.java
index 0b45363b7..2e95e13b4 100644
--- a/manager/src/main/java/org/openremote/manager/system/StatusResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/system/StatusResourceImpl.java
@@ -43,20 +43,26 @@ public class StatusResourceImpl implements StatusResource {
this.healthStatusProviderList = healthStatusProviderList;
Properties versionProps = new Properties();
String authServerUrl = "";
+ String version = null;
ManagerIdentityService identityService = container.getService(ManagerIdentityService.class);
if (identityService != null && identityService.getIdentityProvider().getFrontendURI() != null) {
authServerUrl = identityService.getIdentityProvider().getFrontendURI();
}
- try(InputStream resourceStream = StatusResourceImpl.class.getClassLoader().getResourceAsStream("version.properties")) {
- versionProps.load(resourceStream);
- } catch (IOException e) {
- LOG.log(Level.SEVERE, "Failed to load manager version properties file: version.properties");
- throw new IllegalStateException("Missing manager version.properties file");
+ try (InputStream resourceStream = StatusResourceImpl.class.getClassLoader().getResourceAsStream("version.properties")) {
+ if (resourceStream != null) {
+ versionProps.load(resourceStream);
+ version = versionProps.getProperty("version");
+ }
+ } catch (IOException ignored) {
+ }
+
+ if (version == null) {
+ LOG.log(Level.WARNING, "Failed to load manager version properties file: version.properties");
+ version = "0.0.0";
}
- String version = versionProps.getProperty("version");
serverInfo = Map.of(
"version", version,
"authServerUrl", authServerUrl
diff --git a/model/src/main/java/org/openremote/model/manager/ConfigurationResource.java b/model/src/main/java/org/openremote/model/manager/ConfigurationResource.java
index 8499f8371..fb19b65b9 100644
--- a/model/src/main/java/org/openremote/model/manager/ConfigurationResource.java
+++ b/model/src/main/java/org/openremote/model/manager/ConfigurationResource.java
@@ -41,7 +41,7 @@ public interface ConfigurationResource {
@Path("manager")
@RolesAllowed({Constants.WRITE_ADMIN_ROLE})
@Operation(operationId = "updateConfiguration", summary = "Update manager configuration")
- ObjectNode update(@BeanParam RequestParams requestParams, ObjectNode managerConfiguration);
+ ManagerAppConfig update(@BeanParam RequestParams requestParams, ManagerAppConfig managerConfiguration);
@POST
@Consumes(APPLICATION_JSON)
@@ -60,7 +60,7 @@ public interface ConfigurationResource {
@Produces(APPLICATION_JSON)
@Path("manager")
@Operation(operationId = "getManagerConfig", summary = "Retrieve the manager configuration JSON")
- ObjectNode getManagerConfig();
+ ManagerAppConfig getManagerConfig();
@GET
@Path("manager/image/{filename: .+}")
diff --git a/model/src/main/java/org/openremote/model/manager/ManagerAppConfig.java b/model/src/main/java/org/openremote/model/manager/ManagerAppConfig.java
index 61ac54e54..2b2141ed4 100644
--- a/model/src/main/java/org/openremote/model/manager/ManagerAppConfig.java
+++ b/model/src/main/java/org/openremote/model/manager/ManagerAppConfig.java
@@ -19,13 +19,60 @@
*/
package org.openremote.model.manager;
+import com.fasterxml.jackson.annotation.JsonInclude;
+
import java.io.Serializable;
import java.util.Map;
-
+@JsonInclude(JsonInclude.Include.NON_NULL)
public class ManagerAppConfig implements Serializable {
protected boolean loadLocales;
protected Map<String, String> languages;
protected Map<String, ManagerAppRealmConfig> realms;
protected Map<String, Object> pages;
protected ManagerConfig manager;
+
+ public boolean isLoadLocales() {
+ return loadLocales;
+ }
+
+ public ManagerAppConfig setLoadLocales(boolean loadLocales) {
+ this.loadLocales = loadLocales;
+ return this;
+ }
+
+ public Map<String, String> getLanguages() {
+ return languages;
+ }
+
+ public ManagerAppConfig setLanguages(Map<String, String> languages) {
+ this.languages = languages;
+ return this;
+ }
+
+ public Map<String, ManagerAppRealmConfig> getRealms() {
+ return realms;
+ }
+
+ public ManagerAppConfig setRealms(Map<String, ManagerAppRealmConfig> realms) {
+ this.realms = realms;
+ return this;
+ }
+
+ public Map<String, Object> getPages() {
+ return pages;
+ }
+
+ public ManagerAppConfig setPages(Map<String, Object> pages) {
+ this.pages = pages;
+ return this;
+ }
+
+ public ManagerConfig getManager() {
+ return manager;
+ }
+
+ public ManagerAppConfig setManager(ManagerConfig manager) {
+ this.manager = manager;
+ return this;
+ }
}
diff --git a/model/src/main/java/org/openremote/model/manager/ManagerAppRealmConfig.java b/model/src/main/java/org/openremote/model/manager/ManagerAppRealmConfig.java
index dcaad61ea..21ea3eb9e 100644
--- a/model/src/main/java/org/openremote/model/manager/ManagerAppRealmConfig.java
+++ b/model/src/main/java/org/openremote/model/manager/ManagerAppRealmConfig.java
@@ -28,4 +28,76 @@ public class ManagerAppRealmConfig {
protected String language;
protected String[] headers;
protected ManagerAppRealmNotificationConfig notifications;
+
+ public String getAppTitle() {
+ return appTitle;
+ }
+
+ public ManagerAppRealmConfig setAppTitle(String appTitle) {
+ this.appTitle = appTitle;
+ return this;
+ }
+
+ public String getStyles() {
+ return styles;
+ }
+
+ public ManagerAppRealmConfig setStyles(String styles) {
+ this.styles = styles;
+ return this;
+ }
+
+ public String getLogo() {
+ return logo;
+ }
+
+ public ManagerAppRealmConfig setLogo(String logo) {
+ this.logo = logo;
+ return this;
+ }
+
+ public String getLogoMobile() {
+ return logoMobile;
+ }
+
+ public ManagerAppRealmConfig setLogoMobile(String logoMobile) {
+ this.logoMobile = logoMobile;
+ return this;
+ }
+
+ public String getFavicon() {
+ return favicon;
+ }
+
+ public ManagerAppRealmConfig setFavicon(String favicon) {
+ this.favicon = favicon;
+ return this;
+ }
+
+ public String getLanguage() {
+ return language;
+ }
+
+ public ManagerAppRealmConfig setLanguage(String language) {
+ this.language = language;
+ return this;
+ }
+
+ public String[] getHeaders() {
+ return headers;
+ }
+
+ public ManagerAppRealmConfig setHeaders(String[] headers) {
+ this.headers = headers;
+ return this;
+ }
+
+ public ManagerAppRealmNotificationConfig getNotifications() {
+ return notifications;
+ }
+
+ public ManagerAppRealmConfig setNotifications(ManagerAppRealmNotificationConfig notifications) {
+ this.notifications = notifications;
+ return this;
+ }
}