Skip to content

Commit

Permalink
🐛 Fix the stream close bug caused by the REL fetch check
Browse files Browse the repository at this point in the history
  • Loading branch information
dubdabasoduba committed Feb 12, 2025
1 parent 6fe8901 commit 2924dd0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -205,7 +197,9 @@ private List<String> addSyncFilters(Map<String, String[]> 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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -431,20 +428,20 @@ private Bundle processListEntriesGatewayModeByBundle(
List<Bundle.BundleEntryComponent> 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);
}
Expand Down Expand Up @@ -656,7 +653,7 @@ private boolean shouldSkipDataFiltering(RequestDetailsReader requestDetailsReade

if (entry.getMethodType() != null
&& !entry.getMethodType()
.equals(requestDetailsReader.getRequestType().name())) {
.equals(requestDetailsReader.getRequestType().name())) {
continue;
}

Expand Down Expand Up @@ -706,10 +703,14 @@ public void setFhirR4Client(IGenericClient fhirR4Client) {
}

class IgnoredResourcesConfig {
@Getter List<IgnoredResourcesConfig> entries;
@Getter private String path;
@Getter private String methodType;
@Getter private Map<String, Object> queryParams;
@Getter
List<IgnoredResourcesConfig> entries;
@Getter
private String path;
@Getter
private String methodType;
@Getter
private Map<String, Object> queryParams;

@Override
public String toString() {
Expand Down

0 comments on commit 2924dd0

Please sign in to comment.