Skip to content

Commit df6abd1

Browse files
authored
Remove HTTPClient dependency in hapi-fhir-base (hapifhir#6707)
* Remove HTTPClient dependency in hapi-fhir-base * Spotless * Compile fix
1 parent 57ee024 commit df6abd1

File tree

8 files changed

+97
-65
lines changed

8 files changed

+97
-65
lines changed

hapi-fhir-base/pom.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@
7070
<groupId>org.apache.commons</groupId>
7171
<artifactId>commons-lang3</artifactId>
7272
</dependency>
73-
<dependency>
74-
<groupId>org.apache.httpcomponents</groupId>
75-
<artifactId>httpclient</artifactId>
76-
</dependency>
73+
7774
<!-- JENA Dependencies - Used for Turtle encoding -->
7875
<dependency>
7976
<groupId>org.apache.jena</groupId>

hapi-fhir-base/src/main/java/ca/uhn/fhir/util/UrlUtil.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
import jakarta.annotation.Nonnull;
3232
import jakarta.annotation.Nullable;
3333
import org.apache.commons.lang3.StringUtils;
34-
import org.apache.http.NameValuePair;
35-
import org.apache.http.client.utils.URLEncodedUtils;
36-
import org.apache.http.message.BasicNameValuePair;
3734
import org.hl7.fhir.instance.model.api.IPrimitiveType;
3835

3936
import java.io.UnsupportedEncodingException;
@@ -572,39 +569,6 @@ public static String unescape(String theString) {
572569
return theString;
573570
}
574571

575-
public static List<NameValuePair> translateMatchUrl(String theMatchUrl) {
576-
List<NameValuePair> parameters;
577-
String matchUrl = theMatchUrl;
578-
int questionMarkIndex = matchUrl.indexOf('?');
579-
if (questionMarkIndex != -1) {
580-
matchUrl = matchUrl.substring(questionMarkIndex + 1);
581-
}
582-
583-
final String[] searchList = new String[] {"|", "=>=", "=<=", "=>", "=<"};
584-
final String[] replacementList = new String[] {"%7C", "=%3E%3D", "=%3C%3D", "=%3E", "=%3C"};
585-
matchUrl = StringUtils.replaceEach(matchUrl, searchList, replacementList);
586-
if (matchUrl.contains(" ")) {
587-
throw new InvalidRequestException(Msg.code(1744) + "Failed to parse match URL[" + theMatchUrl
588-
+ "] - URL is invalid (must not contain spaces)");
589-
}
590-
591-
parameters = URLEncodedUtils.parse((matchUrl), Constants.CHARSET_UTF8, '&');
592-
593-
// One issue that has happened before is people putting a "+" sign into an email address in a match URL
594-
// and having that turn into a " ". Since spaces are never appropriate for email addresses, let's just
595-
// assume they really meant "+".
596-
for (int i = 0; i < parameters.size(); i++) {
597-
NameValuePair next = parameters.get(i);
598-
if (next.getName().equals("email") && next.getValue().contains(" ")) {
599-
BasicNameValuePair newPair =
600-
new BasicNameValuePair(next.getName(), next.getValue().replace(' ', '+'));
601-
parameters.set(i, newPair);
602-
}
603-
}
604-
605-
return parameters;
606-
}
607-
608572
/**
609573
* Creates list of sub URIs candidates for search with :above modifier
610574
* Example input: http://[host]/[pathPart1]/[pathPart2]

hapi-fhir-jpaserver-searchparam/src/main/java/ca/uhn/fhir/jpa/searchparam/MatchUrlService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import ca.uhn.fhir.rest.param.ParameterUtil;
3838
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
3939
import ca.uhn.fhir.rest.server.util.ISearchParamRegistry;
40+
import ca.uhn.fhir.rest.server.util.MatchUrlUtil;
4041
import ca.uhn.fhir.util.ReflectionUtil;
4142
import ca.uhn.fhir.util.UrlUtil;
4243
import com.google.common.collect.ArrayListMultimap;
@@ -59,7 +60,7 @@ public class MatchUrlService {
5960
public SearchParameterMap translateMatchUrl(
6061
String theMatchUrl, RuntimeResourceDefinition theResourceDefinition, Flag... theFlags) {
6162
SearchParameterMap paramMap = new SearchParameterMap();
62-
List<NameValuePair> parameters = UrlUtil.translateMatchUrl(theMatchUrl);
63+
List<NameValuePair> parameters = MatchUrlUtil.translateMatchUrl(theMatchUrl);
6364

6465
ArrayListMultimap<String, QualifiedParamList> nameToParamLists = ArrayListMultimap.create();
6566
for (NameValuePair next : parameters) {

hapi-fhir-server/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
<artifactId>owasp-java-html-sanitizer</artifactId>
5050
</dependency>
5151

52+
<dependency>
53+
<groupId>org.apache.httpcomponents</groupId>
54+
<artifactId>httpclient</artifactId>
55+
</dependency>
56+
5257
<!--
5358
Spring is added as an optional dependency just so that it
5459
can be used for CORS
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ca.uhn.fhir.rest.server.util;
2+
3+
import ca.uhn.fhir.i18n.Msg;
4+
import ca.uhn.fhir.rest.api.Constants;
5+
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
6+
import org.apache.commons.lang3.StringUtils;
7+
import org.apache.http.NameValuePair;
8+
import org.apache.http.client.utils.URLEncodedUtils;
9+
import org.apache.http.message.BasicNameValuePair;
10+
11+
import java.util.List;
12+
13+
public class MatchUrlUtil {
14+
15+
/**
16+
* Non-instantiable
17+
*/
18+
private MatchUrlUtil() {
19+
// nothing
20+
}
21+
22+
/**
23+
* Parses a FHIR-style Match URL (Patient?identifier=http://foo|bar) into
24+
* a parsed set of parameters.
25+
*/
26+
public static List<NameValuePair> translateMatchUrl(String theMatchUrl) {
27+
List<NameValuePair> parameters;
28+
String matchUrl = theMatchUrl;
29+
int questionMarkIndex = matchUrl.indexOf('?');
30+
if (questionMarkIndex != -1) {
31+
matchUrl = matchUrl.substring(questionMarkIndex + 1);
32+
}
33+
34+
final String[] searchList = new String[] {"|", "=>=", "=<=", "=>", "=<"};
35+
final String[] replacementList = new String[] {"%7C", "=%3E%3D", "=%3C%3D", "=%3E", "=%3C"};
36+
matchUrl = StringUtils.replaceEach(matchUrl, searchList, replacementList);
37+
if (matchUrl.contains(" ")) {
38+
throw new InvalidRequestException(Msg.code(1744) + "Failed to parse match URL[" + theMatchUrl
39+
+ "] - URL is invalid (must not contain spaces)");
40+
}
41+
42+
parameters = URLEncodedUtils.parse((matchUrl), Constants.CHARSET_UTF8, '&');
43+
44+
// One issue that has happened before is people putting a "+" sign into an email address in a match URL
45+
// and having that turn into a " ". Since spaces are never appropriate for email addresses, let's just
46+
// assume they really meant "+".
47+
for (int i = 0; i < parameters.size(); i++) {
48+
NameValuePair next = parameters.get(i);
49+
if (next.getName().equals("email") && next.getValue().contains(" ")) {
50+
BasicNameValuePair newPair =
51+
new BasicNameValuePair(next.getName(), next.getValue().replace(' ', '+'));
52+
parameters.set(i, newPair);
53+
}
54+
}
55+
56+
return parameters;
57+
}
58+
}

hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/util/ServletRequestUtil.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import ca.uhn.fhir.rest.api.RequestTypeEnum;
2323
import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
2424
import ca.uhn.fhir.rest.server.servlet.ServletSubRequestDetails;
25-
import ca.uhn.fhir.util.UrlUtil;
2625
import com.google.common.collect.ArrayListMultimap;
2726
import org.apache.http.NameValuePair;
2827

@@ -43,7 +42,7 @@ public static ServletSubRequestDetails getServletSubRequestDetails(
4342
requestDetails.setParameters(new HashMap<>());
4443
if (qIndex != -1) {
4544
String params = url.substring(qIndex);
46-
List<NameValuePair> parameters = UrlUtil.translateMatchUrl(params);
45+
List<NameValuePair> parameters = MatchUrlUtil.translateMatchUrl(params);
4746
for (NameValuePair next : parameters) {
4847
theParamValues.put(next.getName(), next.getValue());
4948
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ca.uhn.fhir.rest.server.util;
2+
3+
import org.apache.http.message.BasicNameValuePair;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
class MatchUrlUtilTest {
9+
10+
@Test
11+
public void testTranslateMatchUrl_UrlWithSpaces() {
12+
// %20 is an encoded space character
13+
assertThat(MatchUrlUtil.translateMatchUrl("Observation?names=homer%20simpson")).containsExactlyInAnyOrder(new BasicNameValuePair("names", "homer simpson"));
14+
15+
// + is also an encoded space character
16+
assertThat(MatchUrlUtil.translateMatchUrl("Observation?names=homer+simpson")).containsExactlyInAnyOrder(new BasicNameValuePair("names", "homer simpson"));
17+
}
18+
19+
@Test
20+
public void testTranslateMatchUrl_UrlWithPlusSign() {
21+
// %2B is an encoded plus sign
22+
assertThat(MatchUrlUtil.translateMatchUrl("Observation?names=homer%2Bsimpson")).containsExactlyInAnyOrder(new BasicNameValuePair("names", "homer+simpson"));
23+
}
24+
25+
@Test
26+
public void testTranslateMatchUrl_UrlWithPipe() {
27+
// Real space
28+
assertThat(MatchUrlUtil.translateMatchUrl("Observation?names=homer|simpson")).containsExactlyInAnyOrder(new BasicNameValuePair("names", "homer|simpson"));
29+
}
30+
}

hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/UrlUtilTest.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import ca.uhn.fhir.i18n.Msg;
55
import ca.uhn.fhir.rest.api.Constants;
66
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
7-
import org.apache.http.message.BasicNameValuePair;
87
import org.junit.jupiter.api.Test;
98
import org.junit.jupiter.params.ParameterizedTest;
109
import org.junit.jupiter.params.provider.CsvSource;
@@ -115,27 +114,6 @@ public void testSanitize() {
115114
assertEquals(" ", UrlUtil.sanitizeUrlPart(" \0 "));
116115
}
117116

118-
@Test
119-
public void testTranslateMatchUrl_UrlWithSpaces() {
120-
// %20 is an encoded space character
121-
assertThat(UrlUtil.translateMatchUrl("Observation?names=homer%20simpson")).containsExactlyInAnyOrder(new BasicNameValuePair("names", "homer simpson"));
122-
123-
// + is also an encoded space character
124-
assertThat(UrlUtil.translateMatchUrl("Observation?names=homer+simpson")).containsExactlyInAnyOrder(new BasicNameValuePair("names", "homer simpson"));
125-
}
126-
127-
@Test
128-
public void testTranslateMatchUrl_UrlWithPlusSign() {
129-
// %2B is an encoded plus sign
130-
assertThat(UrlUtil.translateMatchUrl("Observation?names=homer%2Bsimpson")).containsExactlyInAnyOrder(new BasicNameValuePair("names", "homer+simpson"));
131-
}
132-
133-
@Test
134-
public void testTranslateMatchUrl_UrlWithPipe() {
135-
// Real space
136-
assertThat(UrlUtil.translateMatchUrl("Observation?names=homer|simpson")).containsExactlyInAnyOrder(new BasicNameValuePair("names", "homer|simpson"));
137-
}
138-
139117
@ParameterizedTest
140118
@CsvSource({
141119
"null, null",

0 commit comments

Comments
 (0)