Skip to content

Commit

Permalink
Merge pull request #80 from onaio/gateway-enhancements
Browse files Browse the repository at this point in the history
Enhancements and Bug Fixes
  • Loading branch information
ndegwamartin authored Aug 23, 2024
2 parents cd9e528 + 76a9a44 commit e77b133
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 23 deletions.
4 changes: 2 additions & 2 deletions exec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.smartregister</groupId>
<artifactId>opensrp-gateway-plugin</artifactId>
<version>2.0.6</version>
<version>2.0.7</version>
</parent>

<artifactId>exec</artifactId>
Expand Down Expand Up @@ -70,7 +70,7 @@
<dependency>
<groupId>org.smartregister</groupId>
<artifactId>plugins</artifactId>
<version>2.0.6</version>
<version>2.0.7</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.smartregister</groupId>
<artifactId>opensrp-gateway-plugin</artifactId>
<version>2.0.6</version>
<version>2.0.7</version>
</parent>

<artifactId>plugins</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,14 +569,11 @@ private List<OrganizationAffiliation> mapBundleToOrganizationAffiliation(
}

public static List<LocationHierarchy> getLocationsHierarchy(List<String> locationsIdentifiers) {
LocationHierarchyEndpointHelper locationHierarchyEndpointHelper =
new LocationHierarchyEndpointHelper(r4FHIRClient);

return locationsIdentifiers.parallelStream()
.map(
locationsIdentifier ->
locationHierarchyEndpointHelper.getLocationHierarchy(
locationsIdentifier, null, null))
new LocationHierarchyEndpointHelper(r4FHIRClient)
.getLocationHierarchy(locationsIdentifier, null, null))
.filter(
locationHierarchy ->
!org.smartregister.utils.Constants.LOCATION_RESOURCE_NOT_FOUND
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.smartregister.fhir.gateway.plugins;

import static ca.uhn.fhir.rest.api.Constants.PARAM_SUMMARY;
import static org.smartregister.fhir.gateway.plugins.EnvUtil.getEnvironmentVar;

import java.io.FileReader;
Expand Down Expand Up @@ -257,6 +258,7 @@ private IBaseResource processRelatedEntityLocationSyncStrategy(
fhirR4Client.getFhirContext().getRestfulClientFactory().setSocketTimeout(300000);

List<Bundle.BundleEntryComponent> allResults = new ArrayList<>();
int totalResultMatches = 0;

String requestPath =
request.getRequestPath()
Expand Down Expand Up @@ -298,28 +300,35 @@ private IBaseResource processRelatedEntityLocationSyncStrategy(

Bundle res = fhirR4Client.transaction().withBundle(requestBundle).execute();

List<Bundle.BundleEntryComponent> sub =
List<Bundle.BundleEntryComponent> entryComponentList =
res.getEntry().parallelStream()
.map(it -> (Bundle) it.getResource())
.flatMap(it -> it.getEntry().stream())
.collect(Collectors.toList());

allResults.addAll(sub);
allResults.addAll(entryComponentList);
totalResultMatches += entryComponentList.size();
}

resultContent = new BasicResponseHandler().handleResponse(response);

IBaseResource responseResource = this.fhirR4JsonParser.parseResource(resultContent);

if (responseResource instanceof Bundle) {
((Bundle) responseResource).getEntry().addAll(allResults);
((Bundle) responseResource).setTotal(((Bundle) responseResource).getEntry().size());
if (request.getParameters().containsKey(PARAM_SUMMARY)) {
((Bundle) responseResource)
.setTotal(((Bundle) responseResource).getTotal() + totalResultMatches);
} else {

Bundle.BundleLinkComponent selfLinkComponent = new Bundle.BundleLinkComponent();
selfLinkComponent.setRelation(Bundle.LINK_SELF);
selfLinkComponent.setUrl(request.getCompleteUrl());
((Bundle) responseResource).getEntry().addAll(allResults);
((Bundle) responseResource).setTotal(((Bundle) responseResource).getEntry().size());

((Bundle) responseResource).setLink(Collections.singletonList(selfLinkComponent));
Bundle.BundleLinkComponent selfLinkComponent = new Bundle.BundleLinkComponent();
selfLinkComponent.setRelation(Bundle.LINK_SELF);
selfLinkComponent.setUrl(request.getCompleteUrl());

((Bundle) responseResource).setLink(Collections.singletonList(selfLinkComponent));
}
}
return responseResource;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package org.smartregister.fhir.gateway.plugins.endpoint;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smartregister.fhir.gateway.plugins.RestUtils;

import com.google.fhir.gateway.TokenVerifier;
Expand All @@ -14,6 +20,8 @@
import ca.uhn.fhir.parser.IParser;

public abstract class BaseEndpoint extends HttpServlet {
private static final Logger logger = LoggerFactory.getLogger(BaseEndpoint.class);

protected final TokenVerifier tokenVerifier = TokenVerifier.createFromEnvVars();
protected final FhirContext fhirR4Context = FhirContext.forR4();
protected final IParser fhirR4JsonParser = fhirR4Context.newJsonParser().setPrettyPrint(true);
Expand All @@ -24,4 +32,15 @@ protected void doOptions(HttpServletRequest request, HttpServletResponse respons
}

protected BaseEndpoint() throws IOException {}

protected void writeUTF8StringToStream(OutputStream fileOutputStream, String content) {
try (OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
PrintWriter printWriter = new PrintWriter(outputStreamWriter)) {
printWriter.println(content);
printWriter.flush();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
resultContent = fhirR4JsonParser.encodeResourceToString(resultBundle);
}
response.setContentType("application/json");
response.getOutputStream().print(resultContent);
writeUTF8StringToStream(response.getOutputStream(), resultContent);
response.setStatus(HttpStatus.SC_OK);
} catch (AuthenticationException authenticationException) {
response.setContentType("application/json");
response.getOutputStream().print(authenticationException.getMessage());
writeUTF8StringToStream(
response.getOutputStream(), authenticationException.getMessage());
response.setStatus(authenticationException.getStatusCode());
} catch (Exception exception) {
response.setContentType("application/json");
response.getOutputStream().print(exception.getMessage());
writeUTF8StringToStream(response.getOutputStream(), exception.getMessage());
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
}

response.setContentType("application/json");
response.getOutputStream().print(resultContent);
writeUTF8StringToStream(response.getOutputStream(), resultContent);
response.setStatus(HttpStatus.SC_OK);
} catch (AuthenticationException authenticationException) {
response.setContentType("application/json");
response.getOutputStream().print(authenticationException.getMessage());
writeUTF8StringToStream(
response.getOutputStream(), authenticationException.getMessage());
response.setStatus(authenticationException.getStatusCode());
} catch (Exception exception) {
response.setContentType("application/json");
response.getOutputStream().print(exception.getMessage());
writeUTF8StringToStream(response.getOutputStream(), exception.getMessage());
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>org.smartregister</groupId>
<artifactId>opensrp-gateway-plugin</artifactId>
<version>2.0.6</version>
<version>2.0.7</version>
<packaging>pom</packaging>

<modules>
Expand Down

0 comments on commit e77b133

Please sign in to comment.