This is an automated email from the ASF dual-hosted git repository.

arnold pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract.git


The following commit(s) were added to refs/heads/develop by this push:
     new a293dee55 FINERACT-1744 add idempotency key storing for http requests
a293dee55 is described below

commit a293dee55c515a08b5060b56734876daad2bf085
Author: Zoltan Nebli <[email protected]>
AuthorDate: Sun Oct 23 10:28:27 2022 +0200

    FINERACT-1744 add idempotency key storing for http requests
---
 .../service/SynchronousCommandProcessingService.java  | 19 +++++++++++++++++--
 .../core/config/FineractProperties.java               |  2 ++
 .../src/main/resources/application.properties         |  1 +
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java
 
b/fineract-provider/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java
index 537c09ba0..43ae13cb4 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/commands/service/SynchronousCommandProcessingService.java
@@ -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(),
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java
 
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java
index 996b0116b..d0b70e6eb 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java
@@ -31,6 +31,8 @@ public class FineractProperties {
 
     private String nodeId;
 
+    private String idempotencyKeyHeaderName;
+
     private FineractTenantProperties tenant;
 
     private FineractModeProperties mode;
diff --git a/fineract-provider/src/main/resources/application.properties 
b/fineract-provider/src/main/resources/application.properties
index 92d8558b0..03f3f4283 100644
--- a/fineract-provider/src/main/resources/application.properties
+++ b/fineract-provider/src/main/resources/application.properties
@@ -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}}

Reply via email to