Skip to content

Commit

Permalink
Add support for removing query params in request mutations (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin authored Oct 4, 2024
1 parent aa5d9cc commit 383ae75
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2021-2023 Google LLC
# Copyright 2021-2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion coverage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.google.fhir.gateway</groupId>
<artifactId>fhir-gateway</artifactId>
<version>0.3.2</version>
<version>0.4.0</version>
</parent>

<groupId>com.google.fhir.gateway</groupId>
Expand Down
2 changes: 1 addition & 1 deletion exec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>com.google.fhir.gateway</groupId>
<artifactId>fhir-gateway</artifactId>
<version>0.3.2</version>
<version>0.4.0</version>
</parent>

<artifactId>exec</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
implementations do not have to do this; they can redeclare those deps. -->
<groupId>com.google.fhir.gateway</groupId>
<artifactId>fhir-gateway</artifactId>
<version>0.3.2</version>
<version>0.4.0</version>
</parent>

<artifactId>plugins</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<groupId>com.google.fhir.gateway</groupId>
<artifactId>fhir-gateway</artifactId>
<version>0.3.2</version>
<version>0.4.0</version>
<packaging>pom</packaging>

<name>FHIR Information Gateway</name>
Expand Down
4 changes: 2 additions & 2 deletions server/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2021-2023 Google LLC
Copyright 2021-2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@
<parent>
<groupId>com.google.fhir.gateway</groupId>
<artifactId>fhir-gateway</artifactId>
<version>0.3.2</version>
<version>0.4.0</version>
</parent>

<artifactId>server</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Google LLC
* Copyright 2021-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -283,12 +283,16 @@ 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.getAdditionalQueryParams())
&& CollectionUtils.isEmpty(mutation.getDiscardQueryParams()))) {
return;
}

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

mutation
.getQueryParams()
.getAdditionalQueryParams()
.forEach((key, value) -> requestDetails.addParameter(key, value.toArray(new String[0])));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Google LLC
* Copyright 2021-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -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 @@ -31,5 +32,9 @@ public class RequestMutation {
// New values overwrites the old one if there is a conflict for a request param (i.e. a returned
// 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<>();
@Builder.Default Map<String, List<String>> additionalQueryParams = 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
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Google LLC
* Copyright 2021-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down 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 @@ -287,7 +289,7 @@ public boolean canAccess() {
}

public RequestMutation getRequestMutation(RequestDetailsReader requestDetailsReader) {
return RequestMutation.builder().queryParams(paramMutations).build();
return RequestMutation.builder().additionalQueryParams(paramMutations).build();
}

public String postProcess(
Expand All @@ -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 383ae75

Please sign in to comment.