Skip to content

Commit

Permalink
adjust some codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Maijh97 committed Jun 11, 2020
1 parent fa6bf99 commit 95272fe
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public void close() {
if (this.response != null) {
HttpClientUtils.closeQuietly(response);
}
}
catch (Exception ex) {
} catch (Exception ex) {
// ignore
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public class Header {
private Header() {
header = new LinkedHashMap<String, String>();
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() {
Expand Down Expand Up @@ -104,8 +104,12 @@ public void addAll(Map<String, String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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";

Expand Down
11 changes: 0 additions & 11 deletions common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,23 @@ 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);
}
}

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);
}
}

public static <T> T toObj(byte[] json, Class<T> cls) {
try {
return toObj(StringUtils.newString4UTF8(json), cls);
}
catch (Exception e) {
} catch (Exception e) {
throw new NacosDeserializationException(cls, e);
}
}
Expand All @@ -79,8 +76,12 @@ public static <T> T toObj(byte[] json, Type cls) {
}
}

public static <T> T toObj(InputStream inputStream, Class<T> tClass) throws Exception {
return mapper.readValue(inputStream, tClass);
public static <T> T toObj(InputStream inputStream, Class<T> tClass) {
try {
return mapper.readValue(inputStream, tClass);
} catch (IOException e) {
throw new NacosDeserializationException(e);
}
}

public static <T> T toObj(byte[] json, TypeReference<T> typeReference) {
Expand All @@ -94,44 +95,39 @@ public static <T> T toObj(byte[] json, TypeReference<T> typeReference) {
public static <T> T toObj(String json, Class<T> cls) {
try {
return mapper.readValue(json, cls);
}
catch (IOException e) {
} catch (IOException e) {
throw new NacosDeserializationException(cls, e);
}
}

public static <T> T toObj(String json, Type type) {
try {
return mapper.readValue(json, mapper.constructType(type));
}
catch (IOException e) {
} catch (IOException e) {
throw new NacosDeserializationException(e);
}
}

public static <T> T toObj(String json, TypeReference<T> typeReference) {
try {
return mapper.readValue(json, typeReference);
}
catch (IOException e) {
} catch (IOException e) {
throw new NacosDeserializationException(typeReference.getClass(), e);
}
}

public static <T> 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);
}
}

public static JsonNode toObj(String json) {
try {
return mapper.readTree(json);
}
catch (IOException e) {
} catch (IOException e) {
throw new NacosDeserializationException(e);
}
}
Expand Down

This file was deleted.

0 comments on commit 95272fe

Please sign in to comment.