diff --git a/README.md b/README.md index fa36abb..70e7e97 100644 --- a/README.md +++ b/README.md @@ -3,21 +3,19 @@ An OpenMRS module that intercepts and redirects fetch requests for `ChargeItemDe resources to an external API, with support for basic authentication. ## Configuration -The module reads the configuration from a json file named `config.json` in a directory named `fhirproxy` which is -located in the application data directory, below is an example configuration file. +The module reads the configuration from a properties file named `config.properties` in a directory named `fhirproxy` +which is located in the application data directory, below is an example configuration file. ``` -{ - "externalApiEnabled" : true, - "baseUrl" : "http://your-fhirserver.com/fhir/R4", - "username" : "test-user", - "password" : "test-password" -} +external.api.enabled=true +base.url=http://your-fhirserver/fhir/R4 +username=test-user +password=test-password ``` #### Configuration Properties -`externalApiEnabled`: When set to true, the module will delegate `GET` FHIR requests for `ChargeItemDefinition` and +`external.api.enabled`: When set to true, the module will delegate `GET` FHIR requests for `ChargeItemDefinition` and `InventoryItem` to the configured external API. -`baseUrl`: The root URL for the external FHIR API +`base.url`: The root URL for the external FHIR API `username`: The username to authenticate to the external FHIR API diff --git a/api/src/main/java/org/openmrs/module/fhirproxy/Config.java b/api/src/main/java/org/openmrs/module/fhirproxy/Config.java index d1e72b1..e622348 100644 --- a/api/src/main/java/org/openmrs/module/fhirproxy/Config.java +++ b/api/src/main/java/org/openmrs/module/fhirproxy/Config.java @@ -16,4 +16,11 @@ public class Config { @Getter private String password; + public Config(boolean externalApiEnabled, String baseUrl, String username, String password) { + this.externalApiEnabled = externalApiEnabled; + this.baseUrl = baseUrl; + this.username = username; + this.password = password; + } + } diff --git a/api/src/main/java/org/openmrs/module/fhirproxy/Constants.java b/api/src/main/java/org/openmrs/module/fhirproxy/Constants.java index 0a4d345..84530b9 100644 --- a/api/src/main/java/org/openmrs/module/fhirproxy/Constants.java +++ b/api/src/main/java/org/openmrs/module/fhirproxy/Constants.java @@ -13,6 +13,6 @@ public class Constants { public static final String MODULE_ID = "fhirproxy"; - public static final String CONFIG_FILE = "config.json"; + public static final String CONFIG_FILE = "config.properties"; } diff --git a/api/src/main/java/org/openmrs/module/fhirproxy/FhirProxyActivator.java b/api/src/main/java/org/openmrs/module/fhirproxy/FhirProxyActivator.java index 7d10955..f96599d 100644 --- a/api/src/main/java/org/openmrs/module/fhirproxy/FhirProxyActivator.java +++ b/api/src/main/java/org/openmrs/module/fhirproxy/FhirProxyActivator.java @@ -36,7 +36,7 @@ public void started() { if (cfg.isExternalApiEnabled()) { if (StringUtils.isBlank(cfg.getBaseUrl())) { - throw new ModuleException("Fhir Proxy module requires baseUrl when external FHIR API is enabled"); + throw new ModuleException("Fhir Proxy module requires base.url when external FHIR API is enabled"); } else if (StringUtils.isBlank(cfg.getUsername())) { throw new ModuleException("Fhir Proxy module requires username when external FHIR API is enabled"); } else if (StringUtils.isBlank(cfg.getPassword())) { diff --git a/api/src/main/java/org/openmrs/module/fhirproxy/FhirProxyUtils.java b/api/src/main/java/org/openmrs/module/fhirproxy/FhirProxyUtils.java index a7630f7..e90bab1 100644 --- a/api/src/main/java/org/openmrs/module/fhirproxy/FhirProxyUtils.java +++ b/api/src/main/java/org/openmrs/module/fhirproxy/FhirProxyUtils.java @@ -1,14 +1,14 @@ package org.openmrs.module.fhirproxy; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.util.Properties; import org.openmrs.util.OpenmrsUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.databind.ObjectMapper; - /** * Provides utility methods for the module. */ @@ -29,8 +29,13 @@ public static final Config getConfig() throws IOException { LOG.info("Loading config for FHIR proxy module"); File configDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(Constants.MODULE_ID); - File configFile = new File(configDir, Constants.CONFIG_FILE); - config = new ObjectMapper().readValue(configFile, Config.class); + Properties props = new Properties(); + props.load(new FileInputStream(new File(configDir, Constants.CONFIG_FILE))); + final boolean enabled = Boolean.valueOf(props.getProperty("external.api.enabled", "false")); + final String baseUrl = props.getProperty("base.url"); + final String username = props.getProperty("username"); + final String password = props.getProperty("password"); + config = new Config(enabled, baseUrl, username, password); } return config; diff --git a/api/src/test/java/org/openmrs/module/fhirproxy/FhirProxyActivatorTest.java b/api/src/test/java/org/openmrs/module/fhirproxy/FhirProxyActivatorTest.java index d30ffd4..5cda8cb 100644 --- a/api/src/test/java/org/openmrs/module/fhirproxy/FhirProxyActivatorTest.java +++ b/api/src/test/java/org/openmrs/module/fhirproxy/FhirProxyActivatorTest.java @@ -51,7 +51,7 @@ public void started_shouldFailIfBaseUrlIsMissing() { Exception ex = Assert.assertThrows(ModuleException.class, () -> activator.started()); - assertEquals("Fhir Proxy module requires baseUrl when external FHIR API is enabled", ex.getMessage()); + assertEquals("Fhir Proxy module requires base.url when external FHIR API is enabled", ex.getMessage()); } @Test diff --git a/api/src/test/resources/config.json b/api/src/test/resources/config.json deleted file mode 100644 index 1d24992..0000000 --- a/api/src/test/resources/config.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "externalApiEnabled" : true, - "baseUrl" : "http://test.test", - "username" : "test-user", - "password" : "test-password" -} diff --git a/api/src/test/resources/config.properties b/api/src/test/resources/config.properties new file mode 100644 index 0000000..d3e99e4 --- /dev/null +++ b/api/src/test/resources/config.properties @@ -0,0 +1,4 @@ +external.api.enabled=true +base.url=http://test.test +username=test-user +password=test-password