Skip to content

Commit fa95e4d

Browse files
committed
New method MediaType.getCharsetParameter() returns charset instance or null
1 parent 392cb2c commit fa95e4d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

jaxrs-api/src/main/java/jakarta/ws/rs/core/MediaType.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package jakarta.ws.rs.core;
1818

19+
import java.nio.charset.Charset;
20+
import java.nio.charset.UnsupportedCharsetException;
1921
import java.util.Collections;
2022
import java.util.Map;
2123
import java.util.Objects;
@@ -306,6 +308,19 @@ public Map<String, String> getParameters() {
306308
return parameters;
307309
}
308310

311+
/**
312+
* Getter for the media type {@code charset} parameter value.
313+
*
314+
* @return charset built from the {@value #CHARSET_PARAMETER} parameter value. {@code null} if the parameter
315+
* is {@code null}, empty or blank.
316+
* @throws UnsupportedCharsetException if the charset from the {@value #CHARSET_PARAMETER} parameter value
317+
* is not supported.
318+
*/
319+
public Charset getCharsetParameter() throws UnsupportedCharsetException {
320+
final var charset = parameters.get(CHARSET_PARAMETER);
321+
return charset == null || charset.isEmpty() ? null : Charset.forName(charset);
322+
}
323+
309324
/**
310325
* Create a new {@code MediaType} instance with the same type, subtype and parameters copied from the original instance
311326
* and the supplied {@value #CHARSET_PARAMETER} parameter.

jaxrs-api/src/test/java/jakarta/ws/rs/core/MediaTypeTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -19,12 +19,16 @@
1919
import static org.hamcrest.CoreMatchers.not;
2020
import static org.hamcrest.MatcherAssert.assertThat;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.fail;
2223

2324
import org.hamcrest.Description;
2425
import org.hamcrest.DiagnosingMatcher;
2526
import org.hamcrest.Matcher;
2627
import org.junit.jupiter.api.Test;
2728

29+
import java.nio.charset.Charset;
30+
import java.nio.charset.StandardCharsets;
31+
import java.nio.charset.UnsupportedCharsetException;
2832
import java.util.Map;
2933

3034
/**
@@ -34,6 +38,22 @@
3438
*/
3539
public class MediaTypeTest {
3640

41+
/**
42+
* Test {@link MediaType#getCharsetParameter()} method.
43+
*/
44+
@Test
45+
public void testGetCharsetParameter() {
46+
assertEquals(StandardCharsets.UTF_8,
47+
MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8")
48+
.getCharsetParameter(),
49+
"Unexpected produced media type charset parameter.");
50+
try {
51+
MediaType.APPLICATION_XML_TYPE.withCharset("UTF-8").withCharset("unsupported-charset")
52+
.getCharsetParameter();
53+
fail("Unexpected produced media type charset parameter.");
54+
} catch (final UnsupportedCharsetException expectedException) {}
55+
}
56+
3757
/**
3858
* Test {@link MediaType#withCharset(String)} method.
3959
*/

0 commit comments

Comments
 (0)