From 2924dd02ccffdd920fd662ec0d4b5473a5f7d4e6 Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Date: Wed, 12 Feb 2025 14:47:47 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20Fix=20the=20stream=20close?= =?UTF-8?q?=20bug=20caused=20by=20the=20REL=20fetch=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fhir/gateway/plugins/Constants.java | 11 ++ .../gateway/plugins/SyncAccessDecision.java | 127 +++++++++--------- 2 files changed, 75 insertions(+), 63 deletions(-) diff --git a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/Constants.java b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/Constants.java index 98c1d480..edce534f 100644 --- a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/Constants.java +++ b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/Constants.java @@ -71,6 +71,17 @@ public interface SyncStrategy { String CARE_TEAM = "CareTeam"; } + public interface ResourceType { + String LOCATION = "Location"; + } + + public interface HttpMethods{ + String POST = "POST"; + String GET = "GET"; + String PUT = "PUT"; + String PATCH = "PATCH"; + } + public interface Header { @Deprecated String FHIR_GATEWAY_MODE = "fhir-gateway-mode"; String MODE = "mode"; diff --git a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java index 6ea91050..72ee4880 100755 --- a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java +++ b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java @@ -1,49 +1,41 @@ 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; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.parser.IParser; +import ca.uhn.fhir.rest.api.RequestTypeEnum; +import ca.uhn.fhir.rest.client.api.IGenericClient; +import ca.uhn.fhir.rest.server.exceptions.ForbiddenOperationException; +import com.google.common.annotations.VisibleForTesting; +import com.google.fhir.gateway.ExceptionUtil; +import com.google.fhir.gateway.ProxyConstants; +import com.google.fhir.gateway.interfaces.AccessDecision; +import com.google.fhir.gateway.interfaces.RequestDetailsReader; +import com.google.fhir.gateway.interfaces.RequestMutation; +import com.google.gson.Gson; +import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; +import lombok.Getter; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.impl.client.BasicResponseHandler; +import org.apache.http.util.EntityUtils; import org.apache.http.util.TextUtils; import org.hl7.fhir.instance.model.api.IBaseResource; -import org.hl7.fhir.r4.model.Bundle; -import org.hl7.fhir.r4.model.ListResource; -import org.hl7.fhir.r4.model.Location; -import org.hl7.fhir.r4.model.OperationOutcome; -import org.hl7.fhir.r4.model.Resource; +import org.hl7.fhir.r4.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.smartregister.helpers.LocationHelper; -import com.google.common.annotations.VisibleForTesting; -import com.google.fhir.gateway.ExceptionUtil; -import com.google.fhir.gateway.ProxyConstants; -import com.google.fhir.gateway.interfaces.AccessDecision; -import com.google.fhir.gateway.interfaces.RequestDetailsReader; -import com.google.fhir.gateway.interfaces.RequestMutation; -import com.google.gson.Gson; +import java.io.FileReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.stream.Collectors; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.parser.IParser; -import ca.uhn.fhir.rest.api.RequestTypeEnum; -import ca.uhn.fhir.rest.client.api.IGenericClient; -import ca.uhn.fhir.rest.server.exceptions.ForbiddenOperationException; -import jakarta.annotation.Nonnull; -import jakarta.annotation.Nullable; -import lombok.Getter; +import static ca.uhn.fhir.rest.api.Constants.PARAM_SUMMARY; +import static org.smartregister.fhir.gateway.plugins.EnvUtil.getEnvironmentVar; public class SyncAccessDecision implements AccessDecision { public static final String SYNC_FILTER_IGNORE_RESOURCES_FILE_ENV = @@ -108,7 +100,7 @@ public RequestMutation getRequestMutation(RequestDetailsReader requestDetailsRea if (syncStrategyIdsMap.isEmpty() || StringUtils.isBlank(syncStrategy) || (syncStrategyIdsMap.containsKey(syncStrategy) - && syncStrategyIdsMap.get(syncStrategy).isEmpty())) { + && syncStrategyIdsMap.get(syncStrategy).isEmpty())) { ForbiddenOperationException forbiddenOperationException = new ForbiddenOperationException( @@ -205,7 +197,9 @@ private List addSyncFilters(Map syncTags) { return paramValues; } - /** NOTE: Always return a null whenever you want to skip post-processing */ + /** + * NOTE: Always return a null whenever you want to skip post-processing + */ @Override public String postProcess(RequestDetailsReader request, HttpResponse response) throws IOException { @@ -239,7 +233,9 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) resultContent = this.fhirR4JsonParser.encodeResourceToString(resultContentBundle); - } else if (Constants.SyncStrategy.RELATED_ENTITY_LOCATION.equals(syncStrategy)) { + } else if (Constants.SyncStrategy.RELATED_ENTITY_LOCATION.equals(syncStrategy) && + (Constants.HttpMethods.GET.equals(request.getRequestType().name()) + || Constants.HttpMethods.PATCH.equals(request.getRequestType().name()))) { IBaseResource responseResource = processRelatedEntityLocationSyncStrategy(request, response); @@ -253,9 +249,10 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) resultContent = this.fhirR4JsonParser.encodeResourceToString(practitionerDetailsBundle); } - if (Constants.SyncStrategy.LOCATION.equals(request.getResourceName()) - && ("POST".equals(request.getRequestType().name()) - || "PUT".equals(request.getRequestType().name()))) { + if (Constants.ResourceType.LOCATION.equals(request.getResourceName()) + && (Constants.HttpMethods.POST.equals(request.getRequestType().name()) + || Constants.HttpMethods.PUT.equals(request.getRequestType().name()))) { + resultContent = new BasicResponseHandler().handleResponse(response); String requestPath = request.getRequestPath(); String locationId = getLocationId(requestPath, resultContent); @@ -297,11 +294,11 @@ private IBaseResource processRelatedEntityLocationSyncStrategy( + getRequestParametersString(request.getParameters()); for (int startIndex = SyncAccessDecisionConstants.REL_LOCATION_INITIAL_CHUNK_SIZE; - startIndex - < syncStrategyIdsMap - .get(Constants.SyncStrategy.RELATED_ENTITY_LOCATION) - .size(); - startIndex += SyncAccessDecisionConstants.REL_LOCATION_CHUNK_SIZE) { + startIndex + < syncStrategyIdsMap + .get(Constants.SyncStrategy.RELATED_ENTITY_LOCATION) + .size(); + startIndex += SyncAccessDecisionConstants.REL_LOCATION_CHUNK_SIZE) { int endIndex = Math.min( @@ -431,20 +428,20 @@ private Bundle processListEntriesGatewayModeByBundle( List bundleEntryComponentList = ((Bundle) responseResource) .getEntry().stream() - .filter(it -> it.getResource() instanceof ListResource) - .flatMap( - bundleEntryComponent -> - ((ListResource) bundleEntryComponent.getResource()) - .getEntry().stream()) - .skip(start) - .limit(count) - .map( - listEntryComponent -> - createBundleEntryComponent( - Bundle.HTTPVerb.GET, - listEntryComponent.getItem().getReference(), - null)) - .collect(Collectors.toList()); + .filter(it -> it.getResource() instanceof ListResource) + .flatMap( + bundleEntryComponent -> + ((ListResource) bundleEntryComponent.getResource()) + .getEntry().stream()) + .skip(start) + .limit(count) + .map( + listEntryComponent -> + createBundleEntryComponent( + Bundle.HTTPVerb.GET, + listEntryComponent.getItem().getReference(), + null)) + .collect(Collectors.toList()); return requestBundle.setEntry(bundleEntryComponentList); } @@ -656,7 +653,7 @@ private boolean shouldSkipDataFiltering(RequestDetailsReader requestDetailsReade if (entry.getMethodType() != null && !entry.getMethodType() - .equals(requestDetailsReader.getRequestType().name())) { + .equals(requestDetailsReader.getRequestType().name())) { continue; } @@ -706,10 +703,14 @@ public void setFhirR4Client(IGenericClient fhirR4Client) { } class IgnoredResourcesConfig { - @Getter List entries; - @Getter private String path; - @Getter private String methodType; - @Getter private Map queryParams; + @Getter + List entries; + @Getter + private String path; + @Getter + private String methodType; + @Getter + private Map queryParams; @Override public String toString() { From c00de451078ed0a2ee50f27a57178af928f86843 Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Date: Wed, 12 Feb 2025 14:51:00 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20Upgrade=20versions=20a?= =?UTF-8?q?nd=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exec/pom.xml | 4 +- plugins/pom.xml | 2 +- .../fhir/gateway/plugins/Constants.java | 2 +- .../gateway/plugins/SyncAccessDecision.java | 113 ++++++++---------- pom.xml | 2 +- 5 files changed, 58 insertions(+), 65 deletions(-) diff --git a/exec/pom.xml b/exec/pom.xml index d20e49de..c5f30f52 100755 --- a/exec/pom.xml +++ b/exec/pom.xml @@ -4,7 +4,7 @@ org.smartregister opensrp-gateway-plugin - 3.0.0 + 3.0.1 exec @@ -70,7 +70,7 @@ org.smartregister plugins - 3.0.0 + 3.0.1 diff --git a/plugins/pom.xml b/plugins/pom.xml index b0f31f7c..d3a7b9f5 100644 --- a/plugins/pom.xml +++ b/plugins/pom.xml @@ -4,7 +4,7 @@ org.smartregister opensrp-gateway-plugin - 3.0.0 + 3.0.1 plugins diff --git a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/Constants.java b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/Constants.java index edce534f..783c0a23 100644 --- a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/Constants.java +++ b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/Constants.java @@ -75,7 +75,7 @@ public interface ResourceType { String LOCATION = "Location"; } - public interface HttpMethods{ + public interface HttpMethods { String POST = "POST"; String GET = "GET"; String PUT = "PUT"; diff --git a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java index 72ee4880..eceb0fdb 100755 --- a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java +++ b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java @@ -1,26 +1,17 @@ package org.smartregister.fhir.gateway.plugins; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.parser.IParser; -import ca.uhn.fhir.rest.api.RequestTypeEnum; -import ca.uhn.fhir.rest.client.api.IGenericClient; -import ca.uhn.fhir.rest.server.exceptions.ForbiddenOperationException; -import com.google.common.annotations.VisibleForTesting; -import com.google.fhir.gateway.ExceptionUtil; -import com.google.fhir.gateway.ProxyConstants; -import com.google.fhir.gateway.interfaces.AccessDecision; -import com.google.fhir.gateway.interfaces.RequestDetailsReader; -import com.google.fhir.gateway.interfaces.RequestMutation; -import com.google.gson.Gson; -import jakarta.annotation.Nonnull; -import jakarta.annotation.Nullable; -import lombok.Getter; +import static ca.uhn.fhir.rest.api.Constants.PARAM_SUMMARY; +import static org.smartregister.fhir.gateway.plugins.EnvUtil.getEnvironmentVar; + +import java.io.FileReader; +import java.io.IOException; +import java.util.*; +import java.util.stream.Collectors; + import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.impl.client.BasicResponseHandler; -import org.apache.http.util.EntityUtils; import org.apache.http.util.TextUtils; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.*; @@ -28,14 +19,22 @@ import org.slf4j.LoggerFactory; import org.smartregister.helpers.LocationHelper; -import java.io.FileReader; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.*; -import java.util.stream.Collectors; +import com.google.common.annotations.VisibleForTesting; +import com.google.fhir.gateway.ExceptionUtil; +import com.google.fhir.gateway.ProxyConstants; +import com.google.fhir.gateway.interfaces.AccessDecision; +import com.google.fhir.gateway.interfaces.RequestDetailsReader; +import com.google.fhir.gateway.interfaces.RequestMutation; +import com.google.gson.Gson; -import static ca.uhn.fhir.rest.api.Constants.PARAM_SUMMARY; -import static org.smartregister.fhir.gateway.plugins.EnvUtil.getEnvironmentVar; +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.parser.IParser; +import ca.uhn.fhir.rest.api.RequestTypeEnum; +import ca.uhn.fhir.rest.client.api.IGenericClient; +import ca.uhn.fhir.rest.server.exceptions.ForbiddenOperationException; +import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; +import lombok.Getter; public class SyncAccessDecision implements AccessDecision { public static final String SYNC_FILTER_IGNORE_RESOURCES_FILE_ENV = @@ -100,7 +99,7 @@ public RequestMutation getRequestMutation(RequestDetailsReader requestDetailsRea if (syncStrategyIdsMap.isEmpty() || StringUtils.isBlank(syncStrategy) || (syncStrategyIdsMap.containsKey(syncStrategy) - && syncStrategyIdsMap.get(syncStrategy).isEmpty())) { + && syncStrategyIdsMap.get(syncStrategy).isEmpty())) { ForbiddenOperationException forbiddenOperationException = new ForbiddenOperationException( @@ -197,9 +196,7 @@ private List addSyncFilters(Map syncTags) { return paramValues; } - /** - * NOTE: Always return a null whenever you want to skip post-processing - */ + /** NOTE: Always return a null whenever you want to skip post-processing */ @Override public String postProcess(RequestDetailsReader request, HttpResponse response) throws IOException { @@ -233,9 +230,9 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) resultContent = this.fhirR4JsonParser.encodeResourceToString(resultContentBundle); - } else if (Constants.SyncStrategy.RELATED_ENTITY_LOCATION.equals(syncStrategy) && - (Constants.HttpMethods.GET.equals(request.getRequestType().name()) - || Constants.HttpMethods.PATCH.equals(request.getRequestType().name()))) { + } else if (Constants.SyncStrategy.RELATED_ENTITY_LOCATION.equals(syncStrategy) + && (Constants.HttpMethods.GET.equals(request.getRequestType().name()) + || Constants.HttpMethods.PATCH.equals(request.getRequestType().name()))) { IBaseResource responseResource = processRelatedEntityLocationSyncStrategy(request, response); @@ -251,7 +248,7 @@ public String postProcess(RequestDetailsReader request, HttpResponse response) if (Constants.ResourceType.LOCATION.equals(request.getResourceName()) && (Constants.HttpMethods.POST.equals(request.getRequestType().name()) - || Constants.HttpMethods.PUT.equals(request.getRequestType().name()))) { + || Constants.HttpMethods.PUT.equals(request.getRequestType().name()))) { resultContent = new BasicResponseHandler().handleResponse(response); String requestPath = request.getRequestPath(); @@ -294,11 +291,11 @@ private IBaseResource processRelatedEntityLocationSyncStrategy( + getRequestParametersString(request.getParameters()); for (int startIndex = SyncAccessDecisionConstants.REL_LOCATION_INITIAL_CHUNK_SIZE; - startIndex - < syncStrategyIdsMap - .get(Constants.SyncStrategy.RELATED_ENTITY_LOCATION) - .size(); - startIndex += SyncAccessDecisionConstants.REL_LOCATION_CHUNK_SIZE) { + startIndex + < syncStrategyIdsMap + .get(Constants.SyncStrategy.RELATED_ENTITY_LOCATION) + .size(); + startIndex += SyncAccessDecisionConstants.REL_LOCATION_CHUNK_SIZE) { int endIndex = Math.min( @@ -428,20 +425,20 @@ private Bundle processListEntriesGatewayModeByBundle( List bundleEntryComponentList = ((Bundle) responseResource) .getEntry().stream() - .filter(it -> it.getResource() instanceof ListResource) - .flatMap( - bundleEntryComponent -> - ((ListResource) bundleEntryComponent.getResource()) - .getEntry().stream()) - .skip(start) - .limit(count) - .map( - listEntryComponent -> - createBundleEntryComponent( - Bundle.HTTPVerb.GET, - listEntryComponent.getItem().getReference(), - null)) - .collect(Collectors.toList()); + .filter(it -> it.getResource() instanceof ListResource) + .flatMap( + bundleEntryComponent -> + ((ListResource) bundleEntryComponent.getResource()) + .getEntry().stream()) + .skip(start) + .limit(count) + .map( + listEntryComponent -> + createBundleEntryComponent( + Bundle.HTTPVerb.GET, + listEntryComponent.getItem().getReference(), + null)) + .collect(Collectors.toList()); return requestBundle.setEntry(bundleEntryComponentList); } @@ -653,7 +650,7 @@ private boolean shouldSkipDataFiltering(RequestDetailsReader requestDetailsReade if (entry.getMethodType() != null && !entry.getMethodType() - .equals(requestDetailsReader.getRequestType().name())) { + .equals(requestDetailsReader.getRequestType().name())) { continue; } @@ -703,14 +700,10 @@ public void setFhirR4Client(IGenericClient fhirR4Client) { } class IgnoredResourcesConfig { - @Getter - List entries; - @Getter - private String path; - @Getter - private String methodType; - @Getter - private Map queryParams; + @Getter List entries; + @Getter private String path; + @Getter private String methodType; + @Getter private Map queryParams; @Override public String toString() { diff --git a/pom.xml b/pom.xml index d769f7f9..1c9206b7 100755 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.smartregister opensrp-gateway-plugin - 3.0.0 + 3.0.1 pom From c943ae118db3be5aa96fc1f0abb4e5ee2d3f40b8 Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Date: Wed, 12 Feb 2025 15:03:38 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=8E=A8=20Format=20imports=20correctly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fhir/gateway/plugins/SyncAccessDecision.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java index eceb0fdb..94ebb37f 100755 --- a/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java +++ b/plugins/src/main/java/org/smartregister/fhir/gateway/plugins/SyncAccessDecision.java @@ -5,7 +5,12 @@ import java.io.FileReader; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; @@ -14,7 +19,11 @@ import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.util.TextUtils; import org.hl7.fhir.instance.model.api.IBaseResource; -import org.hl7.fhir.r4.model.*; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.ListResource; +import org.hl7.fhir.r4.model.Location; +import org.hl7.fhir.r4.model.OperationOutcome; +import org.hl7.fhir.r4.model.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.smartregister.helpers.LocationHelper;