From 8d0f1faae8ea90896fa4ee53b5d912248a9a66f3 Mon Sep 17 00:00:00 2001 From: etreschenkov Date: Fri, 16 Jun 2023 15:20:32 +0300 Subject: [PATCH] http://jira.mfd.msk:8088/browse/CLS-357 --- ...boundRequest.java => OutboundRequest.java} | 2 +- .../gatewayapi/service/GatewayService.java | 114 +++++++++--------- .../service/OutboundRequestBuilder.java | 46 +++++++ .../service/OutboundRequestType.java | 27 +++++ 4 files changed, 130 insertions(+), 59 deletions(-) rename clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/request/{InboundRequest.java => OutboundRequest.java} (99%) create mode 100644 clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/OutboundRequestBuilder.java create mode 100644 clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/OutboundRequestType.java diff --git a/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/request/InboundRequest.java b/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/request/OutboundRequest.java similarity index 99% rename from clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/request/InboundRequest.java rename to clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/request/OutboundRequest.java index 288123707..b16c1bbf2 100644 --- a/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/request/InboundRequest.java +++ b/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/request/OutboundRequest.java @@ -11,7 +11,7 @@ import java.time.Instant; import java.util.Map; import java.util.UUID; -public class InboundRequest { +public class OutboundRequest { @JsonProperty("id") @ApiModelProperty( value = """ diff --git a/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/GatewayService.java b/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/GatewayService.java index 000cbf094..421bde173 100644 --- a/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/GatewayService.java +++ b/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/GatewayService.java @@ -11,7 +11,7 @@ import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import ru.spcex.clearing.gatewayapi.config.GatewayApiSettings; import ru.spcex.clearing.gatewayapi.config.InboundServerSettings; -import ru.spcex.clearing.gatewayapi.request.InboundRequest; +import ru.spcex.clearing.gatewayapi.request.OutboundRequest; import ru.spcex.clearing.platform.messaging.domain.BaseRequest; import ru.spcex.clearing.platform.messaging.domain.Consts; import ru.spcex.clearing.platform.messaging.domain.cud.clearing.AssetOperationRequest; @@ -23,11 +23,8 @@ import ru.spcex.clearing.util.security.UserRoleVerification; import ru.spcex.platform.enumeration.Section; import ru.spcex.platform.enumeration.Task; -import java.time.Instant; import java.util.Collections; -import java.util.HashMap; import java.util.Map; -import java.util.UUID; @Service public class GatewayService extends QueueConsumer implements InitializingBean { @@ -74,14 +71,11 @@ public class GatewayService extends QueueConsumer implements InitializingBean { logUnknownProperties(userRequest); String url = formingInboundUrl(inboundServerSettings.getPathLOCM()); - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON); - httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - InboundRequest inboundRequest = new InboundRequest(); - inboundRequest.setId(UUID.randomUUID()); - inboundRequest.setType("MEMBER_ON_DEMAND"); - inboundRequest.setDatetime(Instant.now()); - HttpEntity request = new HttpEntity<>(inboundRequest, httpHeaders); + + OutboundRequest outboundFondRequest = OutboundRequestBuilder.builder() + .type(OutboundRequestType.MEMBER_ON_DEMAND) + .build(); + HttpEntity request = makeDefaultRequest(outboundFondRequest); ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, request, Map.class); Map bodyResponse = response.getBody(); @@ -91,20 +85,16 @@ public class GatewayService extends QueueConsumer implements InitializingBean { public void requestOnLimit(BaseRequest userRequest) { LimExportedRequest exportedRequest = userRequest.getRequestPayload(); String url = formingInboundUrl(inboundServerSettings.getPathLOCM()); - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON); - httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - InboundRequest inboundRequest = new InboundRequest(); - inboundRequest.setId(UUID.randomUUID()); - inboundRequest.setType("FILL_LIMITS"); - inboundRequest.setSection(exportedRequest.getSection()); - inboundRequest.setDatetime(Instant.now()); - Map content = new HashMap<>(); - content.put("file", exportedRequest.getLimFileName()); - inboundRequest.setContent(content); - HttpEntity request = new HttpEntity<>(inboundRequest, httpHeaders); + Map content = Map.of("file", exportedRequest.getLimFileName()); + OutboundRequest outboundFondRequest = OutboundRequestBuilder.builder() + .section(exportedRequest.getSection()) + .type(OutboundRequestType.FILL_LIMITS) + .content(content) + .build(); + + HttpEntity request = makeDefaultRequest(outboundFondRequest); try { ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, request, Map.class); Map bodyResponse = response.getBody(); @@ -124,49 +114,51 @@ public class GatewayService extends QueueConsumer implements InitializingBean { logUnknownProperties(userRequest); String url = formingInboundUrl(inboundServerSettings.getPathLOCM()); - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON); - httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - InboundRequest inboundRequest = new InboundRequest(); - inboundRequest.setId(UUID.randomUUID()); - inboundRequest.setType("ON_DEMAND"); - inboundRequest.setSection(Section.FOND.getKey()); - inboundRequest.setDatetime(Instant.now()); - HttpEntity request = new HttpEntity<>(inboundRequest, httpHeaders); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, request, Map.class); + OutboundRequest outboundFondRequest = OutboundRequestBuilder.builder() + .section(Section.FOND.getKey()) + .type(OutboundRequestType.ON_DEMAND) + .build(); + + OutboundRequest outboundMkrRequest = OutboundRequestBuilder.builder() + .section(Section.MKR.getKey()) + .type(OutboundRequestType.ON_DEMAND) + .build(); + + HttpEntity requestFond = makeDefaultRequest(outboundFondRequest); + HttpEntity requestMKR = makeDefaultRequest(outboundMkrRequest); + + ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, requestFond, Map.class); Map bodyResponse = response.getBody(); log.debug("LOSC task 1/2 complete (FOND section), response: {}", bodyResponse); - - inboundRequest.setId(UUID.randomUUID()); - inboundRequest.setSection(Section.MKR.getKey()); - request = new HttpEntity<>(inboundRequest, httpHeaders); - response = restTemplate.exchange(url, HttpMethod.POST, request, Map.class); + response = restTemplate.exchange(url, HttpMethod.POST, requestMKR, Map.class); bodyResponse = response.getBody(); log.debug("LOSC task 2/2 complete (MKR section), response: {}", bodyResponse); } public void requestOnAsset(BaseRequest request) { - log.debug("LOCM task received"); + RequestInfoUpdate requestInfoUpdate = userRoleVerification.validateRoleAndGetResult(request); + if (requestInfoUpdate != null) return; -// RequestInfoUpdate requestInfoUpdate = userRoleVerification.validateRoleAndGetResult(userRequest); -// if (requestInfoUpdate != null) return; -// -// logUnknownProperties(userRequest); -// -// String url = formingInboundUrl(inboundServerSettings.getPathLOCM()); -// HttpHeaders httpHeaders = new HttpHeaders(); -// httpHeaders.setContentType(MediaType.APPLICATION_JSON); -// httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); -// InboundRequest inboundRequest = new InboundRequest(); -// inboundRequest.setId(UUID.randomUUID()); -// inboundRequest.setType("MEMBER_ON_DEMAND"); -// inboundRequest.setDatetime(Instant.now()); -// HttpEntity request = new HttpEntity<>(inboundRequest, httpHeaders); -// -// ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, request, Map.class); -// Map bodyResponse = response.getBody(); -// log.debug("LOCM task complete, response: {}", bodyResponse); + AssetOperationRequest assetOperationRequest = request.getRequestPayload(); + Map content = Map.of("direction", "IN", + "amount", assetOperationRequest.getAmount(), + "quantity", assetOperationRequest.getQuantity(), + "code", assetOperationRequest.getCode(), + "asset", assetOperationRequest.getAsset(), + "firm_id", assetOperationRequest.getFirmId()); + + OutboundRequest outboundRequest = OutboundRequestBuilder.builder() + .section(Section.MKR.getKey()) + .type(OutboundRequestType.ASSET_OPERATION) + .content(content).build(); + + String url = formingInboundUrl(inboundServerSettings.getPathLOCM()); + + HttpEntity r = makeDefaultRequest(outboundRequest); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, r, Map.class); + Map bodyResponse = response.getBody(); + log.debug("Response from service: {}", bodyResponse); } private String formingInboundUrl(String path) { @@ -189,4 +181,10 @@ public class GatewayService extends QueueConsumer implements InitializingBean { } } + private HttpEntity makeDefaultRequest(OutboundRequest outboundRequest) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + return new HttpEntity<>(outboundRequest, httpHeaders); + } } diff --git a/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/OutboundRequestBuilder.java b/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/OutboundRequestBuilder.java new file mode 100644 index 000000000..3dc13f35a --- /dev/null +++ b/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/OutboundRequestBuilder.java @@ -0,0 +1,46 @@ +package ru.spcex.clearing.gatewayapi.service; + +import ru.spcex.clearing.gatewayapi.request.OutboundRequest; + +import java.time.Instant; +import java.util.Map; +import java.util.UUID; + +public class OutboundRequestBuilder { + + private String section; + private OutboundRequestType requestType; + private Map content; + + private OutboundRequestBuilder() { + } + + public static OutboundRequestBuilder builder() { + return new OutboundRequestBuilder(); + } + + public OutboundRequestBuilder section(String section) { + this.section = section; + return this; + } + + public OutboundRequestBuilder type(OutboundRequestType outboundRequestType) { + this.section = section; + return this; + } + + public OutboundRequestBuilder content(Map content) { + this.content = content; + return this; + } + + public OutboundRequest build() { + OutboundRequest outboundRequest = new OutboundRequest(); + outboundRequest.setId(UUID.randomUUID()); + outboundRequest.setDatetime(Instant.now()); + outboundRequest.setSection(section); + outboundRequest.setType(requestType.getKey()); + outboundRequest.setContent(content); + return outboundRequest; + } +} diff --git a/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/OutboundRequestType.java b/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/OutboundRequestType.java new file mode 100644 index 000000000..e78cfee17 --- /dev/null +++ b/clearing-parent/gateway-api/src/main/java/ru/spcex/clearing/gatewayapi/service/OutboundRequestType.java @@ -0,0 +1,27 @@ +package ru.spcex.clearing.gatewayapi.service; + +import ru.spcex.platform.utils.enumeration.IEnumKey; + +public enum OutboundRequestType implements IEnumKey { + ASSET_OPERATION("ASSET_OPERATION"), + ON_DEMAND("ON_DEMAND"), + FILL_LIMITS("FILL_LIMITS"), + MEMBER_ON_DEMAND("MEMBER_ON_DEMAND"), + ; + + private final String key; + + OutboundRequestType(String key) { + this.key = key; + } + + @Override + public String getKey() { + return this.key; + } + + @Override + public boolean equalsByKey(String key) { + return IEnumKey.super.equalsByKey(key); + } +}