-
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.
Browse files
Browse the repository at this point in the history
* enhance nacosRestTemplate * enhance nacosRestTemplate * supplement throw exception * Modify the iterate method of the interceptor and modify some method name * Adjust the way to get HttpClientRequest implement in NacosRestTempalte * Fix code style issue * Fix code style issue * Fix code style issue * Fix code style issue * Log output change
- Loading branch information
Showing
8 changed files
with
401 additions
and
7 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
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.