Skip to content
Merged
Show file tree
Hide file tree
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
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
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
Expand All @@ -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;

/**
Expand All @@ -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.
*/
Expand Down
Loading