|
@@ -2,13 +2,25 @@ package cn.com.qmth.examcloud.app.service;
|
|
|
|
|
|
import cn.com.qmth.examcloud.app.core.SysProperty;
|
|
import cn.com.qmth.examcloud.app.core.SysProperty;
|
|
import cn.com.qmth.examcloud.app.core.router.Router;
|
|
import cn.com.qmth.examcloud.app.core.router.Router;
|
|
|
|
+import cn.com.qmth.examcloud.app.model.Constants;
|
|
import cn.com.qmth.examcloud.app.model.Result;
|
|
import cn.com.qmth.examcloud.app.model.Result;
|
|
|
|
+import okhttp3.*;
|
|
|
|
+import org.apache.commons.collections.MapUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.UUID;
|
|
|
|
+
|
|
|
|
+import static cn.com.qmth.examcloud.app.model.Constants.*;
|
|
|
|
+
|
|
@Component
|
|
@Component
|
|
public class RouterService {
|
|
public class RouterService {
|
|
private static Logger log = LoggerFactory.getLogger(RouterService.class);
|
|
private static Logger log = LoggerFactory.getLogger(RouterService.class);
|
|
@@ -16,14 +28,91 @@ public class RouterService {
|
|
private SysProperty sysProperty;
|
|
private SysProperty sysProperty;
|
|
|
|
|
|
public Result execute(Router router) {
|
|
public Result execute(Router router) {
|
|
|
|
+ //校验参数
|
|
Assert.notNull(router, "参数“router”不能为空!");
|
|
Assert.notNull(router, "参数“router”不能为空!");
|
|
Assert.notNull(router.getServer(), "参数“server”不能为空!");
|
|
Assert.notNull(router.getServer(), "参数“server”不能为空!");
|
|
Assert.hasLength(router.getUrl(), "参数“url”不能为空!");
|
|
Assert.hasLength(router.getUrl(), "参数“url”不能为空!");
|
|
Assert.notNull(router.getMethod(), "参数“method”不能为空!");
|
|
Assert.notNull(router.getMethod(), "参数“method”不能为空!");
|
|
- log.info(String.format("Router:[%s][%s]-%s ", router.getServer().getInstanceName(), router.getMethod().name(), router.getUrl()));
|
|
|
|
|
|
|
|
|
|
+ //处理请求Headers
|
|
|
|
+ Map<String, String> requestHeaders;
|
|
|
|
+ if (MapUtils.isEmpty(router.getHeaders())) {
|
|
|
|
+ requestHeaders = new HashMap<>();
|
|
|
|
+ } else {
|
|
|
|
+ requestHeaders = router.getHeaders();
|
|
|
|
+ }
|
|
|
|
+ requestHeaders.put(PARAM_KEY, router.getKey());
|
|
|
|
+ requestHeaders.put(PARAM_TOKEN, router.getToken());
|
|
|
|
+ requestHeaders.put(PARAM_TRACE_ID, this.getRandomTraceId());
|
|
|
|
+ requestHeaders.put(PARAM_CLIENT, PARAM_CLIENT_VALUE);
|
|
|
|
+
|
|
|
|
+ //处理请求Params
|
|
|
|
+ String requestParams = this.appendParams(router.getParams());
|
|
|
|
+
|
|
|
|
+ //处理请求Body
|
|
|
|
+ RequestBody requestBody = this.appendBody(router.getBody());
|
|
|
|
+
|
|
|
|
+ //处理请求地址
|
|
|
|
+ final String requestUrl = sysProperty.getProxyUrl(router.getServer()) + "/" + router.getUrl() + "?" + requestParams;
|
|
|
|
+ log.info(String.format("Router:[%s][%s]-%s ", router.getServer().getInstanceName(), router.getMethod().name(), requestUrl));
|
|
|
|
|
|
|
|
+ //封装请求
|
|
|
|
+ Request.Builder request = new Request.Builder()
|
|
|
|
+ .url(requestUrl)
|
|
|
|
+ .headers(Headers.of(requestHeaders));
|
|
|
|
+
|
|
|
|
+ switch (router.getMethod()) {
|
|
|
|
+ case GET:
|
|
|
|
+ return this.call(request.get().build());
|
|
|
|
+ case POST:
|
|
|
|
+ return this.call(request.post(requestBody).build());
|
|
|
|
+ case PUT:
|
|
|
|
+ return this.call(request.put(requestBody).build());
|
|
|
|
+ case DELETE:
|
|
|
|
+ return this.call(request.delete(requestBody).build());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return new Result().error();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Result call(Request request) {
|
|
|
|
+ //todo
|
|
return new Result().success();
|
|
return new Result().success();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private RequestBody appendBody(String body) {
|
|
|
|
+ if (StringUtils.isBlank(body)) {
|
|
|
|
+ body = "{}";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return FormBody.create(MediaType.parse(Constants.CHARSET_JSON_UTF8), body);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String appendParams(Map<String, String> params) {
|
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ if (MapUtils.isNotEmpty(params)) {
|
|
|
|
+ for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
|
+ result.append(entry.getKey()).append("=").append(strEncode(entry.getValue())).append("&");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String strEncode(String str) {
|
|
|
|
+ try {
|
|
|
|
+ if (StringUtils.isNotBlank(str)) {
|
|
|
|
+ return URLEncoder.encode(str, "UTF-8");
|
|
|
|
+ }
|
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
|
+ //ignore
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getRandomTraceId() {
|
|
|
|
+ return UUID.randomUUID().toString().replace("-", "");
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|