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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
New method MediaType.getCharsetParameter() returns charset instance o…
…r null
mkarg committed Jan 26, 2025
commit 94f6feddb41e7e9cc393c5d32108c0e2dc389245
15 changes: 15 additions & 0 deletions jaxrs-api/src/main/java/jakarta/ws/rs/core/MediaType.java
Original file line number Diff line number Diff line change
@@ -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;
@@ -306,6 +308,19 @@ public Map<String, String> getParameters() {
return parameters;
}

/**
* 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.
22 changes: 21 additions & 1 deletion jaxrs-api/src/test/java/jakarta/ws/rs/core/MediaTypeTest.java
Original file line number Diff line number Diff line change
@@ -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;

/**
@@ -34,6 +38,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.
*/