From c8e342446f5a01a84ccc395103f925ad04455d0d Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Wed, 24 Apr 2024 17:24:04 +0200 Subject: [PATCH 1/2] fix: fix search alternative --- .../web/controller/SearchQueryDecoder.java | 32 +++++++++++++++++++ .../web/controller/UserController.java | 2 +- .../controller/SearchQueryDecoderTest.java | 24 ++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoder.java create mode 100644 src/test/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoderTest.java diff --git a/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoder.java b/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoder.java new file mode 100644 index 000000000..aaca267a4 --- /dev/null +++ b/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoder.java @@ -0,0 +1,32 @@ +package de.caritas.cob.userservice.api.adapters.web.controller; + +import static com.google.common.collect.Lists.newArrayList; + +import com.github.jknack.handlebars.internal.lang3.StringUtils; +import com.google.common.base.Splitter; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class SearchQueryDecoder { + + private static final String SEPARATOR = "+"; + + /* Allows the query string to contain plus sign, but not change it to space. + See https://bugs.openjdk.org/browse/JDK-8179507 */ + public static String decode(String query) { + if (StringUtils.isBlank(query)) { + return StringUtils.EMPTY; + } + var parts = Splitter.on(SEPARATOR).split(query); + return newArrayList(parts).stream() + .map(SearchQueryDecoder::decodePart) + .collect(Collectors.joining(SEPARATOR)).trim(); + } + + private static String decodePart(String s) { + return URLDecoder.decode(s, StandardCharsets.UTF_8); + } +} diff --git a/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java b/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java index 0d1e7f50b..c751de3c1 100644 --- a/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java +++ b/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java @@ -777,7 +777,7 @@ public ResponseEntity> getConsultants(@RequestParam @Override public ResponseEntity searchConsultants( String query, Integer page, Integer perPage, String field, String order) { - var decodedInfix = URLDecoder.decode(query, StandardCharsets.UTF_8).trim(); + var decodedInfix = SearchQueryDecoder.decode(query); var isAscending = order.equalsIgnoreCase("asc"); var mappedField = consultantDtoMapper.mappedFieldOf(field); var resultMap = diff --git a/src/test/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoderTest.java b/src/test/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoderTest.java new file mode 100644 index 000000000..87a89d081 --- /dev/null +++ b/src/test/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoderTest.java @@ -0,0 +1,24 @@ +package de.caritas.cob.userservice.api.adapters.web.controller; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class SearchQueryDecoderTest { + + @ParameterizedTest + @CsvSource({ + "firstname.lastname+consultant@virtual-identity.com, firstname.lastname+consultant@virtual-identity.com", + "*, *", + "firstname.lastname@virtual-identity.com, firstname.lastname@virtual-identity.com", + "A Test, A Test", + ",''", + "\"\",\"\"", + "stringWithoutPlusSign,stringWithoutPlusSign", + "firstname.lastname%2Bconsultant%40virtual-identity.com, firstname.lastname+consultant@virtual-identity.com" + }) + void decode_Should_Decode_String_Not_ChangingPlusIntoSpace(String input, String expectedOutput) { + assertThat(SearchQueryDecoder.decode(input).trim()).isEqualTo(expectedOutput); + } +} From 4a8f3269e9ffd0d26167d21fc8ec56a5116d1052 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 25 Apr 2024 15:00:03 +0200 Subject: [PATCH 2/2] fix: fix search alternative --- .../api/adapters/web/controller/SearchQueryDecoder.java | 7 ++++++- .../api/adapters/web/controller/UserController.java | 2 -- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoder.java b/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoder.java index aaca267a4..85622a6a7 100644 --- a/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoder.java +++ b/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/SearchQueryDecoder.java @@ -12,6 +12,10 @@ @Slf4j public class SearchQueryDecoder { + private SearchQueryDecoder() { + throw new IllegalStateException("Utility class"); + } + private static final String SEPARATOR = "+"; /* Allows the query string to contain plus sign, but not change it to space. @@ -23,7 +27,8 @@ public static String decode(String query) { var parts = Splitter.on(SEPARATOR).split(query); return newArrayList(parts).stream() .map(SearchQueryDecoder::decodePart) - .collect(Collectors.joining(SEPARATOR)).trim(); + .collect(Collectors.joining(SEPARATOR)) + .trim(); } private static String decodePart(String s) { diff --git a/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java b/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java index c751de3c1..0502c95e3 100644 --- a/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java +++ b/src/main/java/de/caritas/cob/userservice/api/adapters/web/controller/UserController.java @@ -100,8 +100,6 @@ import de.caritas.cob.userservice.api.tenant.TenantContext; import de.caritas.cob.userservice.generated.api.adapters.web.controller.UsersApi; import io.swagger.annotations.Api; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.List; import java.util.Map;