Skip to content

armgit / core

Public Imported from GitHub
0

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

Hotfix for error when saving manager config (#941)

Martin Peeters47b056c41521
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 e22c72e17..5b1a752ae 100644
--- a/manager/src/main/java/org/openremote/manager/app/ConfigurationResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/app/ConfigurationResourceImpl.java
@@ -42,13 +42,13 @@ public class ConfigurationResourceImpl extends ManagerWebResource implements Con
@Override
- public Object update(RequestParams requestParams, Object managerConfiguration) throws IOException {
+ public Object update(RequestParams requestParams, Object managerConfiguration) {
this.configurationService.saveMangerConfig(managerConfiguration);
return managerConfiguration;
}
@Override
- public String fileUpload(RequestParams requestParams, String path, FileInfo fileInfo) throws IOException {
+ public String fileUpload(RequestParams requestParams, String path, FileInfo fileInfo) {
this.configurationService.saveImageFile(path, fileInfo);
return path;
}
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 43fe45dce..a9e101c84 100644
--- a/manager/src/main/java/org/openremote/manager/app/ConfigurationService.java
+++ b/manager/src/main/java/org/openremote/manager/app/ConfigurationService.java
@@ -19,11 +19,6 @@
*/
package org.openremote.manager.app;
-import com.fasterxml.jackson.annotation.JsonAutoDetect;
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.annotation.PropertyAccessor;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.timer.TimerService;
@@ -87,41 +82,34 @@ public class ConfigurationService extends RouteBuilder implements ContainerServi
}
- public void saveMangerConfig(Object managerConfiguration) throws IOException {
- LOG.log(Level.INFO, "Saving manager_config.json");
- try {
- OutputStream out = new FileOutputStream(new File(pathPublicRoot + "/manager_config.json"));
- out.close();
- ObjectMapper mapper = new ObjectMapper();
-
- mapper
- .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
- .setSerializationInclusion(JsonInclude.Include.NON_NULL)
- .enable(SerializationFeature.INDENT_OUTPUT);
-
- out.write(mapper.writeValueAsString(managerConfiguration).getBytes());
- out.close();
- } catch (IOException exception) {
- LOG.log(Level.WARNING, "Saving manager_config.json error", exception);
- throw exception;
+ public void saveMangerConfig(Object managerConfiguration) {
+ LOG.log(Level.INFO, "Saving manager_config.json..");
+ try (OutputStream out = new FileOutputStream(pathPublicRoot + "/manager_config.json")) {
+ out.write(ValueUtil.JSON.writeValueAsString(managerConfiguration).getBytes());
+ } catch (IOException | SecurityException exception) {
+ LOG.log(Level.WARNING, "Error when trying to save manager_config.json", exception);
}
}
- public void saveImageFile(String path, FileInfo fileInfo) throws IOException {
- LOG.log(Level.INFO, "Saving image for manger_config.json");
+ public void saveImageFile(String path, FileInfo fileInfo) {
+ LOG.log(Level.INFO, "Saving image in manger_config.json..");
+ File file = new File(pathPublicRoot + path);
try {
- File file = new File(pathPublicRoot + path);
file.getParentFile().mkdirs();
if (file.exists()) {
file.delete();
}
- OutputStream out = new FileOutputStream(file);
+ } catch (SecurityException se) {
+ LOG.log(Level.WARNING, "Could not access folder for editing image in manager_config.json");
+ return;
+ }
+
+ try (OutputStream out = new FileOutputStream(file)) {
out.write(CodecUtil.decodeBase64(fileInfo.getContents()));
- } catch (IOException exception) {
- LOG.log(Level.WARNING, "saving error for image manger_config.json", exception);
- throw exception;
+ } catch (IOException | SecurityException exception) {
+ LOG.log(Level.WARNING, "Error when saving image in manger_config.json", exception);
}
}
diff --git a/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java b/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java
index 63150afd4..0edc40702 100644
--- a/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java
@@ -42,7 +42,7 @@ public class MapResourceImpl extends WebResource implements MapResource {
}
@Override
- public Object saveSettings(RequestParams requestParams, Map<String, MapRealmConfig> mapConfig) throws Exception {
+ public Object saveSettings(RequestParams requestParams, Map<String, MapRealmConfig> mapConfig) {
return mapService.saveMapConfig(mapConfig);
}
diff --git a/manager/src/main/java/org/openremote/manager/map/MapService.java b/manager/src/main/java/org/openremote/manager/map/MapService.java
index f78e0b3ca..70f267614 100644
--- a/manager/src/main/java/org/openremote/manager/map/MapService.java
+++ b/manager/src/main/java/org/openremote/manager/map/MapService.java
@@ -40,17 +40,11 @@ import org.openremote.model.util.TextUtil;
import org.openremote.model.util.ValueUtil;
import javax.ws.rs.core.UriBuilder;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
+import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
+import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@@ -86,29 +80,20 @@ public class MapService implements ContainerService {
protected Path mapSettingsPath;
protected Metadata metadata;
protected ObjectNode mapConfig;
- protected ObjectNode mapSource;
protected ConcurrentMap<String, ObjectNode> mapSettings = new ConcurrentHashMap<>();
protected ConcurrentMap<String, ObjectNode> mapSettingsJs = new ConcurrentHashMap<>();
- public ObjectNode saveMapConfig(Map<String, MapRealmConfig> mapConfiguration) throws Exception {
- LOG.log(Level.INFO, "Saving mapsettings.json");
+ public ObjectNode saveMapConfig(Map<String, MapRealmConfig> mapConfiguration) {
+ LOG.log(Level.INFO, "Saving mapsettings.json..");
this.mapConfig.putNull("options");
this.mapSettings.clear();
ObjectNode mapSettings = loadMapSettingsJson(mapSettingsPath);
try(OutputStream out = new FileOutputStream(new File(mapSettingsPath.toUri()))){
- ObjectMapper mapper = new ObjectMapper();
mapSettings.putPOJO("options", mapConfiguration);
- mapper
- .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
- .setSerializationInclusion(JsonInclude.Include.NON_NULL)
- .enable(SerializationFeature.INDENT_OUTPUT);
-
- out.write(mapper.writeValueAsString(mapSettings).getBytes());
- out.close();
+ out.write(ValueUtil.JSON.writeValueAsString(mapSettings).getBytes());
this.setData();
- } catch (IOException exception) {
- LOG.log(Level.WARNING, "Saving mapsettings.json error", exception);
- throw exception;
+ } catch (Exception exception) {
+ LOG.log(Level.WARNING, "Error trying to save mapsettings.json", exception);
}
return mapSettings;
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 c0d7d5424..d3d570fa4 100644
--- a/model/src/main/java/org/openremote/model/manager/ConfigurationResource.java
+++ b/model/src/main/java/org/openremote/model/manager/ConfigurationResource.java
@@ -39,7 +39,7 @@ public interface ConfigurationResource {
@Produces(APPLICATION_JSON)
@Path("manager")
@RolesAllowed({Constants.WRITE_ADMIN_ROLE})
- Object update(@BeanParam RequestParams requestParams, Object managerConfiguration) throws IOException;
+ Object update(@BeanParam RequestParams requestParams, Object managerConfiguration);
@POST
@Consumes(APPLICATION_JSON)
@@ -51,7 +51,7 @@ public interface ConfigurationResource {
@QueryParam("path")
String path,
FileInfo fileInfo
- ) throws IOException;
+ );
}
diff --git a/model/src/main/java/org/openremote/model/map/MapResource.java b/model/src/main/java/org/openremote/model/map/MapResource.java
index b6ba815d1..eca221b13 100644
--- a/model/src/main/java/org/openremote/model/map/MapResource.java
+++ b/model/src/main/java/org/openremote/model/map/MapResource.java
@@ -38,7 +38,7 @@ public interface MapResource {
*/
@PUT
@Produces(MediaType.APPLICATION_JSON)
- Object saveSettings(@BeanParam RequestParams requestParams, Map<String, MapRealmConfig> mapConfig) throws Exception;
+ Object saveSettings(@BeanParam RequestParams requestParams, Map<String, MapRealmConfig> mapConfig);
/**
* Returns style used to initialise Mapbox GL