-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
214 additions
and
163 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/java/org/folio/search/exception/ScopeExecutionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.folio.search.exception; | ||
|
||
public class ScopeExecutionException extends RuntimeException { | ||
|
||
public ScopeExecutionException(Exception e) { | ||
super(e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/org/folio/search/service/TenantScopedExecutionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.folio.search.service; | ||
|
||
import static java.util.Collections.singleton; | ||
import static org.apache.commons.lang3.StringUtils.isNotBlank; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.Callable; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.folio.search.exception.ScopeExecutionException; | ||
import org.folio.spring.DefaultFolioExecutionContext; | ||
import org.folio.spring.FolioExecutionContext; | ||
import org.folio.spring.FolioModuleMetadata; | ||
import org.folio.spring.config.properties.FolioEnvironment; | ||
import org.folio.spring.context.ExecutionContextBuilder; | ||
import org.folio.spring.integration.XOkapiHeaders; | ||
import org.folio.spring.scope.FolioExecutionContextSetter; | ||
import org.folio.spring.service.SystemUserScopedExecutionService; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Log4j2 | ||
@Service | ||
@Primary | ||
public class TenantScopedExecutionService extends SystemUserScopedExecutionService { | ||
|
||
private final ExecutionContextBuilder contextBuilder; | ||
|
||
public TenantScopedExecutionService(FolioExecutionContext executionContext, | ||
ExecutionContextBuilder contextBuilder) { | ||
super(executionContext, contextBuilder); | ||
this.contextBuilder = contextBuilder; | ||
} | ||
|
||
public <T> T executeTenantScoped(String tenantId, Callable<T> action) { | ||
try (var fex = new FolioExecutionContextSetter(contextBuilder.buildContext(tenantId))) { | ||
log.info("Executing tenant scoped action [tenant={}]", tenantId); | ||
new ScopeExecutionException(new RuntimeException()).printStackTrace(); | ||
return action.call(); | ||
} catch (Exception e) { | ||
log.error("Failed to execute tenant scoped action", e); | ||
throw new ScopeExecutionException(e); | ||
} | ||
} | ||
|
||
@Primary | ||
@Component | ||
protected static class TenantScopedExecutionContextBuilder extends ExecutionContextBuilder { | ||
|
||
private final FolioExecutionContext executionContext; | ||
|
||
TenantScopedExecutionContextBuilder(FolioEnvironment folioEnvironment, | ||
FolioModuleMetadata moduleMetadata, | ||
FolioExecutionContext executionContext) { | ||
super(folioEnvironment, moduleMetadata); | ||
this.executionContext = executionContext; | ||
} | ||
|
||
@Override | ||
public FolioExecutionContext buildContext(String tenantId) { | ||
return buildContextWithTenant(tenantId); | ||
} | ||
|
||
private FolioExecutionContext buildContextWithTenant(String tenantId) { | ||
Map<String, Collection<String>> headers = new HashMap<>(); | ||
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)); | ||
} | ||
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.