|
@@ -1,39 +1,30 @@
|
|
|
package cn.com.qmth.examcloud.web.cloud;
|
|
|
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Map.Entry;
|
|
|
-
|
|
|
-import cn.com.qmth.examcloud.web.config.SystemProperties;
|
|
|
-import org.apache.commons.collections.CollectionUtils;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.core.io.FileSystemResource;
|
|
|
-import org.springframework.http.HttpEntity;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.HttpMethod;
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
-import org.springframework.http.ResponseEntity;
|
|
|
-import org.springframework.util.LinkedMultiValueMap;
|
|
|
-import org.springframework.util.MultiValueMap;
|
|
|
-import org.springframework.web.client.RestTemplate;
|
|
|
-
|
|
|
import cn.com.qmth.examcloud.api.commons.exchange.BaseRequest;
|
|
|
import cn.com.qmth.examcloud.api.commons.exchange.FormFilePart;
|
|
|
import cn.com.qmth.examcloud.api.commons.exchange.FormRequest;
|
|
|
import cn.com.qmth.examcloud.api.commons.exchange.JsonSerializable;
|
|
|
import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
-import cn.com.qmth.examcloud.commons.util.ByteUtil;
|
|
|
-import cn.com.qmth.examcloud.commons.util.JsonUtil;
|
|
|
-import cn.com.qmth.examcloud.commons.util.SHA256;
|
|
|
-import cn.com.qmth.examcloud.commons.util.StringUtil;
|
|
|
-import cn.com.qmth.examcloud.commons.util.ThreadLocalUtil;
|
|
|
+import cn.com.qmth.examcloud.commons.util.*;
|
|
|
import cn.com.qmth.examcloud.web.config.LogProperties;
|
|
|
+import cn.com.qmth.examcloud.web.config.SystemProperties;
|
|
|
import cn.com.qmth.examcloud.web.exception.ApiFlowLimitedException;
|
|
|
import cn.com.qmth.examcloud.web.support.SpringContextHolder;
|
|
|
import cn.com.qmth.examcloud.web.support.StatusResponse;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.core.io.FileSystemResource;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.*;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
|
|
|
/**
|
|
|
* 云服务客户端基类
|
|
@@ -44,279 +35,286 @@ import cn.com.qmth.examcloud.web.support.StatusResponse;
|
|
|
*/
|
|
|
public abstract class CloudClientSupport {
|
|
|
|
|
|
- protected static final Logger LOGGER = LoggerFactory.getLogger(CloudClientSupport.class);
|
|
|
-
|
|
|
- private RestTemplate restTemplate;
|
|
|
-
|
|
|
- private LogProperties logProperties;
|
|
|
-
|
|
|
- private static String[] excludeFields = new String[]{"password", ".*Password"};
|
|
|
-
|
|
|
- private LogProperties getLogProperties() {
|
|
|
- if (null == logProperties) {
|
|
|
- logProperties = SpringContextHolder.getBean(LogProperties.class);
|
|
|
- }
|
|
|
- return logProperties;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取请求映射前缀
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @return
|
|
|
- */
|
|
|
- protected abstract String getRequestMappingPrefix();
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取 RestTemplate
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @return
|
|
|
- */
|
|
|
- private RestTemplate getRestTemplate() {
|
|
|
- if (null == restTemplate) {
|
|
|
- restTemplate = SpringContextHolder.getBean(RestTemplate.class);
|
|
|
- }
|
|
|
- return restTemplate;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 构建url
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param appName
|
|
|
- * @param requestMapping
|
|
|
- * @return
|
|
|
- */
|
|
|
- private String buildUrl(String appName, String requestMapping) {
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- sb.append("http://");
|
|
|
- String redirection = CloudServiceRedirector.getRedirection(appName);
|
|
|
- if (null != redirection) {
|
|
|
- sb.append(redirection);
|
|
|
- } else {
|
|
|
- sb.append(appName);
|
|
|
- }
|
|
|
- String rmp = getRequestMappingPrefix();
|
|
|
- if (rmp.startsWith("/")) {
|
|
|
- sb.append(rmp);
|
|
|
- } else {
|
|
|
- sb.append("/").append(rmp);
|
|
|
- }
|
|
|
- if ('/' == sb.charAt(sb.length() - 1) && requestMapping.endsWith("/")) {
|
|
|
- sb.deleteCharAt(sb.length() - 1).append(requestMapping);
|
|
|
- } else if ('/' != sb.charAt(sb.length() - 1) && !requestMapping.endsWith("/")) {
|
|
|
- sb.append("/").append(requestMapping);
|
|
|
- } else {
|
|
|
- sb.append(requestMapping);
|
|
|
- }
|
|
|
-
|
|
|
- return sb.toString();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取响应体
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param respEntity
|
|
|
- * @return
|
|
|
- */
|
|
|
- private <T> T getRespBody(ResponseEntity<String> respEntity, Class<T> responseType) {
|
|
|
-
|
|
|
- String body = respEntity.getBody();
|
|
|
- if (HttpStatus.OK == respEntity.getStatusCode()) {
|
|
|
- if (null == responseType) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- T t = JsonUtil.fromJson(body, responseType);
|
|
|
- return t;
|
|
|
- } else if (HttpStatus.INTERNAL_SERVER_ERROR == respEntity.getStatusCode()) {
|
|
|
- StatusResponse sr = JsonUtil.fromJson(body, StatusResponse.class);
|
|
|
- throw new StatusException(sr.getCode(), sr.getDesc());
|
|
|
- } else if (HttpStatus.SERVICE_UNAVAILABLE == respEntity.getStatusCode()) {
|
|
|
- StatusResponse sr = JsonUtil.fromJson(body, StatusResponse.class);
|
|
|
- throw new ApiFlowLimitedException(sr.getCode(), sr.getDesc());
|
|
|
- } else {
|
|
|
- throw new ExamCloudRuntimeException(
|
|
|
- "unexpected http status code [" + respEntity.getStatusCode() + "]");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * exchange
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param url
|
|
|
- * @param method
|
|
|
- * @param body
|
|
|
- * @param responseType
|
|
|
- * @return
|
|
|
- */
|
|
|
- protected <T> T exchange(String url, HttpMethod method, Object body, Class<T> responseType) {
|
|
|
- return exchange(url, method, null, body, responseType);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * exchange
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param url
|
|
|
- * @param method
|
|
|
- * @param httpHeaders
|
|
|
- * @param body
|
|
|
- * @param responseType
|
|
|
- * @return
|
|
|
- */
|
|
|
- protected <T> T exchange(String url, HttpMethod method, HttpHeaders httpHeaders, Object body,
|
|
|
- Class<T> responseType) {
|
|
|
-
|
|
|
- long startTime = System.currentTimeMillis();
|
|
|
-
|
|
|
- if (null == httpHeaders) {
|
|
|
- httpHeaders = new HttpHeaders();
|
|
|
- }
|
|
|
-
|
|
|
- httpHeaders.add("Trace-Id", ThreadLocalUtil.getTraceId());
|
|
|
- httpHeaders.add("timestamp", String.valueOf(startTime));
|
|
|
- httpHeaders.add("App-Id", String.valueOf(SystemProperties.APP_ID));
|
|
|
- httpHeaders.add("App-Code", String.valueOf(SystemProperties.APP_CODE));
|
|
|
-
|
|
|
- String joinStr = StringUtil.join(
|
|
|
- SystemProperties.APP_ID,
|
|
|
- SystemProperties.APP_CODE,
|
|
|
- startTime,
|
|
|
- SystemProperties.APP_SECRET_KEY);
|
|
|
-
|
|
|
- byte[] bytes = SHA256.encode(joinStr);
|
|
|
- String accessToken = ByteUtil.toHexAscii(bytes);
|
|
|
- httpHeaders.add("Access-Token", accessToken);
|
|
|
-
|
|
|
- if (LOGGER.isInfoEnabled()) {
|
|
|
- LOGGER.info("[CALL-IN]. url=" + url);
|
|
|
- if (null == body) {
|
|
|
- LOGGER.info("[CALL-REQ]. request= void");
|
|
|
- } else if (body instanceof JsonSerializable) {
|
|
|
- LOGGER.info("[CALL-REQ]. request=" + JsonUtil.toJson(body, excludeFields));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- HttpEntity<Object> requestEntity = null;
|
|
|
- if (null == body) {
|
|
|
- requestEntity = new HttpEntity<Object>(httpHeaders);
|
|
|
- } else {
|
|
|
- requestEntity = new HttpEntity<Object>(body, httpHeaders);
|
|
|
- }
|
|
|
-
|
|
|
- T respBody = null;
|
|
|
- try {
|
|
|
- ResponseEntity<String> respEntity = getRestTemplate().exchange(url, method,
|
|
|
- requestEntity, String.class);
|
|
|
- respBody = getRespBody(respEntity, responseType);
|
|
|
-
|
|
|
- if (LOGGER.isDebugEnabled() && getLogProperties().isNormalResponseLogEnable()) {
|
|
|
- String respEntityBody = respEntity.getBody();
|
|
|
- int responseJsonMaxSize = getLogProperties().getResponseLogJsonMaxSize();
|
|
|
- if (null == respEntityBody) {
|
|
|
- LOGGER.debug("[CALL-RESP]. response= void");
|
|
|
- } else if (respEntityBody.length() > responseJsonMaxSize) {
|
|
|
- LOGGER.debug("[CALL-RESP]. response= too large");
|
|
|
- } else {
|
|
|
- LOGGER.debug("[CALL-RESP]. response=" + respEntityBody);
|
|
|
- }
|
|
|
- }
|
|
|
- if (LOGGER.isInfoEnabled()) {
|
|
|
- LOGGER.info(StringUtil.join("[CALL-OK]. url=" + url,
|
|
|
- " ; cost " + (System.currentTimeMillis() - startTime), " ms."));
|
|
|
- }
|
|
|
- } catch (ApiFlowLimitedException e) {
|
|
|
- LOGGER.error(StringUtil.join("[CALL-FAIL]. url=" + url,
|
|
|
- " ; cost " + (System.currentTimeMillis() - startTime), " ms."));
|
|
|
- LOGGER.error("[CALL-RESP]. response=" + e.toJson());
|
|
|
- throw e;
|
|
|
- } catch (StatusException e) {
|
|
|
- LOGGER.error(StringUtil.join("[CALL-FAIL]. url=" + url,
|
|
|
- " ; cost " + (System.currentTimeMillis() - startTime), " ms."));
|
|
|
- LOGGER.error("[CALL-RESP]. response=" + e.toJson());
|
|
|
- throw e;
|
|
|
- } catch (Exception e) {
|
|
|
- LOGGER.error(StringUtil.join("[CALL-FATAL]. url=" + url, " ;", e.getMessage()));
|
|
|
- throw e;
|
|
|
- }
|
|
|
-
|
|
|
- return respBody;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * post请求
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param requestMapping
|
|
|
- * @param body
|
|
|
- * @param responseType
|
|
|
- * @return
|
|
|
- */
|
|
|
- protected <T> T post(String appName, String requestMapping, BaseRequest body,
|
|
|
- Class<T> responseType) {
|
|
|
- String url = buildUrl(appName, requestMapping);
|
|
|
- return exchange(url, HttpMethod.POST, body, responseType);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * post请求
|
|
|
- *
|
|
|
- * @param requestMapping
|
|
|
- * @param body
|
|
|
- */
|
|
|
- protected void post(String appName, String requestMapping, BaseRequest body) {
|
|
|
- String url = buildUrl(appName, requestMapping);
|
|
|
- exchange(url, HttpMethod.POST, body, null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * post请求
|
|
|
- *
|
|
|
- * @param requestMapping
|
|
|
- */
|
|
|
- protected void post(String appName, String requestMapping) {
|
|
|
- String url = buildUrl(appName, requestMapping);
|
|
|
- exchange(url, HttpMethod.POST, null, null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 文件表单提交
|
|
|
- *
|
|
|
- * @author WANGWEI
|
|
|
- * @param requestMapping
|
|
|
- * @param req
|
|
|
- * @param responseType
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public <T> T postForm(String appName, String requestMapping, FormRequest req,
|
|
|
- Class<T> responseType) {
|
|
|
- String url = buildUrl(appName, requestMapping);
|
|
|
-
|
|
|
- MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
|
|
|
- HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
- httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
-
|
|
|
- List<FormFilePart> formFilePartList = req.getFormFilePartList();
|
|
|
- if (CollectionUtils.isNotEmpty(formFilePartList)) {
|
|
|
- for (FormFilePart part : formFilePartList) {
|
|
|
- FileSystemResource resource = new CustomFileSystemResource(part.getFile(),
|
|
|
- part.getFilename());
|
|
|
- params.add(part.getParamName(), resource);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- String json = JsonUtil.toJson(req);
|
|
|
- Map<String, String> otherParams = JsonUtil.json2Map(json);
|
|
|
-
|
|
|
- for (Entry<String, String> entry : otherParams.entrySet()) {
|
|
|
- params.add(entry.getKey(), entry.getValue());
|
|
|
- }
|
|
|
-
|
|
|
- return exchange(url, HttpMethod.POST, httpHeaders, params, responseType);
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+ protected static final Logger LOGGER = LoggerFactory.getLogger(CloudClientSupport.class);
|
|
|
+
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ private LogProperties logProperties;
|
|
|
+
|
|
|
+ private static String[] excludeFields = new String[]{"password", ".*Password"};
|
|
|
+
|
|
|
+ private LogProperties getLogProperties() {
|
|
|
+ if (null == logProperties) {
|
|
|
+ logProperties = SpringContextHolder.getBean(LogProperties.class);
|
|
|
+ }
|
|
|
+ return logProperties;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取请求映射前缀
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ protected abstract String getRequestMappingPrefix();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取 RestTemplate
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ private RestTemplate getRestTemplate() {
|
|
|
+ if (null == restTemplate) {
|
|
|
+ restTemplate = SpringContextHolder.getBean(RestTemplate.class);
|
|
|
+ }
|
|
|
+ return restTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建url
|
|
|
+ *
|
|
|
+ * @param appName
|
|
|
+ * @param requestMapping
|
|
|
+ * @return
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ private String buildUrl(String appName, String requestMapping) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("http://");
|
|
|
+ String redirection = CloudServiceRedirector.getRedirection(appName);
|
|
|
+ if (null != redirection) {
|
|
|
+ sb.append(redirection);
|
|
|
+ } else {
|
|
|
+ sb.append(appName);
|
|
|
+ }
|
|
|
+ String rmp = getRequestMappingPrefix();
|
|
|
+ if (rmp.startsWith("/")) {
|
|
|
+ sb.append(rmp);
|
|
|
+ } else {
|
|
|
+ sb.append("/").append(rmp);
|
|
|
+ }
|
|
|
+ if ('/' == sb.charAt(sb.length() - 1) && requestMapping.endsWith("/")) {
|
|
|
+ sb.deleteCharAt(sb.length() - 1).append(requestMapping);
|
|
|
+ } else if ('/' != sb.charAt(sb.length() - 1) && !requestMapping.endsWith("/")) {
|
|
|
+ sb.append("/").append(requestMapping);
|
|
|
+ } else {
|
|
|
+ sb.append(requestMapping);
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取响应体
|
|
|
+ *
|
|
|
+ * @param respEntity
|
|
|
+ * @return
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ private <T> T getRespBody(ResponseEntity<String> respEntity, Class<T> responseType) {
|
|
|
+
|
|
|
+ String body = respEntity.getBody();
|
|
|
+ if (HttpStatus.OK == respEntity.getStatusCode()) {
|
|
|
+ if (null == responseType) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ T t = JsonUtil.fromJson(body, responseType);
|
|
|
+ return t;
|
|
|
+ } else if (HttpStatus.INTERNAL_SERVER_ERROR == respEntity.getStatusCode()) {
|
|
|
+ StatusResponse sr = JsonUtil.fromJson(body, StatusResponse.class);
|
|
|
+ throw new StatusException(sr.getCode(), sr.getDesc());
|
|
|
+ } else if (HttpStatus.SERVICE_UNAVAILABLE == respEntity.getStatusCode()) {
|
|
|
+ StatusResponse sr = JsonUtil.fromJson(body, StatusResponse.class);
|
|
|
+ throw new ApiFlowLimitedException(sr.getCode(), sr.getDesc());
|
|
|
+ } else {
|
|
|
+ throw new ExamCloudRuntimeException("unexpected http status code [" + respEntity.getStatusCode() + "]");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * exchange
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param method
|
|
|
+ * @param body
|
|
|
+ * @param responseType
|
|
|
+ * @return
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ protected <T> T exchange(String url, HttpMethod method, Object body, Class<T> responseType) {
|
|
|
+ return exchange(url, method, null, body, responseType);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * exchange
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param method
|
|
|
+ * @param httpHeaders
|
|
|
+ * @param body
|
|
|
+ * @param responseType
|
|
|
+ * @return
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ protected <T> T exchange(String url, HttpMethod method, HttpHeaders httpHeaders, Object body, Class<T> responseType) {
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+
|
|
|
+ if (null == httpHeaders) {
|
|
|
+ httpHeaders = new HttpHeaders();
|
|
|
+ }
|
|
|
+
|
|
|
+ httpHeaders.add("Trace-Id", ThreadLocalUtil.getTraceId());
|
|
|
+ httpHeaders.add("timestamp", String.valueOf(startTime));
|
|
|
+ httpHeaders.add("App-Id", String.valueOf(SystemProperties.APP_ID));
|
|
|
+ httpHeaders.add("App-Code", String.valueOf(SystemProperties.APP_CODE));
|
|
|
+
|
|
|
+ String joinStr = StringUtil.join(
|
|
|
+ SystemProperties.APP_ID,
|
|
|
+ SystemProperties.APP_CODE,
|
|
|
+ startTime,
|
|
|
+ SystemProperties.APP_SECRET_KEY);
|
|
|
+
|
|
|
+ byte[] bytes = SHA256.encode(joinStr);
|
|
|
+ String accessToken = ByteUtil.toHexAscii(bytes);
|
|
|
+ httpHeaders.add("Access-Token", accessToken);
|
|
|
+
|
|
|
+ int responseJsonMaxSize = getLogProperties().getResponseLogJsonMaxSize();
|
|
|
+ if (LOGGER.isInfoEnabled() && getLogProperties().isNormalResponseLogEnable()) {
|
|
|
+ if (null == body) {
|
|
|
+ LOGGER.info("[RPC-request] - {}", url);
|
|
|
+ } else if (body instanceof JsonSerializable) {
|
|
|
+ String json = JsonUtil.toJson(body, excludeFields);
|
|
|
+ if (json == null) {
|
|
|
+ LOGGER.info("[RPC-request] - {}", url);
|
|
|
+ } else {
|
|
|
+ LOGGER.info("[RPC-request] - {} params: {}", url, this.cutStr(json, responseJsonMaxSize));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpEntity<Object> requestEntity;
|
|
|
+ if (null == body) {
|
|
|
+ requestEntity = new HttpEntity<>(httpHeaders);
|
|
|
+ } else {
|
|
|
+ requestEntity = new HttpEntity<>(body, httpHeaders);
|
|
|
+ }
|
|
|
+
|
|
|
+ T respBody = null;
|
|
|
+ try {
|
|
|
+ ResponseEntity<String> respEntity = getRestTemplate().exchange(url, method, requestEntity, String.class);
|
|
|
+ respBody = getRespBody(respEntity, responseType);
|
|
|
+
|
|
|
+ if (LOGGER.isDebugEnabled() && getLogProperties().isNormalResponseLogEnable()) {
|
|
|
+ String respEntityBody = respEntity.getBody();
|
|
|
+ if (null == respEntityBody) {
|
|
|
+ LOGGER.debug("[RPC-response] - void");
|
|
|
+ } else {
|
|
|
+ LOGGER.debug("[RPC-response] - {}", this.cutStr(respEntityBody, responseJsonMaxSize));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (LOGGER.isInfoEnabled()) {
|
|
|
+ LOGGER.info("[RPC-ok] - {}, cost {} ms", url, System.currentTimeMillis() - startTime);
|
|
|
+ }
|
|
|
+ } catch (ApiFlowLimitedException e) {
|
|
|
+ LOGGER.error("[RPC-fail] - {}, cost {} ms, cause1 = {}", url, System.currentTimeMillis() - startTime, e.toJson());
|
|
|
+ throw e;
|
|
|
+ } catch (StatusException e) {
|
|
|
+ LOGGER.error("[RPC-fail] - {}, cost {} ms, cause2 = {}", url, System.currentTimeMillis() - startTime, e.toJson());
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ LOGGER.error("[RPC-fail] - {}, cost {} ms, cause3 = {}", url, System.currentTimeMillis() - startTime, e.getMessage());
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+
|
|
|
+ return respBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String cutStr(String str, int maxLength) {
|
|
|
+ if (str == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (str.length() > maxLength) {
|
|
|
+ // content too large...
|
|
|
+ return str.substring(0, maxLength) + "......";
|
|
|
+ }
|
|
|
+
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post请求
|
|
|
+ *
|
|
|
+ * @param requestMapping
|
|
|
+ * @param body
|
|
|
+ * @param responseType
|
|
|
+ * @return
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ protected <T> T post(String appName, String requestMapping, BaseRequest body,
|
|
|
+ Class<T> responseType) {
|
|
|
+ String url = buildUrl(appName, requestMapping);
|
|
|
+ return exchange(url, HttpMethod.POST, body, responseType);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post请求
|
|
|
+ *
|
|
|
+ * @param requestMapping
|
|
|
+ * @param body
|
|
|
+ */
|
|
|
+ protected void post(String appName, String requestMapping, BaseRequest body) {
|
|
|
+ String url = buildUrl(appName, requestMapping);
|
|
|
+ exchange(url, HttpMethod.POST, body, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post请求
|
|
|
+ *
|
|
|
+ * @param requestMapping
|
|
|
+ */
|
|
|
+ protected void post(String appName, String requestMapping) {
|
|
|
+ String url = buildUrl(appName, requestMapping);
|
|
|
+ exchange(url, HttpMethod.POST, null, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件表单提交
|
|
|
+ *
|
|
|
+ * @param requestMapping
|
|
|
+ * @param req
|
|
|
+ * @param responseType
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ public <T> T postForm(String appName, String requestMapping, FormRequest req,
|
|
|
+ Class<T> responseType) {
|
|
|
+ String url = buildUrl(appName, requestMapping);
|
|
|
+
|
|
|
+ MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
|
|
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
+ httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
+
|
|
|
+ List<FormFilePart> formFilePartList = req.getFormFilePartList();
|
|
|
+ if (CollectionUtils.isNotEmpty(formFilePartList)) {
|
|
|
+ for (FormFilePart part : formFilePartList) {
|
|
|
+ FileSystemResource resource = new CustomFileSystemResource(part.getFile(),
|
|
|
+ part.getFilename());
|
|
|
+ params.add(part.getParamName(), resource);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String json = JsonUtil.toJson(req);
|
|
|
+ Map<String, String> otherParams = JsonUtil.json2Map(json);
|
|
|
+
|
|
|
+ for (Entry<String, String> entry : otherParams.entrySet()) {
|
|
|
+ params.add(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ return exchange(url, HttpMethod.POST, httpHeaders, params, responseType);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|