Skip to content

Commit

Permalink
added API support for uploading HOCON configuration directly
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed May 26, 2024
1 parent 0de34bd commit ef86963
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
<configuration>
<!--==============================================-->
<!-- ENABLE FORK MODE IF YOU WANT TO USE DEVTOOLS -->
<fork>false</fork>
<!--<fork>false</fork>-->
<!--==============================================-->
</configuration>
<executions>
Expand Down Expand Up @@ -274,7 +274,6 @@
<phase>validate</phase>
<configuration>
<configLocation>${basedir}/src/main/resources/checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/com/erudika/scoold/api/ApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -1022,12 +1025,23 @@ public String config(HttpServletRequest req, HttpServletResponse res) {

@PutMapping("/config")
public String configSet(HttpServletRequest req, HttpServletResponse res) {
Map<String, Object> entity = readEntity(req);
if (entity.isEmpty()) {
badReq("Missing or invalid request body.");
}
for (Map.Entry<String, Object> 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<String, ConfigValue> entry : ConfigFactory.parseString(config).entrySet()) {
System.setProperty(entry.getKey(), entry.getValue().unwrapped().toString());
}
} catch (IOException ex) {
badReq("Missing or invalid request body.");
}
} else {
Map<String, Object> entity = readEntity(req);
if (entity.isEmpty()) {
badReq("Missing or invalid request body.");
}
for (Map.Entry<String, Object> entry : entity.entrySet()) {
System.setProperty(CONF.getConfigRootPrefix() + "." + entry.getKey(), entry.getValue().toString());
}
}
CONF.store();
pc.setAppSettings(CONF.getParaAppSettings());
Expand Down Expand Up @@ -1161,7 +1175,7 @@ private Map<String, Object> 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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/templates/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ef86963

Please sign in to comment.