From eaf4f278dc71b5e22780fad49344b814d35becea Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Tue, 19 Dec 2023 16:52:28 -0500 Subject: [PATCH 1/2] fix(NotificationUtils): Encode user email to retrieve push devices. --- .../middleware/utils/NotificationUtils.java | 8 +++++++- .../middleware/utils/NotificationUtilsTest.java | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java index d914e7656..090808f91 100644 --- a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java +++ b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java @@ -20,8 +20,10 @@ import java.io.IOException; import java.net.URI; +import java.net.URLEncoder; import java.util.Map; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.opentripplanner.middleware.utils.ConfigUtils.getConfigPropertyAsText; /** @@ -327,7 +329,7 @@ public static int getPushInfo(String toUser) { try { Map headers = Map.of("Accept", "application/json"); var httpResponse = HttpUtils.httpRequestRawResponse( - URI.create(PUSH_API_URL + "/devices/get?api_key=" + PUSH_API_KEY + "&user=" + toUser), + URI.create(getPushDevicesUrl(PUSH_API_URL + "/devices/get?api_key=" + PUSH_API_KEY + "&user=", toUser)), 1000, HttpMethod.GET, headers, @@ -346,6 +348,10 @@ public static int getPushInfo(String toUser) { return 0; } + static String getPushDevicesUrl(String baseUrl, String toUser) { + return baseUrl + URLEncoder.encode(toUser, UTF_8); + } + /** * Poll the push middleware for the number of devices registered to receive push notifications * for the specified user, and update the corresponding field in memory and Mongo. diff --git a/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java b/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java index 495f1e1bf..983fb438c 100644 --- a/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java +++ b/src/test/java/org/opentripplanner/middleware/utils/NotificationUtilsTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; +import static org.opentripplanner.middleware.utils.NotificationUtils.getPushDevicesUrl; import static org.opentripplanner.middleware.utils.NotificationUtils.getTwilioLocale; /** @@ -30,4 +31,14 @@ void canGetTwilioLocale() { void twilioLocaleDefaultsToEnglish(String locale) { Assertions.assertEquals("en", getTwilioLocale(locale)); } + + @Test + void testGetPushDevicesUrl() { + String email = "first.last+suffix@example.com"; + String base = "https://get.example.com/devices/?user="; + Assertions.assertEquals( + "https://get.example.com/devices/?user=first.last%2Bsuffix%40example.com", + getPushDevicesUrl(base, email) + ); + } } From 0e8ba243917c8df7c997c4825efa222259f95153 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Wed, 20 Dec 2023 09:53:40 -0500 Subject: [PATCH 2/2] refactor(NotificationUtils): Use String.format for push devices URL. --- .../opentripplanner/middleware/utils/NotificationUtils.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java index 090808f91..4e9e8cbcb 100644 --- a/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java +++ b/src/main/java/org/opentripplanner/middleware/utils/NotificationUtils.java @@ -329,7 +329,11 @@ public static int getPushInfo(String toUser) { try { Map headers = Map.of("Accept", "application/json"); var httpResponse = HttpUtils.httpRequestRawResponse( - URI.create(getPushDevicesUrl(PUSH_API_URL + "/devices/get?api_key=" + PUSH_API_KEY + "&user=", toUser)), + URI.create(getPushDevicesUrl(String.format( + "%s/devices/get?api_key=%s&user=", + PUSH_API_URL, + PUSH_API_KEY + ), toUser)), 1000, HttpMethod.GET, headers,