Skip to content

Conversation

@mkarg
Copy link
Contributor

@mkarg mkarg commented Jan 26, 2025

Proposal

I like to propose the addition of the new method MediaType.getCharsetParameter() which either returns a Charset instance, null, or throws UnsupportedCharsetException.

Justification

Rather often I came across code like the following lines (quoted from Jersey) in applications, filters and compliant implementations:

https://github.com/eclipse-ee4j/jersey/blob/b128e9c9e649e96488a9c1d367e4cbae89a9d1d4/core-common/src/main/java/org/glassfish/jersey/message/internal/ReaderWriter.java#L127-L130

public static Charset getCharset(MediaType m) {
    String name = (m == null) ? null : m.getParameters().get(MediaType.CHARSET_PARAMETER);
    return (name == null) ? StandardCharsets.UTF_8 : Charset.forName(name);
}

I do not see any benefit in repeating the obvious again and again, so why not giving away this standard solution for free as part of JAX-RS itself? 😃

@mkarg mkarg self-assigned this Jan 26, 2025
@mkarg mkarg added api enhancement New feature or request labels Jan 26, 2025
@mkarg mkarg requested review from chkal and jamezp January 26, 2025 18:02
@mkarg mkarg force-pushed the mediatype-getcharsetparameter branch from 33d3220 to 94f6fed Compare January 26, 2025 18:03
@mkarg mkarg requested a review from jansupol March 28, 2025 11:37
@mkarg mkarg merged commit fa95e4d into jakartaee:main Mar 28, 2025
3 checks passed
@mkarg mkarg deleted the mediatype-getcharsetparameter branch March 28, 2025 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants