New method MediaType.getCharsetParameter() returns charset instance or null
diff --git a/jaxrs-api/src/main/java/jakarta/ws/rs/core/MediaType.java b/jaxrs-api/src/main/java/jakarta/ws/rs/core/MediaType.java
index 42d5069..3708221 100644
--- a/jaxrs-api/src/main/java/jakarta/ws/rs/core/MediaType.java
+++ b/jaxrs-api/src/main/java/jakarta/ws/rs/core/MediaType.java
@@ -16,6 +16,8 @@
package jakarta.ws.rs.core;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
@@ -307,6 +309,19 @@
}
/**
+ * Getter for the media type {@code charset} parameter value.
+ *
+ * @return charset built from the {@value #CHARSET_PARAMETER} parameter value. {@code null} if the parameter
+ * is {@code null}, empty or blank.
+ * @throws UnsupportedCharsetException if the charset from the {@value #CHARSET_PARAMETER} parameter value
+ * is not supported.
+ */
+ public Charset getCharsetParameter() throws UnsupportedCharsetException {
+ final var charset = parameters.get(CHARSET_PARAMETER);
+ return charset == null || charset.isEmpty() ? null : Charset.forName(charset);
+ }
+
+ /**
* Create a new {@code MediaType} instance with the same type, subtype and parameters copied from the original instance
* and the supplied {@value #CHARSET_PARAMETER} parameter.
*
diff --git a/jaxrs-api/src/test/java/jakarta/ws/rs/core/MediaTypeTest.java b/jaxrs-api/src/test/java/jakarta/ws/rs/core/MediaTypeTest.java
index b042cae..e9a8ced 100644
--- a/jaxrs-api/src/test/java/jakarta/ws/rs/core/MediaTypeTest.java
+++ b/jaxrs-api/src/test/java/jakarta/ws/rs/core/MediaTypeTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -19,12 +19,16 @@
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
import org.hamcrest.Description;
import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.charset.UnsupportedCharsetException;
import java.util.Map;
/**
@@ -35,6 +39,22 @@
public class MediaTypeTest {
/**
+ * Test {@link MediaType#getCharsetParameter()} method.
+ */
+ @Test
+ public void testGetCharsetParameter() {
+ assertEquals(StandardCharsets.UTF_8,
+ MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8")
+ .getCharsetParameter(),
+ "Unexpected produced media type charset parameter.");
+ try {
+ MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8").withCharset("unsupported-charset")
+ .getCharsetParameter();
+ fail("Unexpected produced media type charset parameter.");
+ } catch (final UnsupportedCharsetException expectedException) {}
+ }
+
+ /**
* Test {@link MediaType#withCharset(String)} method.
*/
@Test