Deprecate HttpUtil.getCharsetAsString(...) and introduce HttpUtil.getCharsetAsSequence(...).

Motivation:

The method HttpUtil.getCharsetAsString(...) is missleading as its return type is CharSequence and not String.

Modifications:

Deprecate HttpUtil.getCharsetAsString(...) and introduce HttpUtil.getCharsetAsSe
quence(...).

Result:

Less confusing method name.
This commit is contained in:
Norman Maurer 2016-11-16 08:13:39 +01:00
parent 782b7bcf4a
commit 0b3122d8ff
2 changed files with 20 additions and 6 deletions

View File

@ -352,7 +352,7 @@ public final class HttpUtil {
* if charset is not presented or unparsable
*/
public static Charset getCharset(HttpMessage message, Charset defaultCharset) {
CharSequence charsetCharSequence = getCharsetAsString(message);
CharSequence charsetCharSequence = getCharsetAsSequence(message);
if (charsetCharSequence != null) {
try {
return Charset.forName(charsetCharSequence.toString());
@ -372,8 +372,23 @@ public final class HttpUtil {
*
* @return the {@code CharSequence} with charset from message's Content-Type header
* or {@code null} if charset is not presented
* @deprecated use {@link #getCharsetAsSequence(HttpMessage)}
*/
@Deprecated
public static CharSequence getCharsetAsString(HttpMessage message) {
return getCharsetAsSequence(message);
}
/**
* Fetch charset from message's Content-Type header as a char sequence.
*
* A lot of sites/possibly clients have charset="CHARSET", for example charset="utf-8". Or "utf8" instead of "utf-8"
* This is not according to standard, but this method provide an ability to catch desired mistakes manually in code
*
* @return the {@code CharSequence} with charset from message's Content-Type header
* or {@code null} if charset is not presented
*/
public static CharSequence getCharsetAsSequence(HttpMessage message) {
CharSequence contentTypeValue = message.headers().get(HttpHeaderNames.CONTENT_TYPE);
if (contentTypeValue != null) {
int indexOfCharset = AsciiString.indexOfIgnoreCaseAscii(contentTypeValue, CHARSET_EQUALS, 0);

View File

@ -18,7 +18,6 @@ package io.netty.handler.codec.http;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import java.nio.charset.StandardCharsets;
import java.util.List;
import static io.netty.handler.codec.http.HttpHeadersTestUtils.of;
@ -57,10 +56,10 @@ public class HttpUtilTest {
public void testGetCharsetAsRawString() {
HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
message.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=\"utf8\"");
assertEquals("\"utf8\"", HttpUtil.getCharsetAsString(message));
assertEquals("\"utf8\"", HttpUtil.getCharsetAsSequence(message));
message.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
assertNull(HttpUtil.getCharsetAsString(message));
assertNull(HttpUtil.getCharsetAsSequence(message));
}
@Test
@ -80,13 +79,13 @@ public class HttpUtilTest {
assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(message));
message.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message, StandardCharsets.UTF_8));
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message, CharsetUtil.UTF_8));
message.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTFFF");
assertEquals(CharsetUtil.ISO_8859_1, HttpUtil.getCharset(message));
message.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTFFF");
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message, StandardCharsets.UTF_8));
assertEquals(CharsetUtil.UTF_8, HttpUtil.getCharset(message, CharsetUtil.UTF_8));
}
@Test