diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/CharsetConverter.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/CharsetConverter.java new file mode 100644 index 0000000000000..07c9dee91493b --- /dev/null +++ b/core/runtime/src/main/java/io/quarkus/runtime/configuration/CharsetConverter.java @@ -0,0 +1,28 @@ +package io.quarkus.runtime.configuration; + +import static io.quarkus.runtime.configuration.ConverterSupport.DEFAULT_QUARKUS_CONVERTER_PRIORITY; + +import java.io.Serializable; +import java.nio.charset.Charset; + +import javax.annotation.Priority; + +import org.eclipse.microprofile.config.spi.Converter; + +/** + * A converter which converts a Charset string into an instance of {@link java.nio.charset.Charset}. + */ +@Priority(DEFAULT_QUARKUS_CONVERTER_PRIORITY) +public class CharsetConverter implements Converter, Serializable { + + private static final long serialVersionUID = 2320905063828247874L; + + @Override + public Charset convert(String value) { + try { + return Charset.forName(value); + } catch (Exception e) { + throw new IllegalArgumentException("Unable to create Charset from " + value + "'", e); + } + } +} diff --git a/core/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter b/core/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter index 1e06ce609f6d6..9c6a04c4ca0b8 100644 --- a/core/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter +++ b/core/runtime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter @@ -1,4 +1,5 @@ io.quarkus.runtime.configuration.InetSocketAddressConverter +io.quarkus.runtime.configuration.CharsetConverter io.quarkus.runtime.configuration.CidrAddressConverter io.quarkus.runtime.configuration.InetAddressConverter io.quarkus.runtime.configuration.RegexConverter diff --git a/core/runtime/src/test/java/io/quarkus/runtime/configuration/CharsetConverterTestCase.java b/core/runtime/src/test/java/io/quarkus/runtime/configuration/CharsetConverterTestCase.java new file mode 100644 index 0000000000000..b93e0a4c9219c --- /dev/null +++ b/core/runtime/src/test/java/io/quarkus/runtime/configuration/CharsetConverterTestCase.java @@ -0,0 +1,39 @@ +package io.quarkus.runtime.configuration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.nio.charset.StandardCharsets; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class CharsetConverterTestCase { + + private CharsetConverter charsetConverter; + + @BeforeEach + public void setup() { + charsetConverter = new CharsetConverter(); + } + + @Test + public void testUTF8Uppercase() { + assertEquals(charsetConverter.convert("UTF-8"), StandardCharsets.UTF_8); + } + + @Test + public void testUTF8Lowercase() { + assertEquals(charsetConverter.convert("utf-8"), StandardCharsets.UTF_8); + } + + @Test + public void testOther() { + assertEquals(charsetConverter.convert("US-ASCII"), StandardCharsets.US_ASCII); + } + + @Test + public void testInvalidCharset() { + assertThrows(IllegalArgumentException.class, () -> charsetConverter.convert("whatever")); + } +}