Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New method MediaType.getCharsetParameter() returns charset instance or null #1305

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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
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.

1 participant