-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2926 from Maijh97/develop
[ISSUE #2858] Unified implementation of Http requests
- Loading branch information
Showing
22 changed files
with
1,690 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,22 @@ | |
|
||
package com.alibaba.nacos.common.http; | ||
|
||
import com.alibaba.nacos.api.common.Constants; | ||
import com.alibaba.nacos.common.http.handler.RequestHandler; | ||
import com.alibaba.nacos.common.http.param.Header; | ||
import com.alibaba.nacos.common.http.param.MediaType; | ||
import com.alibaba.nacos.common.utils.HttpMethod; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.commons.lang3.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; | ||
|
@@ -34,6 +42,7 @@ | |
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; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">liaochuntao</a> | ||
|
@@ -157,7 +166,6 @@ 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); | ||
|
@@ -166,6 +174,21 @@ public void initEntity(Object body, String mediaType) throws Exception { | |
} | ||
} | ||
|
||
public void initFromEntity(Map<String, String> body, String charset) throws Exception{ | ||
if (body.isEmpty()) { | ||
return; | ||
} | ||
List<NameValuePair> params = new ArrayList<NameValuePair>(body.size()); | ||
for (Map.Entry<String, String> 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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
common/src/main/java/com/alibaba/nacos/common/http/HttpRestResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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; | ||
|
||
import com.alibaba.nacos.common.http.param.Header; | ||
import com.alibaba.nacos.common.model.RestResult; | ||
|
||
/** | ||
* http RestResult | ||
* | ||
* @author mai.jh | ||
* @date 2020/5/31 | ||
*/ | ||
public class HttpRestResult<T> extends RestResult<T> { | ||
|
||
private static final long serialVersionUID = 3766947816720175947L; | ||
private Header header; | ||
|
||
public HttpRestResult() { | ||
} | ||
|
||
public HttpRestResult(Header header, int code, T data) { | ||
super(code, data); | ||
this.header = header; | ||
} | ||
|
||
public Header getHeader() { | ||
return header; | ||
} | ||
|
||
public void setHeader(Header header) { | ||
this.header = header; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
common/src/main/java/com/alibaba/nacos/common/http/client/AsyncHttpClientRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.client; | ||
|
||
import com.alibaba.nacos.common.http.Callback; | ||
import com.alibaba.nacos.common.model.RequestHttpEntity; | ||
|
||
import java.io.Closeable; | ||
import java.lang.reflect.Type; | ||
import java.net.URI; | ||
|
||
/** | ||
* Represents a client-side Async HTTP request. | ||
* Created via an implementation execute. | ||
* | ||
* @author mai.jh | ||
* @date 2020/5/29 | ||
*/ | ||
public interface AsyncHttpClientRequest extends Closeable { | ||
|
||
|
||
/** | ||
* execute async http request | ||
* | ||
* @param uri http url | ||
* @param httpMethod http request method | ||
* @param requestHttpEntity http request entity | ||
* @param responseType http response type | ||
* @param callback http response callback | ||
* @throws Exception ex | ||
*/ | ||
<T> void execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity, | ||
final Type responseType, final Callback<T> callback) throws Exception; | ||
} |
82 changes: 82 additions & 0 deletions
82
common/src/main/java/com/alibaba/nacos/common/http/client/DefaultAsyncHttpClientRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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.client; | ||
|
||
import com.alibaba.nacos.common.http.Callback; | ||
import com.alibaba.nacos.common.http.HttpRestResult; | ||
import com.alibaba.nacos.common.http.handler.ResponseHandler; | ||
import com.alibaba.nacos.common.model.RequestHttpEntity; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.methods.HttpRequestBase; | ||
import org.apache.http.concurrent.FutureCallback; | ||
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.net.URI; | ||
|
||
/** | ||
* {@link AsyncHttpClientRequest} implementation that uses apache async http client to | ||
* execute streaming requests | ||
* | ||
* @author mai.jh | ||
* @date 2020/5/29 | ||
*/ | ||
public class DefaultAsyncHttpClientRequest implements AsyncHttpClientRequest { | ||
|
||
private final CloseableHttpAsyncClient asyncClient; | ||
|
||
public DefaultAsyncHttpClientRequest(CloseableHttpAsyncClient asyncClient) { | ||
this.asyncClient = asyncClient; | ||
if (!this.asyncClient.isRunning()) { | ||
this.asyncClient.start(); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> void execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity, final Type responseType, final Callback<T> callback) throws Exception { | ||
HttpRequestBase httpRequestBase = DefaultHttpClientRequest.build(uri, httpMethod, requestHttpEntity); | ||
asyncClient.execute(httpRequestBase, new FutureCallback<HttpResponse>() { | ||
@Override | ||
public void completed(HttpResponse result) { | ||
DefaultClientHttpResponse response = new DefaultClientHttpResponse(result); | ||
try { | ||
HttpRestResult<T> httpRestResult = ResponseHandler.responseEntityExtractor(response, responseType); | ||
callback.onReceive(httpRestResult); | ||
} catch (Exception e) { | ||
callback.onError(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void failed(Exception ex) { | ||
callback.onError(ex); | ||
} | ||
|
||
@Override | ||
public void cancelled() { | ||
|
||
} | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
this.asyncClient.close(); | ||
} | ||
} |
Oops, something went wrong.