From 4c1c78938fc86a71ee92f47cc78cb1f16954d0e5 Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 14 Jan 2025 11:56:51 -0500 Subject: [PATCH 1/3] Use URLEncoder.encode, deprecate worse named method --- .../hl7/fhir/dstu2/utils/client/FHIRToolingClient.java | 2 +- .../hl7/fhir/dstu2/utils/client/ResourceAddress.java | 2 +- .../dstu2016may/utils/client/FHIRToolingClient.java | 2 +- .../fhir/dstu2016may/utils/client/ResourceAddress.java | 2 +- .../hl7/fhir/dstu3/utils/client/FHIRToolingClient.java | 2 +- .../hl7/fhir/dstu3/utils/client/ResourceAddress.java | 2 +- .../hl7/fhir/r4/utils/client/FHIRToolingClient.java | 2 +- .../org/hl7/fhir/r4/utils/client/ResourceAddress.java | 2 +- .../hl7/fhir/r4b/utils/client/FHIRToolingClient.java | 2 +- .../org/hl7/fhir/r4b/utils/client/ResourceAddress.java | 2 +- .../hl7/fhir/r5/utils/client/FHIRToolingClient.java | 2 +- .../org/hl7/fhir/r5/utils/client/ResourceAddress.java | 2 +- .../main/java/org/hl7/fhir/utilities/Utilities.java | 10 +++++++--- 13 files changed, 19 insertions(+), 15 deletions(-) diff --git a/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/client/FHIRToolingClient.java b/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/client/FHIRToolingClient.java index 38b7852733..07c289008a 100644 --- a/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/client/FHIRToolingClient.java +++ b/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/client/FHIRToolingClient.java @@ -342,7 +342,7 @@ public Parameters operateType(Class resourceClass, Strin if (!complex) for (ParametersParameterComponent p : params.getParameter()) if (p.getValue() instanceof PrimitiveType) - ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&"; + ps += p.getName() + "=" + Utilities.encodeUriParam(((PrimitiveType) p.getValue()).asStringValue()) + "&"; ResourceRequest result; if (complex) result = utils.issuePostRequest(resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps), diff --git a/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/client/ResourceAddress.java b/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/client/ResourceAddress.java index e58b6fdaa0..8d70647a3d 100644 --- a/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/client/ResourceAddress.java +++ b/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/client/ResourceAddress.java @@ -426,7 +426,7 @@ public static URI appendHttpParameters(URI basePath, Map paramet } else { query = ""; } - query += httpParameterName + "=" + Utilities.encodeUri(parameters.get(httpParameterName)); + query += httpParameterName + "=" + Utilities.encodeUriParam(parameters.get(httpParameterName)); } return new URI(basePath.getScheme(), basePath.getUserInfo(), basePath.getHost(), basePath.getPort(), diff --git a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/client/FHIRToolingClient.java b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/client/FHIRToolingClient.java index 6379ac3345..3d5c1de917 100644 --- a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/client/FHIRToolingClient.java +++ b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/client/FHIRToolingClient.java @@ -522,7 +522,7 @@ public Parameters operateType(Class resourceClass, Strin if (!complex) for (ParametersParameterComponent p : params.getParameter()) if (p.getValue() instanceof PrimitiveType) - ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&"; + ps += p.getName() + "=" + Utilities.encodeUriParam(((PrimitiveType) p.getValue()).asStringValue()) + "&"; ResourceRequest result; if (complex) result = utils.issuePostRequest(resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps), diff --git a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/client/ResourceAddress.java b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/client/ResourceAddress.java index 835d38284a..9d92fcb0c9 100644 --- a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/client/ResourceAddress.java +++ b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/client/ResourceAddress.java @@ -427,7 +427,7 @@ public static URI appendHttpParameters(URI basePath, Map paramet } else { query = ""; } - query += httpParameterName + "=" + Utilities.encodeUri(parameters.get(httpParameterName)); + query += httpParameterName + "=" + Utilities.encodeUriParam(parameters.get(httpParameterName)); } return new URI(basePath.getScheme(), basePath.getUserInfo(), basePath.getHost(), basePath.getPort(), diff --git a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/FHIRToolingClient.java b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/FHIRToolingClient.java index 25d6fedd3f..a6484c3154 100644 --- a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/FHIRToolingClient.java +++ b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/FHIRToolingClient.java @@ -312,7 +312,7 @@ public Parameters operateType(Class resourceClass, Strin if (!complex) for (ParametersParameterComponent p : params.getParameter()) if (p.getValue() instanceof PrimitiveType) - ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&"; + ps += p.getName() + "=" + Utilities.encodeUriParam(((PrimitiveType) p.getValue()).asStringValue()) + "&"; ResourceRequest result; URI url = resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps); if (complex) { diff --git a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/ResourceAddress.java b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/ResourceAddress.java index b32b33e9a0..297c2983f2 100644 --- a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/ResourceAddress.java +++ b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/utils/client/ResourceAddress.java @@ -418,7 +418,7 @@ public static URI appendHttpParameters(URI basePath, Map paramete } else { query = ""; } - query += httpParameterName + "=" + Utilities.encodeUri(parameters.get(httpParameterName)); + query += httpParameterName + "=" + Utilities.encodeUriParam(parameters.get(httpParameterName)); } return new URI(basePath.getScheme(), basePath.getUserInfo(), basePath.getHost(),basePath.getPort(), basePath.getPath(), query, basePath.getFragment()); diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/client/FHIRToolingClient.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/client/FHIRToolingClient.java index 46d4f148c8..2e1ba931de 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/client/FHIRToolingClient.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/client/FHIRToolingClient.java @@ -288,7 +288,7 @@ public Parameters operateType(Class resourceClass, Strin if (!complex) for (ParametersParameterComponent p : params.getParameter()) if (p.getValue() instanceof PrimitiveType) - ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&"; + ps += p.getName() + "=" + Utilities.encodeUriParam(((PrimitiveType) p.getValue()).asStringValue()) + "&"; ResourceRequest result; URI url = resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps); if (complex) { diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/client/ResourceAddress.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/client/ResourceAddress.java index 20a51e2ee7..25f2ddc396 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/client/ResourceAddress.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/utils/client/ResourceAddress.java @@ -433,7 +433,7 @@ public static URI appendHttpParameters(URI basePath, Map paramet } else { query = ""; } - query += httpParameterName + "=" + Utilities.encodeUri(parameters.get(httpParameterName)); + query += httpParameterName + "=" + Utilities.encodeUriParam(parameters.get(httpParameterName)); } return new URI(basePath.getScheme(), basePath.getUserInfo(), basePath.getHost(), basePath.getPort(), diff --git a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/client/FHIRToolingClient.java b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/client/FHIRToolingClient.java index 9df16205b0..e09e260366 100644 --- a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/client/FHIRToolingClient.java +++ b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/client/FHIRToolingClient.java @@ -301,7 +301,7 @@ public Parameters operateType(Class resourceClass, Strin if (!complex) for (ParametersParameterComponent p : params.getParameter()) if (p.getValue() instanceof PrimitiveType) - ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&"; + ps += p.getName() + "=" + Utilities.encodeUriParam(((PrimitiveType) p.getValue()).asStringValue()) + "&"; ResourceRequest result; URI url = resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps); if (complex) { diff --git a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/client/ResourceAddress.java b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/client/ResourceAddress.java index 7a8b5b53bf..98f49deab2 100644 --- a/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/client/ResourceAddress.java +++ b/org.hl7.fhir.r4b/src/main/java/org/hl7/fhir/r4b/utils/client/ResourceAddress.java @@ -424,7 +424,7 @@ public static URI appendHttpParameters(URI basePath, Map paramet } else { query = ""; } - query += httpParameterName + "=" + Utilities.encodeUri(parameters.get(httpParameterName)); + query += httpParameterName + "=" + Utilities.encodeUriParam(parameters.get(httpParameterName)); } return new URI(basePath.getScheme(), basePath.getUserInfo(), basePath.getHost(), basePath.getPort(), diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/FHIRToolingClient.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/FHIRToolingClient.java index 986455991e..d6168e4a18 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/FHIRToolingClient.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/FHIRToolingClient.java @@ -345,7 +345,7 @@ public Parameters operateType(Class resourceClass, Strin if (!complex) for (ParametersParameterComponent p : params.getParameter()) if (p.getValue() instanceof PrimitiveType) - ps += p.getName() + "=" + Utilities.encodeUri(((PrimitiveType) p.getValue()).asStringValue()) + "&"; + ps += p.getName() + "=" + Utilities.encodeUriParam(((PrimitiveType) p.getValue()).asStringValue()) + "&"; ResourceRequest result; URI url = resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps); if (complex) { diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/ResourceAddress.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/ResourceAddress.java index 9209162107..dd769038cb 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/ResourceAddress.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/client/ResourceAddress.java @@ -426,7 +426,7 @@ public static URI appendHttpParameters(URI basePath, Map paramete } else { query = ""; } - query += httpParameterName + "=" + Utilities.encodeUri(parameters.get(httpParameterName)); + query += httpParameterName + "=" + Utilities.encodeUriParam(parameters.get(httpParameterName)); } return new URI(basePath.getScheme(), basePath.getUserInfo(), basePath.getHost(),basePath.getPort(), basePath.getPath(), query, basePath.getFragment()); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java index d3efecee46..06871327bc 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java @@ -16,6 +16,7 @@ import java.net.URI; import java.net.URLDecoder; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -926,11 +927,14 @@ public static String[] splitByCamelCase(String name) { return parts.toArray(new String[]{}); } - - public static String encodeUri(String v) { - return v.replace("%", "%25").replace(" ", "%20").replace("?", "%3F").replace("=", "%3D").replace("|", "%7C").replace("+", "%2B"); + @Deprecated + public static String encodeUri(String string) { + return URLEncoder.encode(string, StandardCharsets.UTF_8); } + public static String encodeUriParam(String param) { + return URLEncoder.encode(param, StandardCharsets.UTF_8); + } public static String normalize(String s) { if (noString(s)) From 9626760c8122b50afca4bf5fd236f74f22976a7d Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 14 Jan 2025 12:03:58 -0500 Subject: [PATCH 2/3] Use same method, do not duplicate --- .../src/main/java/org/hl7/fhir/utilities/Utilities.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java index 06871327bc..5b36e91c5c 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/Utilities.java @@ -929,7 +929,7 @@ public static String[] splitByCamelCase(String name) { @Deprecated public static String encodeUri(String string) { - return URLEncoder.encode(string, StandardCharsets.UTF_8); + return encodeUriParam(string); } public static String encodeUriParam(String param) { From 9bb7ac3d4dc9a434734de933a310cd3ed3f2b439 Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 14 Jan 2025 13:35:53 -0500 Subject: [PATCH 3/3] Cherrypick updated validation message --- org.hl7.fhir.utilities/src/main/resources/Messages.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/resources/Messages.properties b/org.hl7.fhir.utilities/src/main/resources/Messages.properties index abe7a46b7f..d9f84f6542 100644 --- a/org.hl7.fhir.utilities/src/main/resources/Messages.properties +++ b/org.hl7.fhir.utilities/src/main/resources/Messages.properties @@ -556,8 +556,8 @@ RESOURCETYPE_PROPERTY_WRONG_TYPE = The JSON element ''resourceType'' has the wro Reference_REF_Aggregation = Reference is {0} which isn''t supported by the specified aggregation mode(s) for the reference ({1}) Reference_REF_BadTargetType = Invalid Resource target type. Found {0}, but expected one of ({1}) Reference_REF_BadTargetType2 = The type ''{0}'' implied by the reference URL {1} is not a valid Target for this element (must be one of {2}) -Reference_REF_CantMatchChoice = Unable to find a match for profile {0} among choices: {1} -Reference_REF_CantMatchType = Unable to find a match for profile {0} (by type) among choices: {1} +Reference_REF_CantMatchChoice = Unable to find a profile match for {0} among choices: {1} +Reference_REF_CantMatchType = Unable to find a profile match for {0} (by type) among choices: {1} Reference_REF_CantResolve = Unable to resolve resource with reference ''{0}'' Reference_REF_CantResolveProfile = Unable to resolve the profile reference ''{0}'' Reference_REF_Format1 = Relative URLs must be of the format [ResourceName]/[id], or a search URL is allowed ([type]?parameters. Encountered {0})