|
@@ -0,0 +1,107 @@
|
|
|
+package com.dragonsoft.dcuc.approve.business.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.dragonsoft.dcuc.approve.business.INoticeBusiness;
|
|
|
+import com.dragonsoft.dcuc.approve.model.req.v3.ZeroTrustApproveCallbackReqV3VO;
|
|
|
+import com.dragonsoft.dcuc.approve.model.resp.v3.ZeroTrustMessageRespVO;
|
|
|
+import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
|
|
|
+import com.dragonsoft.duceap.commons.util.json.JsonUtils;
|
|
|
+import com.github.rholder.retry.*;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.RequestEntity;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.remoting.RemoteAccessException;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.net.URI;
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author hunagzqa
|
|
|
+ * @date 2022/11/16
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class NoticeBusinessImpl implements INoticeBusiness {
|
|
|
+
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setRestTemplate(RestTemplate restTemplate) {
|
|
|
+ this.restTemplate = restTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(NoticeBusinessImpl.class);
|
|
|
+
|
|
|
+ private static final Retryer<ResponseStatus> RESPONSE_STATUS_RETRYER =
|
|
|
+ RetryerBuilder.<ResponseStatus>newBuilder()
|
|
|
+ .retryIfException()
|
|
|
+ .retryIfResult(item -> !item.getStatusCode().equals(ResponseStatus.SUCCESS_CODE))
|
|
|
+ .withAttemptTimeLimiter(AttemptTimeLimiters.fixedTimeLimit(5, TimeUnit.SECONDS))
|
|
|
+ .withWaitStrategy(WaitStrategies.fixedWait(1, TimeUnit.SECONDS))
|
|
|
+ .withStopStrategy(StopStrategies.stopAfterAttempt(5))
|
|
|
+ .build();
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseStatus retryApproveNotification(String callbackUrl, ZeroTrustApproveCallbackReqV3VO zeroTrustApproveCallbackReqV3VO) {
|
|
|
+ StringBuilder errorMessageBuilder = new StringBuilder();
|
|
|
+
|
|
|
+ ResponseStatus responseStatus;
|
|
|
+ try {
|
|
|
+ responseStatus = RESPONSE_STATUS_RETRYER.call(() -> baseApproveNotification(callbackUrl, zeroTrustApproveCallbackReqV3VO));
|
|
|
+ } catch (ExecutionException | RetryException e) {
|
|
|
+ logger.error("retry error", e);
|
|
|
+ responseStatus = ResponseStatus.fail(e.getMessage());
|
|
|
+ }
|
|
|
+ if (!responseStatus.getStatusCode().equals(ResponseStatus.SUCCESS_CODE)) {
|
|
|
+ errorMessageBuilder.append("URL: ").append(callbackUrl)
|
|
|
+ .append(responseStatus.getMessage())
|
|
|
+ .append(";");
|
|
|
+ }
|
|
|
+ String errorMessage = errorMessageBuilder.toString();
|
|
|
+ if (StrUtil.isNotBlank(errorMessage)) {
|
|
|
+ return ResponseStatus.fail(errorMessage);
|
|
|
+ }
|
|
|
+ return ResponseStatus.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseStatus baseApproveNotification(String callbackUrl, ZeroTrustApproveCallbackReqV3VO zeroTrustApproveCallbackReqV3VO) {
|
|
|
+
|
|
|
+ String resultMessage;
|
|
|
+ ResponseStatus responseStatus = ResponseStatus.success();
|
|
|
+ try {
|
|
|
+ logger.info("打印状态变更通知请求:{} 内容:{}", callbackUrl, JsonUtils.toJSONString(zeroTrustApproveCallbackReqV3VO));
|
|
|
+
|
|
|
+ RequestEntity<ZeroTrustApproveCallbackReqV3VO> requestEntity = new RequestEntity<>(zeroTrustApproveCallbackReqV3VO,
|
|
|
+ HttpMethod.POST, URI.create(callbackUrl));
|
|
|
+ ResponseEntity<ZeroTrustMessageRespVO> responseEntity = restTemplate.exchange(requestEntity, ZeroTrustMessageRespVO.class);
|
|
|
+ logger.info("打印状态变更通知返回值:" + responseEntity);
|
|
|
+
|
|
|
+ if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
|
|
+ ZeroTrustMessageRespVO zeroTrustMessageRespVO = responseEntity.getBody();
|
|
|
+ if (zeroTrustMessageRespVO == null) {
|
|
|
+ logger.info("打印状态变更通知返回值:" + responseEntity);
|
|
|
+ throw new RemoteAccessException(responseEntity.toString());
|
|
|
+
|
|
|
+ } else if (zeroTrustMessageRespVO.isRespFail()) {
|
|
|
+ logger.info("打印状态变更通知返回值:" + responseEntity);
|
|
|
+ throw new RemoteAccessException(zeroTrustMessageRespVO.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("状态变更通知接口调用失败", e);
|
|
|
+ resultMessage = "状态变更通知接口调用失败:" + e.getMessage();
|
|
|
+ responseStatus = ResponseStatus.fail(resultMessage);
|
|
|
+ throw new RemoteAccessException(responseStatus.getMessage());
|
|
|
+ }
|
|
|
+ return responseStatus;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|