Skip to content

Commit

Permalink
Merge pull request #2 from SoftmedTanzania/implement-hrhis-send-pract…
Browse files Browse the repository at this point in the history
…itioners-updates

Implement HRHIS send practitioners updates to NHIF
  • Loading branch information
cozej4 committed Oct 19, 2021
2 parents 791a33a + a7f274d commit 78a5001
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,107 @@
package tz.go.moh.him.nhif.mediator.hrhis;

import akka.actor.ActorSelection;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import org.apache.http.HttpStatus;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.HttpHeaders;
import org.json.JSONObject;
import org.openhim.mediator.engine.MediatorConfig;
import org.openhim.mediator.engine.messages.FinishRequest;
import org.openhim.mediator.engine.messages.MediatorHTTPRequest;
import org.openhim.mediator.engine.messages.MediatorHTTPResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefaultOrchestrator extends UntypedActor {
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
/**
* The logger instance.
*/
protected final LoggingAdapter log = Logging.getLogger(getContext().system(), this);

private final MediatorConfig config;
/**
* The mediator configuration.
*/
protected final MediatorConfig config;

/**
* Represents a mediator request.
*/
protected MediatorHTTPRequest workingRequest;

/**
* Initializes a new instance of the {@link DefaultOrchestrator} class.
*
* @param config The mediator configuration.
*/
public DefaultOrchestrator(MediatorConfig config) {
this.config = config;
}

/**
* Handles the received message.
*
* @param msg The received message.
*/
@Override
public void onReceive(Object msg) throws Exception {
if (msg instanceof MediatorHTTPRequest) {
FinishRequest finishRequest = new FinishRequest("A message from my new mediator!", "text/plain", HttpStatus.SC_OK);
((MediatorHTTPRequest) msg).getRequestHandler().tell(finishRequest, getSelf());

workingRequest = (MediatorHTTPRequest) msg;

log.info("Received request: " + workingRequest.getHost() + " " + workingRequest.getMethod() + " " + workingRequest.getPath());

Map<String, String> headers = new HashMap<>();

headers.put(HttpHeaders.CONTENT_TYPE, "application/json");

List<Pair<String, String>> parameters = new ArrayList<>();


String host;
int port;
String path;
String scheme;

if (config.getDynamicConfig().isEmpty()) {
log.debug("Dynamic config is empty, using config from mediator.properties");

host = config.getProperty("destination.host");
port = Integer.parseInt(config.getProperty("destination.port"));
path = config.getProperty("destination.path");
scheme = config.getProperty("destination.scheme");
} else {
log.debug("Using dynamic config");

JSONObject destinationProperties = new JSONObject(config.getDynamicConfig()).getJSONObject("destinationConnectionProperties");

host = destinationProperties.getString("destinationHost");
port = destinationProperties.getInt("destinationPort");
path = destinationProperties.getString("destinationPath");
scheme = destinationProperties.getString("destinationScheme");

if (destinationProperties.has("destinationAuthorization")) {
String authorization = destinationProperties.getString("destinationAuthorization");
if (authorization != null && !"".equals(authorization)) {
headers.put(HttpHeaders.AUTHORIZATION, authorization);
}

}
}

host = scheme + "://" + host + ":" + port + path;

MediatorHTTPRequest request = new MediatorHTTPRequest(workingRequest.getRequestHandler(), getSelf(), host, "POST",
host, workingRequest.getBody(), headers, parameters);

ActorSelection httpConnector = getContext().actorSelection(config.userPathFor("http-connector"));
httpConnector.tell(request, getSelf());

} else if (msg instanceof MediatorHTTPResponse) {
workingRequest.getRequestHandler().tell(((MediatorHTTPResponse) msg).toFinishRequest(), getSelf());
} else {
unhandled(msg);
}
Expand Down
38 changes: 29 additions & 9 deletions src/main/java/tz/go/moh/him/nhif/mediator/hrhis/MediatorMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,37 @@
import java.util.Properties;

public class MediatorMain {

/**
* Builds the routing table.
*
* @return Returns the routing table.
* @throws RoutingTable.RouteAlreadyMappedException if the route is already mapped
*/
private static RoutingTable buildRoutingTable() throws RoutingTable.RouteAlreadyMappedException {
RoutingTable routingTable = new RoutingTable();

//TODO Configure routes here
//...
routingTable.addRoute("/mediator", DefaultOrchestrator.class);
routingTable.addRoute("/nhif", DefaultOrchestrator.class);

return routingTable;
}

/**
* Builds the startup actors configuration.
*
* @return Returns the startup actors configuration.
*/
private static StartupActorsConfig buildStartupActorsConfig() {
StartupActorsConfig startupActors = new StartupActorsConfig();

//TODO Add own startup actors here
//...

return startupActors;
}

/**
* Loads the configuration.
*
* @param configPath The path of the configuration.
* @return Returns the configuration instance.
* @throws IOException if an IO exception occurs
* @throws RoutingTable.RouteAlreadyMappedException if the route is already mapped
*/
private static MediatorConfig loadConfig(String configPath) throws IOException, RoutingTable.RouteAlreadyMappedException {
MediatorConfig config = new MediatorConfig();

Expand Down Expand Up @@ -73,6 +84,12 @@ private static MediatorConfig loadConfig(String configPath) throws IOException,
return config;
}

/**
* The main entry point of the application.
*
* @param args The arguments.
* @throws Exception if an exception occurs
*/
public static void main(String... args) throws Exception {
//setup actor system
final ActorSystem system = ActorSystem.create("mediator");
Expand All @@ -91,6 +108,9 @@ public static void main(String... args) throws Exception {
}

MediatorConfig config = loadConfig(configPath);

config.setSSLContext(new MediatorConfig.SSLContext(true));

final MediatorServer server = new MediatorServer(system, config);

//setup shutdown hook
Expand Down
54 changes: 49 additions & 5 deletions src/main/resources/mediator-registration-info.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,70 @@
"name": "NHIF Mediator HRHIS Route",
"host": "localhost",
"port": "3104",
"path": "/mediator",
"path": "/nhif",
"type": "http"
}
],
"defaultChannelConfig": [
{
"name": "NHIF Mediator HRHIS",
"urlPattern": "^/mediator$",
"urlPattern": "^/nhif$",
"type": "http",
"allow": ["nhifmediatorhrhis"],
"allow": ["hrhis-role"],
"routes": [
{
"name": "NHIF Mediator HRHIS Route",
"name": "HRHIS - NHIF Route",
"host": "localhost",
"port": "3104",
"path": "/mediator",
"path": "/nhif",
"type": "http",
"primary": "true"
}
]
}
],
"configDefs": [
{
"param": "destinationConnectionProperties",
"displayName": "Destination Connection Properties",
"description": "Configuration to set the hostname, port and path for the destination server",
"type": "struct",
"template": [
{
"param": "destinationHost",
"displayName": "Destination Host Name",
"description": "IP address/hostname of the destination server. e.g 192.168.1.1",
"type": "string"
},
{
"param": "destinationPort",
"displayName": "Destination Port Number",
"description": "The port number of the destination server. e.g 8080",
"type": "number"
},
{
"param": "destinationPath",
"displayName": "Destination Path",
"description": "The destination path for receiving of data from the HIM. eg /hdr",
"type": "string"
},
{
"param": "destinationScheme",
"displayName": "Destination Scheme",
"description": "Whether the destination is using HTTP or HTTPS requests",
"type": "option",
"values": [
"http",
"https"
]
},
{
"param": "destinationAuthorization",
"displayName": "Destination Authorization",
"description": "The destination authorization credentials for receiving data from the HIM.",
"type": "string"
}
]
}
]
}
7 changes: 6 additions & 1 deletion src/main/resources/mediator.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
mediator.name=NHIF-Mediator-HRHIS
mediator.host=localhost
mediator.port=3104
mediator.timeout=60000
mediator.timeout=600000
mediator.heartbeats=true

core.host=localhost
core.api.port=8080
core.api.user=root@openhim.org
core.api.password=openhim-password

destination.host=reqbin.com
destination.port=443
destination.path=/echo/post/json
destination.scheme=https

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tz.go.moh.him.nhif.mediator.hrhis.main;

import org.junit.Assert;
import org.junit.Test;
import org.openhim.mediator.engine.MediatorConfig;
import tz.go.moh.him.nhif.mediator.hrhis.MediatorMain;

import java.lang.reflect.Method;

/**
* Contains tests for the {@link MediatorMain} class.
*/
public class MediatorMainTest {

/**
* Test the mediator main class loading the configuration.
*
* @throws Exception
*/
@Test
public void mediatorMainTest() throws Exception {

Method loadConfigMethod = MediatorMain.class.getDeclaredMethod("loadConfig", String.class);

loadConfigMethod.setAccessible(true);
MediatorConfig mediatorConfig = (MediatorConfig) loadConfigMethod.invoke(null, "src/test/resources/mediator.properties");

Assert.assertEquals("localhost", mediatorConfig.getServerHost());
Assert.assertEquals(new Integer(3104), mediatorConfig.getServerPort());
Assert.assertEquals(new Integer(600000), mediatorConfig.getRootTimeout());
Assert.assertTrue(mediatorConfig.getHeartsbeatEnabled());
}
}

0 comments on commit 78a5001

Please sign in to comment.