forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE alibaba#3210] Enhanced nacos resttemplate response handler (al…
…ibaba#3212) * Enhanced nacos resttemplate response handler * Enhanced nacos resttemplate response handler * Add license * [alibaba#3212] Modify some class name and comment * [alibaba#3212] Modify some class name and comment * [alibaba#3212] Modify some class name and comment * [alibaba#3212] change the name of property * Fix code style issue
- Loading branch information
Showing
16 changed files
with
393 additions
and
81 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
32 changes: 32 additions & 0 deletions
32
common/src/main/java/com/alibaba/nacos/common/constant/ResponseHandlerType.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,32 @@ | ||
/* | ||
* 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.constant; | ||
|
||
/** | ||
* Response Handler Type. | ||
* | ||
* @author mai.jh | ||
*/ | ||
public final class ResponseHandlerType { | ||
|
||
public static final String STRING_TYPE = "java.lang.String"; | ||
|
||
public static final String RESTRESULT_TYPE = "com.alibaba.nacos.common.model.RestResult"; | ||
|
||
public static final String DEFAULT_BEAN_TYPE = "default_bean_handler"; | ||
|
||
} |
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
76 changes: 76 additions & 0 deletions
76
common/src/main/java/com/alibaba/nacos/common/http/client/AbstractNacosRestTemplate.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,76 @@ | ||
/* | ||
* 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.constant.ResponseHandlerType; | ||
import com.alibaba.nacos.common.utils.JacksonUtils; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* For NacosRestTemplate and NacosAsyncRestTemplate, provide initialization and register of response converter. | ||
* | ||
* @author mai.jh | ||
*/ | ||
public abstract class AbstractNacosRestTemplate { | ||
|
||
private final Map<String, ResponseHandler> responseHandlerMap = new HashMap<String, ResponseHandler>(); | ||
|
||
public AbstractNacosRestTemplate() { | ||
// init response handler | ||
responseHandlerMap.put(ResponseHandlerType.STRING_TYPE, new StringResponseHandler()); | ||
responseHandlerMap.put(ResponseHandlerType.RESTRESULT_TYPE, new RestResultResponseHandler()); | ||
responseHandlerMap.put(ResponseHandlerType.DEFAULT_BEAN_TYPE, new BeanResponseHandler()); | ||
} | ||
|
||
/** | ||
* register customization Response Handler. | ||
* | ||
* @param responseHandler {@link ResponseHandler} | ||
*/ | ||
public void registerResponseHandler(String responseHandlerType, ResponseHandler responseHandler) { | ||
responseHandlerMap.put(responseHandlerType, responseHandler); | ||
} | ||
|
||
/** | ||
* Select a response handler by responseType. | ||
* | ||
* @param responseType responseType | ||
* @return ResponseHandler | ||
*/ | ||
protected ResponseHandler selectResponseHandler(Type responseType) { | ||
ResponseHandler responseHandler = null; | ||
if (responseType == null) { | ||
responseHandler = responseHandlerMap.get(ResponseHandlerType.STRING_TYPE); | ||
} | ||
if (responseHandler == null) { | ||
JavaType javaType = JacksonUtils.constructJavaType(responseType); | ||
String name = javaType.getRawClass().getName(); | ||
responseHandler = responseHandlerMap.get(name); | ||
} | ||
// When the corresponding type of response handler cannot be obtained, | ||
// the default bean response handler is used | ||
if (responseHandler == null) { | ||
responseHandler = responseHandlerMap.get(ResponseHandlerType.DEFAULT_BEAN_TYPE); | ||
} | ||
responseHandler.setResponseType(responseType); | ||
return responseHandler; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
common/src/main/java/com/alibaba/nacos/common/http/client/AbstractResponseHandler.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,64 @@ | ||
/* | ||
* 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.HttpRestResult; | ||
import com.alibaba.nacos.common.http.param.Header; | ||
import com.alibaba.nacos.common.utils.IoUtils; | ||
import org.apache.http.HttpStatus; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Abstract response handler. | ||
* | ||
* @author mai.jh | ||
*/ | ||
public abstract class AbstractResponseHandler<T> implements ResponseHandler<T> { | ||
|
||
private Type responseType; | ||
|
||
@Override | ||
public final void setResponseType(Type responseType) { | ||
this.responseType = responseType; | ||
} | ||
|
||
@Override | ||
public final HttpRestResult<T> handle(HttpClientResponse response) throws Exception { | ||
if (HttpStatus.SC_OK != response.getStatusCode()) { | ||
return handleError(response); | ||
} | ||
return convertResult(response, this.responseType); | ||
} | ||
|
||
private HttpRestResult<T> handleError(HttpClientResponse response) throws Exception { | ||
Header headers = response.getHeaders(); | ||
String message = IoUtils.toString(response.getBody(), headers.getCharset()); | ||
return new HttpRestResult<T>(headers, response.getStatusCode(), null, message); | ||
} | ||
|
||
/** | ||
* Abstract convertResult method, Different types of converters for expansion. | ||
* | ||
* @param response http client response | ||
* @param responseType responseType | ||
* @return HttpRestResult | ||
* @throws Exception ex | ||
*/ | ||
public abstract HttpRestResult<T> convertResult(HttpClientResponse response, Type responseType) throws Exception; | ||
|
||
} |
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
41 changes: 41 additions & 0 deletions
41
common/src/main/java/com/alibaba/nacos/common/http/client/BeanResponseHandler.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,41 @@ | ||
/* | ||
* 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.HttpRestResult; | ||
import com.alibaba.nacos.common.http.param.Header; | ||
import com.alibaba.nacos.common.utils.JacksonUtils; | ||
|
||
import java.io.InputStream; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* bean response handler, | ||
* Mainly converter response type as bean type. | ||
* | ||
* @author mai.jh | ||
*/ | ||
public class BeanResponseHandler<T> extends AbstractResponseHandler<T> { | ||
|
||
@Override | ||
public HttpRestResult<T> convertResult(HttpClientResponse response, Type responseType) throws Exception { | ||
final Header headers = response.getHeaders(); | ||
InputStream body = response.getBody(); | ||
T extractBody = JacksonUtils.toObj(body, responseType); | ||
return new HttpRestResult<T>(headers, response.getStatusCode(), extractBody, null); | ||
} | ||
} |
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
Oops, something went wrong.