-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ISSUE #3197] NacosRestTemplate enhance #3198
Merged
KomachiSion
merged 12 commits into
alibaba:develop
from
Maijh97:nacos_resttemplate_enhance
Jul 13, 2020
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e2cfc78
enhance nacosRestTemplate
Maijh97 299b2f9
enhance nacosRestTemplate
Maijh97 09aa8f9
supplement throw exception
Maijh97 68bc514
Modify the iterate method of the interceptor and modify some method name
Maijh97 0a6346c
Adjust the way to get HttpClientRequest implement in NacosRestTempalte
Maijh97 d19409e
Merge branch 'develop-upstrem' into nacos_resttemplate_enhance
Maijh97 cc0888f
Fix code style issue
Maijh97 0b4038d
Fix code style issue
Maijh97 5caa7c8
Fix code style issue
Maijh97 2abb429
Fix code style issue
Maijh97 daee273
Log output change
Maijh97 d167fcb
Merge branch 'develop-upstrem' into nacos_resttemplate_enhance
Maijh97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 47 additions & 0 deletions
47
common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequestInterceptor.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,47 @@ | ||
/* | ||
* 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.model.RequestHttpEntity; | ||
|
||
import java.net.URI; | ||
|
||
/** | ||
* Intercepts client-side HTTP requests. Implementations of this interface can be. | ||
* | ||
* @author mai.jh | ||
*/ | ||
public interface HttpClientRequestInterceptor { | ||
|
||
/** | ||
* is intercept. | ||
* | ||
* @param uri uri | ||
* @param httpMethod http method | ||
* @param requestHttpEntity request entity | ||
* @return boolean | ||
*/ | ||
boolean isIntercept(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity); | ||
|
||
/** | ||
* if isIntercept method is true Intercept the given request, and return a response Otherwise, | ||
* the {@link HttpClientRequest} will be used for execution. | ||
* | ||
* @return HttpClientResponse | ||
*/ | ||
HttpClientResponse intercept(); | ||
} |
58 changes: 58 additions & 0 deletions
58
common/src/main/java/com/alibaba/nacos/common/http/client/InterceptingHttpClientRequest.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,58 @@ | ||
/* | ||
* 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.model.RequestHttpEntity; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* Wrap http client request and perform corresponding interception. | ||
* | ||
* @author mai.jh | ||
*/ | ||
public class InterceptingHttpClientRequest implements HttpClientRequest { | ||
|
||
private final HttpClientRequest httpClientRequest; | ||
|
||
private final Iterator<HttpClientRequestInterceptor> interceptors; | ||
|
||
public InterceptingHttpClientRequest(HttpClientRequest httpClientRequest, | ||
Iterator<HttpClientRequestInterceptor> interceptors) { | ||
this.httpClientRequest = httpClientRequest; | ||
this.interceptors = interceptors; | ||
} | ||
|
||
@Override | ||
public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity) | ||
throws Exception { | ||
while (interceptors.hasNext()) { | ||
HttpClientRequestInterceptor nextInterceptor = interceptors.next(); | ||
if (nextInterceptor.isIntercept(uri, httpMethod, requestHttpEntity)) { | ||
return nextInterceptor.intercept(); | ||
} | ||
} | ||
return httpClientRequest.execute(uri, httpMethod, requestHttpEntity); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
httpClientRequest.close(); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we print the interceptor name or class name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay