From ed456a8666a378c5a99791fb75e1ad940e3b7737 Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Tue, 29 Oct 2024 12:26:03 +0100 Subject: [PATCH] added CountryMessageConverter (cherry picked from commit cdce1bb2d4f6a1fe2ab16d147b240893b404b2c8) --- .../gbif/registry/ws/config/WebMvcConfig.java | 6 +++++ .../ws/converter/CountryMessageConverter.java | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 registry-ws/src/main/java/org/gbif/registry/ws/converter/CountryMessageConverter.java diff --git a/registry-ws/src/main/java/org/gbif/registry/ws/config/WebMvcConfig.java b/registry-ws/src/main/java/org/gbif/registry/ws/config/WebMvcConfig.java index 3b3b13c85e..8dbd3be3ae 100644 --- a/registry-ws/src/main/java/org/gbif/registry/ws/config/WebMvcConfig.java +++ b/registry-ws/src/main/java/org/gbif/registry/ws/config/WebMvcConfig.java @@ -23,6 +23,7 @@ import org.gbif.checklistbank.ws.client.NubResourceClient; import org.gbif.registry.domain.ws.*; import org.gbif.registry.security.precheck.AuthPreCheckInterceptor; +import org.gbif.registry.ws.converter.CountryMessageConverter; import org.gbif.registry.ws.converter.UuidTextMessageConverter; import org.gbif.registry.ws.provider.CollectionDescriptorsSearchRequestHandlerMethodArgumentResolver; import org.gbif.registry.ws.provider.CollectionSearchRequestHandlerMethodArgumentResolver; @@ -192,4 +193,9 @@ public NubResourceClient nubResourceClient(@Value("${api.root.url}") String apiR .withUrl(apiRootUrl) .build(NubResourceClient.class); } + + @Bean + public CountryMessageConverter countryMessageConverter() { + return new CountryMessageConverter(); + } } diff --git a/registry-ws/src/main/java/org/gbif/registry/ws/converter/CountryMessageConverter.java b/registry-ws/src/main/java/org/gbif/registry/ws/converter/CountryMessageConverter.java new file mode 100644 index 0000000000..681382472c --- /dev/null +++ b/registry-ws/src/main/java/org/gbif/registry/ws/converter/CountryMessageConverter.java @@ -0,0 +1,22 @@ +package org.gbif.registry.ws.converter; + +import org.gbif.api.util.VocabularyUtils; +import org.gbif.api.vocabulary.Country; +import org.springframework.core.convert.converter.Converter; + +public class CountryMessageConverter implements Converter { + + @Override + public Country convert(String source) { + return parseCountry(source); + } + + private Country parseCountry(String param) { + Country country = Country.fromIsoCode(param); + if (country == null) { + // if nothing found also try by enum name + country = VocabularyUtils.lookupEnum(param, Country.class); + } + return country; + } +}