Commit 9138da46 by Nepxion

重构类结构

parent ec15c596
......@@ -31,6 +31,11 @@
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
......
package com.nepxion.discovery.console.handler;
package com.nepxion.discovery.common.handler;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -18,7 +18,7 @@ import org.springframework.web.client.DefaultResponseErrorHandler;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
public class ConsoleErrorHandler extends DefaultResponseErrorHandler {
public class RestErrorHandler extends DefaultResponseErrorHandler {
private String cause;
@Override
......
......@@ -9,8 +9,6 @@ package com.nepxion.discovery.common.util;
* @version 1.0
*/
import java.text.SimpleDateFormat;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
......@@ -22,7 +20,7 @@ public class JsonUtil {
static {
objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
// objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
}
public static <T> String toJson(T object) {
......
package com.nepxion.discovery.common.util;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference;
import com.nepxion.discovery.common.handler.RestErrorHandler;
public class RestUtil {
public static <T> T fromJson(RestTemplate restTemplate, String result, TypeReference<T> typeReference) {
try {
return JsonUtil.fromJson(result, typeReference);
} catch (Exception e) {
String cause = getCause(restTemplate);
if (StringUtils.isNotEmpty(cause)) {
throw new IllegalArgumentException(cause);
}
throw e;
}
}
public static String getCause(RestTemplate restTemplate) {
ResponseErrorHandler responseErrorHandler = restTemplate.getErrorHandler();
if (responseErrorHandler instanceof RestErrorHandler) {
RestErrorHandler errorHandler = (RestErrorHandler) responseErrorHandler;
return errorHandler.getCause();
}
return null;
}
}
\ No newline at end of file
......@@ -12,6 +12,18 @@ package com.nepxion.discovery.common.util;
import org.apache.commons.lang3.StringUtils;
public class UrlUtil {
public static String formatUrl(String url) {
if (!url.startsWith("http://")) {
url = "http://" + url;
}
if (!url.endsWith("/")) {
url = url + "/";
}
return url;
}
public static String formatContextPath(String contextPath) {
if (StringUtils.isEmpty(contextPath)) {
contextPath = "/";
......
......@@ -21,7 +21,8 @@ import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference;
import com.nepxion.discovery.common.entity.ResultEntity;
import com.nepxion.discovery.common.entity.RouterEntity;
import com.nepxion.discovery.common.util.JsonUtil;
import com.nepxion.discovery.common.handler.RestErrorHandler;
import com.nepxion.discovery.common.util.RestUtil;
import com.nepxion.discovery.common.util.UrlUtil;
import com.nepxion.discovery.console.desktop.context.PropertiesContext;
import com.nepxion.discovery.console.desktop.entity.Instance;
......@@ -31,7 +32,7 @@ public class ServiceController {
static {
restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new ServiceErrorHandler());
restTemplate.setErrorHandler(new RestErrorHandler());
}
public static Map<String, List<Instance>> getInstanceMap() {
......@@ -39,7 +40,7 @@ public class ServiceController {
String result = restTemplate.getForEntity(url, String.class).getBody();
return convert(result, new TypeReference<Map<String, List<Instance>>>() {
return RestUtil.fromJson(restTemplate, result, new TypeReference<Map<String, List<Instance>>>() {
});
}
......@@ -48,7 +49,7 @@ public class ServiceController {
String result = restTemplate.getForEntity(url, String.class).getBody();
return convert(result, new TypeReference<List<String>>() {
return RestUtil.fromJson(restTemplate, result, new TypeReference<List<String>>() {
});
}
......@@ -57,7 +58,7 @@ public class ServiceController {
String result = restTemplate.getForEntity(url, String.class).getBody();
return convert(result, new TypeReference<List<String>>() {
return RestUtil.fromJson(restTemplate, result, new TypeReference<List<String>>() {
});
}
......@@ -66,7 +67,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, routeServiceIds, String.class).getBody();
return convert(result, new TypeReference<RouterEntity>() {
return RestUtil.fromJson(restTemplate, result, new TypeReference<RouterEntity>() {
});
}
......@@ -81,8 +82,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, entity, String.class).getBody();
if (!StringUtils.equals(result, "OK") && !StringUtils.equals(result, "NO")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause();
result = RestUtil.getCause(restTemplate);
}
return result;
......@@ -94,8 +94,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, null, String.class).getBody();
if (!StringUtils.equals(result, "OK") && !StringUtils.equals(result, "NO")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause();
result = RestUtil.getCause(restTemplate);
}
return result;
......@@ -119,7 +118,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, entity, String.class).getBody();
return convert(result, new TypeReference<List<ResultEntity>>() {
return RestUtil.fromJson(restTemplate, result, new TypeReference<List<ResultEntity>>() {
});
}
......@@ -134,8 +133,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, entity, String.class).getBody();
if (!StringUtils.equals(result, "OK")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause();
result = RestUtil.getCause(restTemplate);
}
return result;
......@@ -146,7 +144,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, null, String.class).getBody();
return convert(result, new TypeReference<List<ResultEntity>>() {
return RestUtil.fromJson(restTemplate, result, new TypeReference<List<ResultEntity>>() {
});
}
......@@ -156,8 +154,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, null, String.class).getBody();
if (!StringUtils.equals(result, "OK")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause();
result = RestUtil.getCause(restTemplate);
}
return result;
......@@ -168,7 +165,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, version, String.class).getBody();
return convert(result, new TypeReference<List<ResultEntity>>() {
return RestUtil.fromJson(restTemplate, result, new TypeReference<List<ResultEntity>>() {
});
}
......@@ -178,8 +175,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, version, String.class).getBody();
if (!StringUtils.equals(result, "OK")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause();
result = RestUtil.getCause(restTemplate);
}
return result;
......@@ -190,7 +186,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, null, String.class).getBody();
return convert(result, new TypeReference<List<ResultEntity>>() {
return RestUtil.fromJson(restTemplate, result, new TypeReference<List<ResultEntity>>() {
});
}
......@@ -200,8 +196,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, null, String.class).getBody();
if (!StringUtils.equals(result, "OK")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause();
result = RestUtil.getCause(restTemplate);
}
return result;
......@@ -209,11 +204,8 @@ public class ServiceController {
private static String getUrl() {
String url = PropertiesContext.getProperties().getString("url");
if (!url.endsWith("/")) {
url += "/";
}
return url;
return UrlUtil.formatUrl(url);
}
private static String getUrl(Instance instance) {
......@@ -225,15 +217,4 @@ public class ServiceController {
private static String getInvokeType(boolean async) {
return async ? "async" : "sync";
}
private static <T> T convert(String result, TypeReference<T> typeReference) {
try {
return JsonUtil.fromJson(result, typeReference);
} catch (Exception e) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause();
throw new IllegalArgumentException(result);
}
}
}
\ No newline at end of file
package com.nepxion.discovery.console.desktop.controller;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
public class ServiceErrorHandler extends DefaultResponseErrorHandler {
private String cause;
@Override
public void handleError(ClientHttpResponse response) throws IOException {
// 这里绝对不能关闭InputStream
InputStream inputStream = response.getBody();
cause = IOUtils.toString(inputStream, "UTF-8");
}
public String getCause() {
return cause;
}
}
\ No newline at end of file
......@@ -16,8 +16,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.common.handler.RestErrorHandler;
import com.nepxion.discovery.console.endpoint.ConsoleEndpoint;
import com.nepxion.discovery.console.handler.ConsoleErrorHandler;
@Configuration
@Import(SwaggerConfiguration.class)
......@@ -46,7 +46,7 @@ public class ConsoleAutoConfiguration {
@Bean
public RestTemplate consoleRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new ConsoleErrorHandler());
restTemplate.setErrorHandler(new RestErrorHandler());
return restTemplate;
}
......
......@@ -24,8 +24,8 @@ import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.common.entity.ResultEntity;
import com.nepxion.discovery.common.util.RestUtil;
import com.nepxion.discovery.common.util.UrlUtil;
import com.nepxion.discovery.console.handler.ConsoleErrorHandler;
public abstract class AbstractRestInvoker {
private static final Logger LOG = LoggerFactory.getLogger(AbstractRestInvoker.class);
......@@ -61,8 +61,7 @@ public abstract class AbstractRestInvoker {
result = doRest(url);
if (!StringUtils.equals(result, "OK")) {
ConsoleErrorHandler errorHandler = (ConsoleErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause();
result = RestUtil.getCause(restTemplate);
}
} catch (Exception e) {
result = e.getMessage();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment