Skip to content

Commit

Permalink
Add support for removing query params in request mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Oct 2, 2024
1 parent f0788a6 commit a5389ce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,14 @@ private void serveWellKnown(ServletRequestDetails request) {
void mutateRequest(RequestDetails requestDetails, AccessDecision accessDecision) {
RequestMutation mutation =
accessDecision.getRequestMutation(new RequestDetailsToReader(requestDetails));
if (mutation == null || CollectionUtils.isEmpty(mutation.getQueryParams())) {
if (mutation == null
|| (CollectionUtils.isEmpty(mutation.getQueryParams())
&& CollectionUtils.isEmpty(mutation.getDiscardQueryParams()))) {
return;
}

mutation.getDiscardQueryParams().forEach(requestDetails::removeParameter);

mutation
.getQueryParams()
.forEach((key, value) -> requestDetails.addParameter(key, value.toArray(new String[0])));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.fhir.gateway.interfaces;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -32,4 +33,8 @@ public class RequestMutation {
// parameter in RequestMutation is already present in the original request).
// Old parameter values should be explicitly retained while mutating values for that parameter.
@Builder.Default Map<String, List<String>> queryParams = new HashMap<>();

// Query parameters that are no longer needed when forwarding the request to the upstream server
// Parameters with the keys in this list will be removed
@Builder.Default List<String> discardQueryParams = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
import java.io.Writer;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.hamcrest.Matchers;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.CapabilityStatement;
import org.junit.Before;
Expand Down Expand Up @@ -307,6 +309,43 @@ public String postProcess(
arrayContainingInAnyOrder("param3-value2", "param3-value1"));
}

@Test
public void mutateRequestRemoveQueryParams() {

List<String> queryParamsToRemove = new ArrayList<>();
queryParamsToRemove.add("param1");

ServletRequestDetails requestDetails = new ServletRequestDetails();
requestDetails.addParameter("param1", new String[] {"param1-value1"});
requestDetails.addParameter("param2", new String[] {"param2-value1"});

AccessDecision mutableAccessDecision =
new AccessDecision() {
public boolean canAccess() {
return true;
}

public RequestMutation getRequestMutation(RequestDetailsReader requestDetailsReader) {
RequestMutation requestMutation = RequestMutation.builder().build();
requestMutation.getDiscardQueryParams().addAll(queryParamsToRemove);
return requestMutation;
}

public String postProcess(
RequestDetailsReader requestDetailsReader, HttpResponse response) throws IOException {
return null;
}
};
assertThat(requestDetails.getParameters().size(), Matchers.equalTo(2));

testInstance.mutateRequest(requestDetails, mutableAccessDecision);

assertThat(requestDetails.getParameters().size(), Matchers.equalTo(1));

assertThat(
requestDetails.getParameters().get("param2"), arrayContainingInAnyOrder("param2-value1"));
}

@Test
public void shouldSendGzippedResponseWhenRequested() throws IOException {
testInstance = createTestInstance(true, null);
Expand Down

0 comments on commit a5389ce

Please sign in to comment.