From 2171d59c2f4d08807c29c076808b66ef11b48a29 Mon Sep 17 00:00:00 2001 From: psmagin Date: Fri, 21 Jun 2024 11:22:37 +0300 Subject: [PATCH] - change execution service --- .../service/TenantScopedExecutionService.java | 47 ++++++------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/folio/search/service/TenantScopedExecutionService.java b/src/main/java/org/folio/search/service/TenantScopedExecutionService.java index 6c2e53a1a..d41c68b40 100644 --- a/src/main/java/org/folio/search/service/TenantScopedExecutionService.java +++ b/src/main/java/org/folio/search/service/TenantScopedExecutionService.java @@ -1,5 +1,6 @@ package org.folio.search.service; +import static java.util.Collections.emptyMap; import static java.util.Collections.singleton; import static org.apache.commons.lang3.StringUtils.isNotBlank; @@ -26,16 +27,19 @@ @Primary public class TenantScopedExecutionService extends SystemUserScopedExecutionService { - private final ExecutionContextBuilder contextBuilder; + private final FolioExecutionContext executionContext; + private final TenantScopedExecutionContextBuilder contextBuilder; public TenantScopedExecutionService(FolioExecutionContext executionContext, ExecutionContextBuilder contextBuilder) { super(executionContext, contextBuilder); - this.contextBuilder = contextBuilder; + this.executionContext = executionContext; + this.contextBuilder = (TenantScopedExecutionContextBuilder) contextBuilder; } public T executeTenantScoped(String tenantId, Callable action) { - try (var fex = new FolioExecutionContextSetter(contextBuilder.buildContext(tenantId))) { + Map> headers = executionContext == null ? emptyMap() : executionContext.getAllHeaders(); + try (var fex = new FolioExecutionContextSetter(contextBuilder.buildContext(tenantId, headers))) { log.info("Executing tenant scoped action [tenant={}]", tenantId); return action.call(); } catch (Exception e) { @@ -48,42 +52,21 @@ public T executeTenantScoped(String tenantId, Callable action) { @Component protected static class TenantScopedExecutionContextBuilder extends ExecutionContextBuilder { - private final FolioExecutionContext executionContext; + private final FolioModuleMetadata moduleMetadata; TenantScopedExecutionContextBuilder(FolioEnvironment folioEnvironment, - FolioModuleMetadata moduleMetadata, - FolioExecutionContext executionContext) { + FolioModuleMetadata moduleMetadata) { super(folioEnvironment, moduleMetadata); - this.executionContext = executionContext; + this.moduleMetadata = moduleMetadata; } - @Override - public FolioExecutionContext buildContext(String tenantId) { - return buildContextWithTenant(tenantId); - } - - private FolioExecutionContext buildContextWithTenant(String tenantId) { - Map> headers = new HashMap<>(); + public FolioExecutionContext buildContext(String tenantId, Map> headers) { + Map> newHeaders = new HashMap<>(headers); if (isNotBlank(tenantId)) { - headers.put(XOkapiHeaders.TENANT, singleton(tenantId)); - } - var okapiUrl = executionContext.getOkapiUrl(); - if (isNotBlank(okapiUrl)) { - headers.put(XOkapiHeaders.URL, singleton(okapiUrl)); - } - var token = executionContext.getToken(); - if (isNotBlank(token)) { - headers.put(XOkapiHeaders.TOKEN, singleton(token)); + newHeaders.put(XOkapiHeaders.TENANT, singleton(tenantId)); } - var userId = executionContext.getUserId() == null ? "" : executionContext.getUserId().toString(); - if (isNotBlank(userId)) { - headers.put(XOkapiHeaders.USER_ID, singleton(userId)); - } - var requestId = executionContext.getRequestId(); - if (isNotBlank(requestId)) { - headers.put(XOkapiHeaders.REQUEST_ID, singleton(requestId)); - } - return new DefaultFolioExecutionContext(executionContext.getFolioModuleMetadata(), headers); + return new DefaultFolioExecutionContext(moduleMetadata, newHeaders); } + } }