From aa2a61b75b91e0d5c7aa26b6edbc1d88fb1d1c12 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Wed, 16 Sep 2020 12:16:54 +0800 Subject: [PATCH] Fix http client contentType charset problem --- .../alibaba/nacos/common/http/HttpUtils.java | 4 +- .../nacos/common/http/param/MediaType.java | 68 ++++++++++++++++++- .../common/http/param/MediaTypeTest.java | 62 +++++++++++++++++ 3 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 common/src/test/java/com/alibaba/nacos/common/http/param/MediaTypeTest.java diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java index 7f5238a6aad..54f99affc3e 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.MediaType; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.StringUtils; @@ -81,7 +82,8 @@ public static void initRequestEntity(HttpRequestBase requestBase, Object body, H } if (requestBase instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase; - ContentType contentType = ContentType.create(header.getValue(HttpHeaderConsts.CONTENT_TYPE), header.getCharset()); + MediaType mediaType = MediaType.valueOf(header.getValue(HttpHeaderConsts.CONTENT_TYPE)); + ContentType contentType = ContentType.create(mediaType.getType(), mediaType.getCharset()); HttpEntity entity; if (body instanceof byte[]) { entity = new ByteArrayEntity((byte[]) body, contentType); diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java b/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java index 4838f961384..774c48307f4 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java @@ -16,6 +16,9 @@ package com.alibaba.nacos.common.http.param; +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.common.utils.StringUtils; + /** * Http Media type. * @@ -23,9 +26,6 @@ */ public final class MediaType { - private MediaType() { - } - public static final String APPLICATION_ATOM_XML = "application/atom+xml"; public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded;charset=UTF-8"; @@ -46,4 +46,66 @@ private MediaType() { public static final String TEXT_PLAIN = "text/plain;charset=UTF-8"; + private MediaType(String type, String charset) { + this.type = type; + this.charset = charset; + } + + /** + * content type. + */ + private final String type; + + /** + * content type charset. + */ + private final String charset; + + /** + * Parse the given String contentType into a {@code MediaType} object. + * + * @param contentType mediaType + * @return MediaType + */ + public static MediaType valueOf(String contentType) { + if (StringUtils.isEmpty(contentType)) { + throw new IllegalArgumentException("MediaType must not be empty"); + } + String[] values = contentType.split(";"); + String charset = Constants.ENCODE; + for (String value : values) { + if (value.startsWith("charset=")) { + charset = value.substring("charset=".length()); + } + } + return new MediaType(values[0], charset); + } + + /** + * Use the given contentType and charset to assemble into a {@code MediaType} object. + * + * @param contentType contentType + * @param charset charset + * @return MediaType + */ + public static MediaType valueOf(String contentType, String charset) { + if (StringUtils.isEmpty(contentType)) { + throw new IllegalArgumentException("MediaType must not be empty"); + } + String[] values = contentType.split(";"); + return new MediaType(values[0], StringUtils.isEmpty(charset) ? Constants.ENCODE : charset); + } + + public String getType() { + return type; + } + + public String getCharset() { + return charset; + } + + @Override + public String toString() { + return type + ";charset=" + charset; + } } diff --git a/common/src/test/java/com/alibaba/nacos/common/http/param/MediaTypeTest.java b/common/src/test/java/com/alibaba/nacos/common/http/param/MediaTypeTest.java new file mode 100644 index 00000000000..eddbfecb31f --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/http/param/MediaTypeTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.param; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * MediaTypeTest. + * + * @author mai.jh + */ +public class MediaTypeTest { + + @Test + public void testValueOf() { + MediaType mediaType = MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED); + String type = "application/x-www-form-urlencoded"; + String charset = "UTF-8"; + assertEquals(type, mediaType.getType()); + assertEquals(charset, mediaType.getCharset()); + assertEquals(MediaType.APPLICATION_FORM_URLENCODED, mediaType.toString()); + } + + @Test + public void testValueOf2() { + MediaType mediaType = MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED, "ISO-8859-1"); + String type = "application/x-www-form-urlencoded"; + String charset = "ISO-8859-1"; + String excepted = "application/x-www-form-urlencoded;charset=ISO-8859-1"; + assertEquals(type, mediaType.getType()); + assertEquals(charset, mediaType.getCharset()); + assertEquals(excepted, mediaType.toString()); + } + + @Test + public void testValueOf3() { + MediaType mediaType = MediaType.valueOf("application/x-www-form-urlencoded", "ISO-8859-1"); + String type = "application/x-www-form-urlencoded"; + String charset = "ISO-8859-1"; + String excepted = "application/x-www-form-urlencoded;charset=ISO-8859-1"; + assertEquals(type, mediaType.getType()); + assertEquals(charset, mediaType.getCharset()); + assertEquals(excepted, mediaType.toString()); + } + +}