From 6379ba25bfbb8eb9cf66ea3ef070bcc74a72c564 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 4 Feb 2021 19:13:56 +0800 Subject: [PATCH] [ISSUE-#4854] Modify Header to support Keys Ignore Case (#4870) * Modify header key Ignore Case, Replace LinkedHashMap with TreeMap(String.case_insensitive_order) * Delete redundant import * add junit test case --- .../response/JdkHttpClientResponse.java | 6 +++- .../nacos/common/http/param/Header.java | 19 ++++++------ .../nacos/common/http/param/HeaderTest.java | 31 +++++++++++++++++++ 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 common/src/test/java/com/alibaba/nacos/common/http/param/HeaderTest.java diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java index 90fef3445..ab50f5d01 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java @@ -24,6 +24,8 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; +import java.util.List; +import java.util.Map; /** * JDk http client response implement. @@ -49,7 +51,9 @@ public class JdkHttpClientResponse implements HttpClientResponse { if (this.responseHeader == null) { this.responseHeader = Header.newInstance(); } - this.responseHeader.setOriginalResponseHeader(conn.getHeaderFields()); + for (Map.Entry> entry : conn.getHeaderFields().entrySet()) { + this.responseHeader.addOriginalResponseHeader(entry.getKey(), entry.getValue()); + } return this.responseHeader; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java index fcaf31c72..9b3b683cb 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java @@ -23,9 +23,9 @@ import com.alibaba.nacos.common.utils.StringUtils; import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.TreeMap; /** * Http header. @@ -41,8 +41,8 @@ public class Header { private final Map> originalResponseHeader; private Header() { - header = new LinkedHashMap(); - originalResponseHeader = new LinkedHashMap>(); + header = new TreeMap(String.CASE_INSENSITIVE_ORDER); + originalResponseHeader = new TreeMap>(String.CASE_INSENSITIVE_ORDER); addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_JSON); addParam(HttpHeaderConsts.ACCEPT_CHARSET, "UTF-8"); addParam(HttpHeaderConsts.ACCEPT_ENCODING, "gzip"); @@ -142,14 +142,13 @@ public class Header { * *

Currently only corresponds to the response header of JDK. * - * @param headers original response header + * @param key original response header key + * @param values original response header values */ - public void setOriginalResponseHeader(Map> headers) { - if (MapUtils.isNotEmpty(headers)) { - this.originalResponseHeader.putAll(headers); - for (Map.Entry> entry : this.originalResponseHeader.entrySet()) { - addParam(entry.getKey(), entry.getValue().get(0)); - } + public void addOriginalResponseHeader(String key, List values) { + if (StringUtils.isNotEmpty(key)) { + this.originalResponseHeader.put(key, values); + addParam(key, values.get(0)); } } diff --git a/common/src/test/java/com/alibaba/nacos/common/http/param/HeaderTest.java b/common/src/test/java/com/alibaba/nacos/common/http/param/HeaderTest.java new file mode 100644 index 000000000..0b7847a14 --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/http/param/HeaderTest.java @@ -0,0 +1,31 @@ +/* + * 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; + +public class HeaderTest { + + @Test + public void testHeaderKyeIgnoreCase() { + Header header = Header.newInstance(); + header.addParam("Content-Encoding", "gzip"); + assertEquals("gzip", header.getValue("content-encoding")); + } +}