Commit 9138da46 by Nepxion

重构类结构

parent ec15c596
...@@ -31,6 +31,11 @@ ...@@ -31,6 +31,11 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
</dependency> </dependency>
......
package com.nepxion.discovery.console.handler; package com.nepxion.discovery.common.handler;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
...@@ -18,7 +18,7 @@ import org.springframework.web.client.DefaultResponseErrorHandler; ...@@ -18,7 +18,7 @@ import org.springframework.web.client.DefaultResponseErrorHandler;
import com.nepxion.discovery.common.constant.DiscoveryConstant; import com.nepxion.discovery.common.constant.DiscoveryConstant;
public class ConsoleErrorHandler extends DefaultResponseErrorHandler { public class RestErrorHandler extends DefaultResponseErrorHandler {
private String cause; private String cause;
@Override @Override
......
...@@ -9,8 +9,6 @@ package com.nepxion.discovery.common.util; ...@@ -9,8 +9,6 @@ package com.nepxion.discovery.common.util;
* @version 1.0 * @version 1.0
*/ */
import java.text.SimpleDateFormat;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
...@@ -22,7 +20,7 @@ public class JsonUtil { ...@@ -22,7 +20,7 @@ public class JsonUtil {
static { static {
objectMapper = new ObjectMapper(); 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) { 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; ...@@ -12,6 +12,18 @@ package com.nepxion.discovery.common.util;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
public class UrlUtil { 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) { public static String formatContextPath(String contextPath) {
if (StringUtils.isEmpty(contextPath)) { if (StringUtils.isEmpty(contextPath)) {
contextPath = "/"; contextPath = "/";
......
...@@ -21,7 +21,8 @@ import org.springframework.web.client.RestTemplate; ...@@ -21,7 +21,8 @@ import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.nepxion.discovery.common.entity.ResultEntity; import com.nepxion.discovery.common.entity.ResultEntity;
import com.nepxion.discovery.common.entity.RouterEntity; 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.common.util.UrlUtil;
import com.nepxion.discovery.console.desktop.context.PropertiesContext; import com.nepxion.discovery.console.desktop.context.PropertiesContext;
import com.nepxion.discovery.console.desktop.entity.Instance; import com.nepxion.discovery.console.desktop.entity.Instance;
...@@ -31,7 +32,7 @@ public class ServiceController { ...@@ -31,7 +32,7 @@ public class ServiceController {
static { static {
restTemplate = new RestTemplate(); restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new ServiceErrorHandler()); restTemplate.setErrorHandler(new RestErrorHandler());
} }
public static Map<String, List<Instance>> getInstanceMap() { public static Map<String, List<Instance>> getInstanceMap() {
...@@ -39,7 +40,7 @@ public class ServiceController { ...@@ -39,7 +40,7 @@ public class ServiceController {
String result = restTemplate.getForEntity(url, String.class).getBody(); 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 { ...@@ -48,7 +49,7 @@ public class ServiceController {
String result = restTemplate.getForEntity(url, String.class).getBody(); 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 { ...@@ -57,7 +58,7 @@ public class ServiceController {
String result = restTemplate.getForEntity(url, String.class).getBody(); 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 { ...@@ -66,7 +67,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, routeServiceIds, String.class).getBody(); 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 { ...@@ -81,8 +82,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, entity, String.class).getBody(); String result = restTemplate.postForEntity(url, entity, String.class).getBody();
if (!StringUtils.equals(result, "OK") && !StringUtils.equals(result, "NO")) { if (!StringUtils.equals(result, "OK") && !StringUtils.equals(result, "NO")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler(); result = RestUtil.getCause(restTemplate);
result = errorHandler.getCause();
} }
return result; return result;
...@@ -94,8 +94,7 @@ public class ServiceController { ...@@ -94,8 +94,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, null, String.class).getBody(); String result = restTemplate.postForEntity(url, null, String.class).getBody();
if (!StringUtils.equals(result, "OK") && !StringUtils.equals(result, "NO")) { if (!StringUtils.equals(result, "OK") && !StringUtils.equals(result, "NO")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler(); result = RestUtil.getCause(restTemplate);
result = errorHandler.getCause();
} }
return result; return result;
...@@ -119,7 +118,7 @@ public class ServiceController { ...@@ -119,7 +118,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, entity, String.class).getBody(); 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 { ...@@ -134,8 +133,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, entity, String.class).getBody(); String result = restTemplate.postForEntity(url, entity, String.class).getBody();
if (!StringUtils.equals(result, "OK")) { if (!StringUtils.equals(result, "OK")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler(); result = RestUtil.getCause(restTemplate);
result = errorHandler.getCause();
} }
return result; return result;
...@@ -146,7 +144,7 @@ public class ServiceController { ...@@ -146,7 +144,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, null, String.class).getBody(); 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 { ...@@ -156,8 +154,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, null, String.class).getBody(); String result = restTemplate.postForEntity(url, null, String.class).getBody();
if (!StringUtils.equals(result, "OK")) { if (!StringUtils.equals(result, "OK")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler(); result = RestUtil.getCause(restTemplate);
result = errorHandler.getCause();
} }
return result; return result;
...@@ -168,7 +165,7 @@ public class ServiceController { ...@@ -168,7 +165,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, version, String.class).getBody(); 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 { ...@@ -178,8 +175,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, version, String.class).getBody(); String result = restTemplate.postForEntity(url, version, String.class).getBody();
if (!StringUtils.equals(result, "OK")) { if (!StringUtils.equals(result, "OK")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler(); result = RestUtil.getCause(restTemplate);
result = errorHandler.getCause();
} }
return result; return result;
...@@ -190,7 +186,7 @@ public class ServiceController { ...@@ -190,7 +186,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, null, String.class).getBody(); 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 { ...@@ -200,8 +196,7 @@ public class ServiceController {
String result = restTemplate.postForEntity(url, null, String.class).getBody(); String result = restTemplate.postForEntity(url, null, String.class).getBody();
if (!StringUtils.equals(result, "OK")) { if (!StringUtils.equals(result, "OK")) {
ServiceErrorHandler errorHandler = (ServiceErrorHandler) restTemplate.getErrorHandler(); result = RestUtil.getCause(restTemplate);
result = errorHandler.getCause();
} }
return result; return result;
...@@ -209,11 +204,8 @@ public class ServiceController { ...@@ -209,11 +204,8 @@ public class ServiceController {
private static String getUrl() { private static String getUrl() {
String url = PropertiesContext.getProperties().getString("url"); String url = PropertiesContext.getProperties().getString("url");
if (!url.endsWith("/")) {
url += "/";
}
return url; return UrlUtil.formatUrl(url);
} }
private static String getUrl(Instance instance) { private static String getUrl(Instance instance) {
...@@ -225,15 +217,4 @@ public class ServiceController { ...@@ -225,15 +217,4 @@ public class ServiceController {
private static String getInvokeType(boolean async) { private static String getInvokeType(boolean async) {
return async ? "async" : "sync"; 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; ...@@ -16,8 +16,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.web.client.RestTemplate; 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.endpoint.ConsoleEndpoint;
import com.nepxion.discovery.console.handler.ConsoleErrorHandler;
@Configuration @Configuration
@Import(SwaggerConfiguration.class) @Import(SwaggerConfiguration.class)
...@@ -46,7 +46,7 @@ public class ConsoleAutoConfiguration { ...@@ -46,7 +46,7 @@ public class ConsoleAutoConfiguration {
@Bean @Bean
public RestTemplate consoleRestTemplate() { public RestTemplate consoleRestTemplate() {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new ConsoleErrorHandler()); restTemplate.setErrorHandler(new RestErrorHandler());
return restTemplate; return restTemplate;
} }
......
...@@ -24,8 +24,8 @@ import org.springframework.web.client.RestTemplate; ...@@ -24,8 +24,8 @@ import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.common.constant.DiscoveryConstant; import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.common.entity.ResultEntity; 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.common.util.UrlUtil;
import com.nepxion.discovery.console.handler.ConsoleErrorHandler;
public abstract class AbstractRestInvoker { public abstract class AbstractRestInvoker {
private static final Logger LOG = LoggerFactory.getLogger(AbstractRestInvoker.class); private static final Logger LOG = LoggerFactory.getLogger(AbstractRestInvoker.class);
...@@ -61,8 +61,7 @@ public abstract class AbstractRestInvoker { ...@@ -61,8 +61,7 @@ public abstract class AbstractRestInvoker {
result = doRest(url); result = doRest(url);
if (!StringUtils.equals(result, "OK")) { if (!StringUtils.equals(result, "OK")) {
ConsoleErrorHandler errorHandler = (ConsoleErrorHandler) restTemplate.getErrorHandler(); result = RestUtil.getCause(restTemplate);
result = errorHandler.getCause();
} }
} catch (Exception e) { } catch (Exception e) {
result = e.getMessage(); 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