diff --git a/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpClient.java b/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpClient.java index fe75b0ca513..0550c3e0440 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpClient.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.common.http; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.handler.ResponseHandler; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; @@ -97,19 +98,11 @@ protected HttpRequestBase build(String url, Header header, String method) throws protected HttpRequestBase build(String url, Header header, Object body, String method) throws Exception { - BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method); - httpMethod.init(url); - httpMethod.initHeader(header); - httpMethod.initEntity(body, header.getValue("Content-Type")); - return httpMethod.getRequestBase(); - } - - private Header convertHeader(org.apache.http.Header[] headers) { - final Header nHeader = Header.newInstance(); - for (org.apache.http.Header header : headers) { - nHeader.addParam(header.getName(), header.getValue()); - } - return nHeader; + final BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method); + final HttpRequestBase httpRequestBase = httpMethod.init(url); + HttpUtils.initRequestHeader(httpRequestBase, header); + HttpUtils.initRequestEntity(httpRequestBase, body, header.getValue(HttpHeaderConsts.CONTENT_TYPE)); + return httpRequestBase; } public static class HttpGetWithEntity extends HttpEntityEnclosingRequestBase { diff --git a/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java b/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java index 5caea1577c3..30bd8a9d111 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java @@ -16,21 +16,9 @@ package com.alibaba.nacos.common.http; -import com.alibaba.nacos.common.http.handler.RequestHandler; -import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.utils.HttpMethod; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - import com.alibaba.nacos.common.utils.StringUtils; -import org.apache.http.HttpEntity; -import org.apache.http.HttpEntityEnclosingRequest; -import org.apache.http.HttpRequest; -import org.apache.http.NameValuePair; -import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; @@ -39,9 +27,6 @@ import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpTrace; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.apache.http.message.BasicNameValuePair; /** * Base http method. @@ -139,78 +124,18 @@ protected HttpRequestBase createRequest(String url) { private String name; - private HttpRequest requestBase; - BaseHttpMethod(String name) { this.name = name; } - public void init(String url) { - requestBase = createRequest(url); + public HttpRequestBase init(String url) { + return createRequest(url); } protected HttpRequestBase createRequest(String url) { throw new UnsupportedOperationException(); } - /** - * Init http header. - * - * @param header header - */ - public void initHeader(Header header) { - Iterator> iterator = header.iterator(); - while (iterator.hasNext()) { - Map.Entry entry = iterator.next(); - requestBase.setHeader(entry.getKey(), entry.getValue()); - } - } - - /** - * Init http entity. - * - * @param body body - * @param mediaType mediaType {@link ContentType} - * @throws Exception exception - */ - public void initEntity(Object body, String mediaType) throws Exception { - if (body == null) { - return; - } - if (requestBase instanceof HttpEntityEnclosingRequest) { - HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase; - ContentType contentType = ContentType.create(mediaType); - StringEntity entity = new StringEntity(RequestHandler.parse(body), contentType); - request.setEntity(entity); - } - } - - /** - * Init request from entity map. - * - * @param body body map - * @param charset charset of entity - * @throws Exception exception - */ - public void initFromEntity(Map body, String charset) throws Exception { - if (body == null || body.isEmpty()) { - return; - } - List params = new ArrayList(body.size()); - for (Map.Entry entry : body.entrySet()) { - params.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); - } - if (requestBase instanceof HttpEntityEnclosingRequest) { - HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase; - HttpEntity entity = new UrlEncodedFormEntity(params, charset); - request.setEntity(entity); - } - } - - public HttpRequestBase getRequestBase() { - return (HttpRequestBase) requestBase; - } - /** * Value of {@link BaseHttpMethod}. * 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 215fde44097..887d04aed17 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 @@ -16,14 +16,25 @@ package com.alibaba.nacos.common.http; +import com.alibaba.nacos.common.http.handler.RequestHandler; +import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.utils.StringUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpEntityEnclosingRequest; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.message.BasicNameValuePair; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.net.URLDecoder; import java.net.URLEncoder; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -40,6 +51,63 @@ public final class HttpUtils { private static final Pattern CONTEXT_PATH_MATCH = Pattern.compile("(\\/)\\1+"); + /** + * Init http header. + * + * @param requestBase requestBase {@link HttpRequestBase} + * @param header header + */ + public static void initRequestHeader(HttpRequestBase requestBase, Header header) { + Iterator> iterator = header.iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + requestBase.setHeader(entry.getKey(), entry.getValue()); + } + } + + /** + * Init http entity. + * + * @param requestBase requestBase {@link HttpRequestBase} + * @param body body + * @param mediaType mediaType {@link ContentType} + * @throws Exception exception + */ + public static void initRequestEntity(HttpRequestBase requestBase, Object body, String mediaType) throws Exception { + if (body == null) { + return; + } + if (requestBase instanceof HttpEntityEnclosingRequest) { + HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase; + ContentType contentType = ContentType.create(mediaType); + StringEntity entity = new StringEntity(RequestHandler.parse(body), contentType); + request.setEntity(entity); + } + } + + /** + * Init request from entity map. + * + * @param requestBase requestBase {@link HttpRequestBase} + * @param body body map + * @param charset charset of entity + * @throws Exception exception + */ + public static void initRequestFromEntity(HttpRequestBase requestBase, Map body, String charset) throws Exception { + if (body == null || body.isEmpty()) { + return; + } + List params = new ArrayList(body.size()); + for (Map.Entry entry : body.entrySet()) { + params.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + } + if (requestBase instanceof HttpEntityEnclosingRequest) { + HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase; + HttpEntity entity = new UrlEncodedFormEntity(params, charset); + request.setEntity(entity); + } + } + /** * Build URL. * diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/request/DefaultHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/request/DefaultHttpClientRequest.java index b36fc478039..2a84c390b3c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/request/DefaultHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/request/DefaultHttpClientRequest.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.BaseHttpMethod; import com.alibaba.nacos.common.http.HttpClientConfig; +import com.alibaba.nacos.common.http.HttpUtils; import com.alibaba.nacos.common.http.client.response.DefaultClientHttpResponse; import com.alibaba.nacos.common.http.client.response.HttpClientResponse; import com.alibaba.nacos.common.http.param.Header; @@ -56,19 +57,18 @@ public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity } static HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity) throws Exception { - Header headers = requestHttpEntity.getHeaders(); - BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method); - httpMethod.init(uri.toString()); - httpMethod.initHeader(headers); + final Header headers = requestHttpEntity.getHeaders(); + final BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method); + final HttpRequestBase httpRequestBase = httpMethod.init(uri.toString()); + HttpUtils.initRequestHeader(httpRequestBase, headers); if (MediaType.APPLICATION_FORM_URLENCODED.equals(headers.getValue(HttpHeaderConsts.CONTENT_TYPE)) && requestHttpEntity.getBody() instanceof Map) { - httpMethod.initFromEntity((Map) requestHttpEntity.getBody(), headers.getCharset()); + HttpUtils.initRequestFromEntity(httpRequestBase, (Map) requestHttpEntity.getBody(), headers.getCharset()); } else { - httpMethod.initEntity(requestHttpEntity.getBody(), headers.getValue(HttpHeaderConsts.CONTENT_TYPE)); + HttpUtils.initRequestEntity(httpRequestBase, requestHttpEntity.getBody(), headers.getValue(HttpHeaderConsts.CONTENT_TYPE)); } - HttpRequestBase requestBase = httpMethod.getRequestBase(); - replaceDefaultConfig(requestBase, requestHttpEntity.getHttpClientConfig()); - return requestBase; + replaceDefaultConfig(httpRequestBase, requestHttpEntity.getHttpClientConfig()); + return httpRequestBase; } /**