FINERACT-1744 add idempotency key storing for http requests

This commit is contained in:
Zoltan Nebli
2022-10-27 10:37:51 +02:00
committed by Arnold Gálovics
parent 3898e2b836
commit a293dee55c
3 changed files with 20 additions and 2 deletions
@@ -38,6 +38,7 @@ import org.apache.fineract.commands.handler.NewCommandSourceHandler;
import org.apache.fineract.commands.provider.CommandHandlerProvider;
import org.apache.fineract.infrastructure.configuration.domain.ConfigurationDomainService;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder;
import org.apache.fineract.infrastructure.core.serialization.ToApiJsonSerializer;
@@ -49,6 +50,9 @@ import org.apache.fineract.useradministration.domain.AppUser;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Service
@Slf4j
@@ -62,8 +66,8 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
private final CommandSourceRepository commandSourceRepository;
private final ConfigurationDomainService configurationDomainService;
private final CommandHandlerProvider commandHandlerProvider;
private final IdempotencyKeyGenerator idempotencyKeyGenerator;
private final FineractProperties fineractProperties;
@Transactional
@Override
@@ -90,8 +94,19 @@ public class SynchronousCommandProcessingService implements CommandProcessingSer
.orElseThrow(() -> new CommandNotFoundException(command.commandId()));
commandSourceResult.markAsChecked(maker);
} else {
String requestIdempotencyKey = null;
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes != null) {
if (requestAttributes instanceof ServletRequestAttributes) {
requestIdempotencyKey = ((ServletRequestAttributes) requestAttributes).getRequest()
.getHeader(fineractProperties.getIdempotencyKeyHeaderName());
}
}
commandSourceResult = CommandSource.fullEntryFrom(wrapper, command, maker,
wrapper.getIdempotencyKey() == null ? idempotencyKeyGenerator.create() : wrapper.getIdempotencyKey());
wrapper.getIdempotencyKey() == null
? (requestIdempotencyKey == null ? idempotencyKeyGenerator.create() : requestIdempotencyKey)
: wrapper.getIdempotencyKey());
}
commandSourceResult.updateResourceId(result.getResourceId());
commandSourceResult.updateForAudit(result.getOfficeId(), result.getGroupId(), result.getClientId(), result.getLoanId(),
@@ -31,6 +31,8 @@ public class FineractProperties {
private String nodeId;
private String idempotencyKeyHeaderName;
private FineractTenantProperties tenant;
private FineractModeProperties mode;
@@ -59,6 +59,7 @@ fineract.events.external.producer.jms.enabled=${FINERACT_EXTERNAL_EVENTS_PRODUCE
fineract.events.external.producer.jms.event-queue-name=${FINERACT_EXTERNAL_EVENTS_PRODUCER_JMS_QUEUE_NAME:JMS-event-queue}
fineract.events.external.producer.jms.broker-url=${FINERACT_EXTERNAL_EVENTS_PRODUCER_JMS_BROKER_URL:tcp://127.0.0.1:61616}
fineract.idempotency-key-header-name=${FINERACT_IDEMPOTENCY_KEY_HEADER_NAME:Idempotency-Key}
# Logging pattern for the console
logging.pattern.console=${CONSOLE_LOG_PATTERN:%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(%replace([%X{correlationId}]){'\\[\\]', ''}) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}}