From ef86963c18d3928698892968361cf9a3e5ffe309 Mon Sep 17 00:00:00 2001 From: Alex Bogdanovski Date: Sun, 26 May 2024 15:58:28 +0300 Subject: [PATCH] added API support for uploading HOCON configuration directly --- pom.xml | 3 +- .../com/erudika/scoold/api/ApiController.java | 28 ++++++++++++++----- src/main/resources/templates/api.yaml | 5 +++- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 7c06d3dd..67fb9a23 100755 --- a/pom.xml +++ b/pom.xml @@ -224,7 +224,7 @@ - false + @@ -274,7 +274,6 @@ validate ${basedir}/src/main/resources/checkstyle.xml - UTF-8 true true false diff --git a/src/main/java/com/erudika/scoold/api/ApiController.java b/src/main/java/com/erudika/scoold/api/ApiController.java index 5c821f4f..0fdfbd2d 100644 --- a/src/main/java/com/erudika/scoold/api/ApiController.java +++ b/src/main/java/com/erudika/scoold/api/ApiController.java @@ -54,6 +54,8 @@ import com.erudika.scoold.core.UnapprovedReply; import com.erudika.scoold.utils.BadRequestException; import com.erudika.scoold.utils.ScooldUtils; +import com.typesafe.config.ConfigFactory; +import com.typesafe.config.ConfigValue; import jakarta.inject.Inject; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -73,6 +75,7 @@ import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; @@ -1022,12 +1025,23 @@ public String config(HttpServletRequest req, HttpServletResponse res) { @PutMapping("/config") public String configSet(HttpServletRequest req, HttpServletResponse res) { - Map entity = readEntity(req); - if (entity.isEmpty()) { - badReq("Missing or invalid request body."); - } - for (Map.Entry entry : entity.entrySet()) { - System.setProperty(CONF.getConfigRootPrefix() + "." + entry.getKey(), entry.getValue().toString()); + if ("application/hocon".equals(req.getContentType())) { + try { + String config = IOUtils.toString(req.getInputStream(), "utf-8"); + for (Map.Entry entry : ConfigFactory.parseString(config).entrySet()) { + System.setProperty(entry.getKey(), entry.getValue().unwrapped().toString()); + } + } catch (IOException ex) { + badReq("Missing or invalid request body."); + } + } else { + Map entity = readEntity(req); + if (entity.isEmpty()) { + badReq("Missing or invalid request body."); + } + for (Map.Entry entry : entity.entrySet()) { + System.setProperty(CONF.getConfigRootPrefix() + "." + entry.getKey(), entry.getValue().toString()); + } } CONF.store(); pc.setAppSettings(CONF.getParaAppSettings()); @@ -1161,7 +1175,7 @@ private Map readEntity(HttpServletRequest req) { req.setAttribute(REST_ENTITY_ATTRIBUTE, entity); return entity; } catch (IOException ex) { - badReq("Missing or invalid request body."); + badReq("Expected 'application/json' body but got '" + req.getContentType() + "' in request body."); } catch (Exception ex) { logger.error(null, ex); } diff --git a/src/main/resources/templates/api.yaml b/src/main/resources/templates/api.yaml index c504591d..10c5769d 100644 --- a/src/main/resources/templates/api.yaml +++ b/src/main/resources/templates/api.yaml @@ -1513,12 +1513,15 @@ paths: security: - scoold_auth: [] requestBody: - description: All configuration properties and values inside a JSON object. Property keys **must not start with** the `scoold.` prefix. + description: All configuration properties and values inside a JSON object or HOCON string. Property keys **must not start with** the `scoold.` prefix. required: true content: application/json: schema: type: object + application/hocon: + schema: + type: string responses: '200': description: Returns the whole updated Scoold configuration as a JSON object