From 4dc126365f0c0bfea533be4c984a2ad6263c818e Mon Sep 17 00:00:00 2001 From: Stig Norland Date: Tue, 29 Oct 2024 09:04:15 +0100 Subject: [PATCH] merging part 1 --- .../common/records/HttpResponseFormatter.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/search-commons/src/main/java/no/unit/nva/search/common/records/HttpResponseFormatter.java b/search-commons/src/main/java/no/unit/nva/search/common/records/HttpResponseFormatter.java index 62909868c..f66d37ffa 100644 --- a/search-commons/src/main/java/no/unit/nva/search/common/records/HttpResponseFormatter.java +++ b/search-commons/src/main/java/no/unit/nva/search/common/records/HttpResponseFormatter.java @@ -3,8 +3,11 @@ import static com.google.common.net.MediaType.CSV_UTF_8; import static no.unit.nva.constants.Words.COMMA; +import static no.unit.nva.search.common.constant.Functions.decodeUTF; import static no.unit.nva.search.common.constant.Functions.hasContent; +import static no.unit.nva.search.common.enums.ParameterKind.FUZZY_KEYWORD; +import static nva.commons.core.attempt.Try.attempt; import static nva.commons.core.paths.UriWrapper.fromUri; import static java.util.Objects.nonNull; @@ -16,6 +19,9 @@ import no.unit.nva.search.common.QueryKeys; import no.unit.nva.search.common.csv.ResourceCsvTransformer; import no.unit.nva.search.common.enums.ParameterKey; +import no.unit.nva.search.common.enums.ValueEncoding; + +import nva.commons.core.paths.UriWrapper; import java.net.URI; import java.util.List; @@ -55,9 +61,25 @@ public HttpResponseFormatter( this.offset = offset; this.size = size; this.facetPaths = facetPaths; + trimUriToIdentifierWhereApplicable(requestParameter); this.queryKeys = requestParameter; } + private void trimUriToIdentifierWhereApplicable(QueryKeys requestParameter) { + if (nonNull(requestParameter)) { + requestParameter + .getSearchKeys() + .filter(this::isFuzzyKeyword) + .forEach( + key -> { + var value = decodedValue(requestParameter, key); + if (isUri(value)) { + requestParameter.set(key, uriToIdentifier(value)); + } + }); + } + } + public HttpResponseFormatter(SwsResponse response, MediaType mediaType) { this(response, mediaType, null, 0, 0, Map.of(), null); } @@ -125,6 +147,25 @@ private URI nextResultsBySortKey(Map requestParameter, URI gatew return fromUri(gatewayUri).addQueryParameters(requestParameter).getUri(); } + private boolean isFuzzyKeyword(K key) { + return FUZZY_KEYWORD.equals(key.fieldType()); + } + + private boolean isUri(String decodedValue) { + return attempt(() -> URI.create(decodedValue).toURL()).isSuccess(); + } + + private String uriToIdentifier(String decodedValue) { + return UriWrapper.fromUri(decodedValue).getLastPathElement(); + } + + private & ParameterKey> String decodedValue( + QueryKeys parameters, K key) { + return key.valueEncoding() == ValueEncoding.NONE + ? parameters.get(key).toString() + : decodeUTF(parameters.get(key).toString()); + } + @Override public String toString() { return CSV_UTF_8.is(this.mediaType) ? toCsvText() : toPagedResponse().toJsonString();