From 95272feb2827d4434636a1a30f5b5658e1ba15cc Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 11 Jun 2020 17:38:34 +0800 Subject: [PATCH] adjust some codes --- .../common/constant/HttpHeaderConsts.java | 3 ++ .../client/DefaultClientHttpResponse.java | 3 +- .../http/client/DefaultHttpClientRequest.java | 2 +- .../nacos/common/http/param/Header.java | 14 +++++--- .../nacos/common/http/param/MediaType.java | 4 +-- .../alibaba/nacos/common/utils/IoUtils.java | 11 ------- .../nacos/common/utils/JacksonUtils.java | 32 ++++++++----------- .../nacos/common/http/HttpUtilsTest.java | 20 ------------ 8 files changed, 30 insertions(+), 59 deletions(-) delete mode 100644 common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java diff --git a/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java b/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java index 18d5236ce11..baa43b84331 100644 --- a/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java +++ b/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java @@ -27,5 +27,8 @@ public interface HttpHeaderConsts { String REQUEST_SOURCE_HEADER = "Request-Source"; String CONTENT_TYPE = "Content-Type"; String CONTENT_LENGTH = "Content-Length"; + String ACCEPT_CHARSET = "Accept-Charset"; + String ACCEPT_ENCODING = "Accept-Encoding"; + String CONTENT_ENCODING = "Content-Encoding"; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java index 8f721e960f6..957c763f7e6 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java @@ -72,8 +72,7 @@ public void close() { if (this.response != null) { HttpClientUtils.closeQuietly(response); } - } - catch (Exception ex) { + } catch (Exception ex) { // ignore } } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java index 52bb63c00eb..e36c22d4ba7 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java @@ -41,7 +41,7 @@ @SuppressWarnings({"unchecked", "rawtypes", "resource"}) public class DefaultHttpClientRequest implements HttpClientRequest { - private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); + private static final Logger logger = LoggerFactory.getLogger(DefaultHttpClientRequest.class); private final CloseableHttpClient client; 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 786efeffbdf..239fe95d64c 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 @@ -39,9 +39,9 @@ public class Header { private Header() { header = new LinkedHashMap(); addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_JSON); - addParam("Accept-Charset", "UTF-8"); - addParam("Accept-Encoding", "gzip"); - addParam("Content-Encoding", "gzip"); + addParam(HttpHeaderConsts.ACCEPT_CHARSET, "UTF-8"); + addParam(HttpHeaderConsts.ACCEPT_ENCODING, "gzip"); + addParam(HttpHeaderConsts.CONTENT_ENCODING, "gzip"); } public static Header newInstance() { @@ -104,8 +104,12 @@ public void addAll(Map params) { } public String getCharset() { - String value = getValue(HttpHeaderConsts.CONTENT_TYPE); - return (StringUtils.isNotBlank(value) ? analysisCharset(value) : Constants.ENCODE); + String acceptCharset = getValue(HttpHeaderConsts.ACCEPT_CHARSET); + if (acceptCharset == null) { + String contentType = getValue(HttpHeaderConsts.CONTENT_TYPE); + acceptCharset = StringUtils.isNotBlank(contentType) ? analysisCharset(contentType) : Constants.ENCODE; + } + return acceptCharset; } private String analysisCharset(String 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 a5246f811b7..8f3c411f2c8 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 @@ -25,7 +25,7 @@ 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"; + public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded"; public static final String APPLICATION_OCTET_STREAM = "application/octet-stream"; @@ -35,7 +35,7 @@ private MediaType() {} public static final String APPLICATION_XML = "application/xml"; - public static final String APPLICATION_JSON = "application/json;charset=UTF-8"; + public static final String APPLICATION_JSON = "application/json"; public static final String MULTIPART_FORM_DATA = "multipart/form-data"; diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java index 8b73233f62c..105635fe4e7 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java @@ -167,17 +167,6 @@ public static void cleanDirectory(File directory) throws IOException { } } - public static void copy(byte[] in, OutputStream out) throws IOException { - try { - out.write(in); - } finally { - try { - out.close(); - } catch (IOException io) { - } - } - } - static public long copy(InputStream input, OutputStream output) throws IOException { byte[] buffer = new byte[1024]; int bytesRead; diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java index c384fa7e7c3..b2d6ba9ba46 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java @@ -47,8 +47,7 @@ public final class JacksonUtils { public static String toJson(Object obj) { try { return mapper.writeValueAsString(obj); - } - catch (JsonProcessingException e) { + } catch (JsonProcessingException e) { throw new NacosSerializationException(obj.getClass(), e); } } @@ -56,8 +55,7 @@ public static String toJson(Object obj) { public static byte[] toJsonBytes(Object obj) { try { return ByteUtils.toBytes(mapper.writeValueAsString(obj)); - } - catch (JsonProcessingException e) { + } catch (JsonProcessingException e) { throw new NacosSerializationException(obj.getClass(), e); } } @@ -65,8 +63,7 @@ public static byte[] toJsonBytes(Object obj) { public static T toObj(byte[] json, Class cls) { try { return toObj(StringUtils.newString4UTF8(json), cls); - } - catch (Exception e) { + } catch (Exception e) { throw new NacosDeserializationException(cls, e); } } @@ -79,8 +76,12 @@ public static T toObj(byte[] json, Type cls) { } } - public static T toObj(InputStream inputStream, Class tClass) throws Exception { - return mapper.readValue(inputStream, tClass); + public static T toObj(InputStream inputStream, Class tClass) { + try { + return mapper.readValue(inputStream, tClass); + } catch (IOException e) { + throw new NacosDeserializationException(e); + } } public static T toObj(byte[] json, TypeReference typeReference) { @@ -94,8 +95,7 @@ public static T toObj(byte[] json, TypeReference typeReference) { public static T toObj(String json, Class cls) { try { return mapper.readValue(json, cls); - } - catch (IOException e) { + } catch (IOException e) { throw new NacosDeserializationException(cls, e); } } @@ -103,8 +103,7 @@ public static T toObj(String json, Class cls) { public static T toObj(String json, Type type) { try { return mapper.readValue(json, mapper.constructType(type)); - } - catch (IOException e) { + } catch (IOException e) { throw new NacosDeserializationException(e); } } @@ -112,8 +111,7 @@ public static T toObj(String json, Type type) { public static T toObj(String json, TypeReference typeReference) { try { return mapper.readValue(json, typeReference); - } - catch (IOException e) { + } catch (IOException e) { throw new NacosDeserializationException(typeReference.getClass(), e); } } @@ -121,8 +119,7 @@ public static T toObj(String json, TypeReference typeReference) { public static T toObj(InputStream inputStream, Type type) { try { return mapper.readValue(inputStream, mapper.constructType(type)); - } - catch (IOException e) { + } catch (IOException e) { throw new NacosDeserializationException(type, e); } } @@ -130,8 +127,7 @@ public static T toObj(InputStream inputStream, Type type) { public static JsonNode toObj(String json) { try { return mapper.readTree(json); - } - catch (IOException e) { + } catch (IOException e) { throw new NacosDeserializationException(e); } } diff --git a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java deleted file mode 100644 index 59682b5c5bb..00000000000 --- a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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; - -public class HttpUtilsTest { - -}