Skip to content

Commit

Permalink
refactor: unified implementation of http client api adjustment. (alib…
Browse files Browse the repository at this point in the history
  • Loading branch information
Maijh97 authored Aug 20, 2020
1 parent e7c269a commit 136b456
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.Callable;

/**
* Server Agent.
Expand Down Expand Up @@ -94,9 +93,9 @@ public HttpRestResult<String> httpGet(String path, Map<String, String> headers,
if (headers != null) {
newHeaders.addAll(headers);
}

Query query = Query.newInstance().initParams(paramValues);
HttpRestResult<String> result = NACOS_RESTTEMPLATE
.get(getUrl(currentServerAddr, path), httpConfig, newHeaders, paramValues, String.class);
.get(getUrl(currentServerAddr, path), httpConfig, newHeaders, query, String.class);
if (isFail(result)) {
LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}",
serverListMgr.getCurrentServerAddr(), result.getCode());
Expand Down Expand Up @@ -152,8 +151,7 @@ public HttpRestResult<String> httpPost(String path, Map<String, String> headers,
newHeaders.addAll(headers);
}
HttpRestResult<String> result = NACOS_RESTTEMPLATE
.postForm(getUrl(currentServerAddr, path), httpConfig, newHeaders,
new HashMap<String, String>(0), paramValues, String.class);
.postForm(getUrl(currentServerAddr, path), httpConfig, newHeaders, paramValues, String.class);

if (isFail(result)) {
LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", currentServerAddr,
Expand Down Expand Up @@ -207,8 +205,9 @@ public HttpRestResult<String> httpDelete(String path, Map<String, String> header
if (headers != null) {
newHeaders.addAll(headers);
}
Query query = Query.newInstance().initParams(paramValues);
HttpRestResult<String> result = NACOS_RESTTEMPLATE
.delete(getUrl(currentServerAddr, path), httpConfig, newHeaders, paramValues, String.class);
.delete(getUrl(currentServerAddr, path), httpConfig, newHeaders, query, String.class);
if (isFail(result)) {
LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}",
serverListMgr.getCurrentServerAddr(), result.getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ public String callServer(String api, Map<String, String> params, Map<String, Str

try {
HttpRestResult<String> restResult = nacosRestTemplate
.exchangeForm(url, header, params, body, method, String.class);
.exchangeForm(url, header, Query.newInstance().initParams(params), body, method, String.class);
end = System.currentTimeMillis();

MetricsMonitor.getNamingRequestMonitor(method, url, String.valueOf(restResult.getCode()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.alibaba.nacos.common.http.HttpRestResult;
import com.alibaba.nacos.common.http.client.NacosRestTemplate;
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.Query;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -138,7 +139,7 @@ public boolean login(String server) {
}
try {
HttpRestResult<String> restResult = nacosRestTemplate
.postForm(url, Header.EMPTY, params, bodyMap, String.class);
.postForm(url, Header.EMPTY, Query.newInstance().initParams(params), bodyMap, String.class);
if (!restResult.ok()) {
SECURITY_LOGGER.error("login failed: {}", JacksonUtils.toJson(restResult));
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public static String decode(String str, String encode) throws UnsupportedEncodin
* @return {@link URI}
*/
public static URI buildUri(String url, Query query) throws URISyntaxException {
if (!query.isEmpty()) {
if (query != null && !query.isEmpty()) {
url = url + "?" + query.toQueryUrl();
}
return new URI(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,6 @@ public <T> void get(String url, Header header, Query query, Type responseType, C
execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseType, callback);
}

/**
* async http get URL request params are expanded using the given map {@code paramValues}.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type.
*
* <p>{@code callback} Result callback execution
* if you need response headers, you can convert the received RestResult to HttpRestResult.
*
* @param url url
* @param header headers
* @param paramValues paramValues
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void get(String url, Header header, Map<String, String> paramValues, Type responseType,
Callback<T> callback) {
execute(url, HttpMethod.GET, new RequestHttpEntity(header, Query.newInstance().initParams(paramValues)),
responseType, callback);
}

/**
* async get request, may be pulling a lot of data URL request params are expanded using the given query {@link
* Query}, More request parameters can be set via body.
Expand Down Expand Up @@ -150,7 +130,7 @@ public <T> void put(String url, Header header, Query query, Object body, Type re
* async http put Json Create a new resource by PUTting the given body to http request, http header contentType
* default 'application/json;charset=UTF-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
* <p>URL request params are expanded using the given query {@link Query}.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type
*
Expand All @@ -159,15 +139,36 @@ public <T> void put(String url, Header header, Query query, Object body, Type re
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param query http query param
* @param body http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void putJson(String url, Header header, Map<String, String> paramValues, String body, Type responseType,
public <T> void putJson(String url, Header header, Query query, String body, Type responseType,
Callback<T> callback) {
execute(url, HttpMethod.PUT, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON),
Query.newInstance().initParams(paramValues), body), responseType, callback);
execute(url, HttpMethod.PUT,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON), query, body), responseType,
callback);
}

/**
* async http put Json Create a new resource by PUTting the given body to http request, http header contentType
* default 'application/json;charset=UTF-8'.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type
*
* <p>{@code callback} Result callback execution,
* if you need response headers, you can convert the received RestResult to HttpRestResult.
*
* @param url url
* @param header http header param
* @param body http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void putJson(String url, Header header, String body, Type responseType, Callback<T> callback) {
execute(url, HttpMethod.PUT, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON), body),
responseType, callback);
}

/**
Expand Down Expand Up @@ -199,24 +200,22 @@ public <T> void putForm(String url, Header header, Query query, Map<String, Stri
* async http put from Create a new resource by PUTting the given map {@code bodyValues} to http request, http
* header contentType default 'application/x-www-form-urlencoded;charset=utf-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type.
*
* <p>{@code callback} Result callback execution,
* if you need response headers, you can convert the received RestResult to HttpRestResult.
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param bodyValues http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void putForm(String url, Header header, Map<String, String> paramValues, Map<String, String> bodyValues,
Type responseType, Callback<T> callback) {
execute(url, HttpMethod.PUT, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues), responseType, callback);
public <T> void putForm(String url, Header header, Map<String, String> bodyValues, Type responseType,
Callback<T> callback) {
execute(url, HttpMethod.PUT,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), bodyValues),
responseType, callback);
}

/**
Expand Down Expand Up @@ -244,7 +243,7 @@ public <T> void post(String url, Header header, Query query, Object body, Type r
* async http post Json Create a new resource by POSTing the given object to the http request, http header
* contentType default 'application/json;charset=UTF-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
* <p>URL request params are expanded using the given query {@link Query}.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type.
*
Expand All @@ -253,15 +252,36 @@ public <T> void post(String url, Header header, Query query, Object body, Type r
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param query http query param
* @param body http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void postJson(String url, Header header, Map<String, String> paramValues, String body, Type responseType,
public <T> void postJson(String url, Header header, Query query, String body, Type responseType,
Callback<T> callback) {
execute(url, HttpMethod.POST, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON),
Query.newInstance().initParams(paramValues), body), responseType, callback);
execute(url, HttpMethod.POST,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON), query, body), responseType,
callback);
}

/**
* async http post Json Create a new resource by POSTing the given object to the http request, http header
* contentType default 'application/json;charset=UTF-8'.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type.
*
* <p>{@code callback} Result callback execution,
* if you need response headers, you can convert the received RestResult to HttpRestResult.
*
* @param url url
* @param header http header param
* @param body http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void postJson(String url, Header header, String body, Type responseType, Callback<T> callback) {
execute(url, HttpMethod.POST, new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON), body),
responseType, callback);
}

/**
Expand Down Expand Up @@ -293,26 +313,22 @@ public <T> void postForm(String url, Header header, Query query, Map<String, Str
* async http post from Create a new resource by PUTting the given map {@code bodyValues} to http request, http
* header contentType default 'application/x-www-form-urlencoded;charset=utf-8'.
*
* <p>URL request params are expanded using the given map {@code paramValues}.
*
* <p>{@code responseType} can be an RestResult or RestResult data {@code T} type.
*
* <p>{@code callback} Result callback execution,
* if you need response headers, you can convert the received RestResult to HttpRestResult.
*
* @param url url
* @param header http header param
* @param paramValues http query param
* @param bodyValues http body param
* @param responseType return type
* @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)}
*/
public <T> void postForm(String url, Header header, Map<String, String> paramValues, Map<String, String> bodyValues,
Type responseType, Callback<T> callback) {
public <T> void postForm(String url, Header header, Map<String, String> bodyValues, Type responseType,
Callback<T> callback) {
execute(url, HttpMethod.POST,
new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED),
Query.newInstance().initParams(paramValues), bodyValues), responseType, callback);

new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), bodyValues),
responseType, callback);
}

@SuppressWarnings("unchecked")
Expand Down
Loading

0 comments on commit 136b456

Please sign in to comment.