|
70 | 70 | import org.eclipse.ditto.policies.model.signals.commands.query.PolicyQueryCommandResponse; |
71 | 71 | import org.eclipse.ditto.policies.model.signals.commands.query.RetrievePolicy; |
72 | 72 | import org.eclipse.ditto.policies.model.signals.commands.query.RetrievePolicyResponse; |
| 73 | +import org.jspecify.annotations.NonNull; |
73 | 74 |
|
74 | 75 | /** |
75 | 76 | * Authorizes {@link PolicyCommand}s and filters {@link PolicyCommandResponse}s. |
@@ -315,10 +316,14 @@ private CompletableFuture<Optional<PolicyEnforcer>> resolveSourceEnforcer(final |
315 | 316 | // admin) fails hasSourceSideRead for every entry in the source and silently loses all imported |
316 | 317 | // entries in the resolved view. |
317 | 318 | return policyResolver.apply(sourceId) |
318 | | - .thenCompose(opt -> opt.isPresent() |
319 | | - ? PolicyEnforcer.withResolvedImportsAndNamespacePolicies(opt.get(), policyResolver, |
| 319 | + .thenCompose(opt -> opt |
| 320 | + .map(policyEntries -> PolicyEnforcer.withResolvedImportsAndNamespacePolicies( |
| 321 | + policyEntries, |
| 322 | + policyResolver, |
320 | 323 | namespacePoliciesConfig).thenApply(Optional::of) |
321 | | - : CompletableFuture.completedFuture(Optional.<PolicyEnforcer>empty())) |
| 324 | + ) |
| 325 | + .orElseGet(() -> CompletableFuture.completedFuture(Optional.empty())) |
| 326 | + ) |
322 | 327 | .toCompletableFuture(); |
323 | 328 | } |
324 | 329 |
|
@@ -476,27 +481,38 @@ private JsonObject getJsonViewForPolicyQueryCommandResponse(final JsonObject res |
476 | 481 | */ |
477 | 482 | private static DittoRuntimeException errorForPolicyCommand(final Signal<?> policySignal) { |
478 | 483 |
|
479 | | - if (policySignal instanceof PolicyCommand<?> policyCommand) { |
480 | | - final CommandToExceptionRegistry<PolicyCommand<?>, DittoRuntimeException> registry; |
481 | | - if (policyCommand instanceof PolicyActionCommand) { |
482 | | - registry = PolicyCommandToActionsExceptionRegistry.getInstance(); |
483 | | - } else if (policyCommand instanceof PolicyModifyCommand) { |
484 | | - registry = PolicyCommandToModifyExceptionRegistry.getInstance(); |
485 | | - } else { |
486 | | - registry = PolicyCommandToAccessExceptionRegistry.getInstance(); |
| 484 | + switch (policySignal) { |
| 485 | + case PolicyCommand<?> policyCommand -> { |
| 486 | + final CommandToExceptionRegistry<PolicyCommand<?>, DittoRuntimeException> registry = |
| 487 | + getPolicyCommandDittoRuntimeExceptionCommandToExceptionRegistry(policyCommand); |
| 488 | + return registry.exceptionFrom(policyCommand); |
487 | 489 | } |
488 | | - return registry.exceptionFrom(policyCommand); |
489 | | - } else if (policySignal instanceof WithEntityId withEntityId) { |
490 | | - return PolicyNotAccessibleException.newBuilder(PolicyId.of(withEntityId.getEntityId())) |
491 | | - .dittoHeaders(policySignal.getDittoHeaders()) |
492 | | - .build(); |
| 490 | + case WithEntityId withEntityId -> { |
| 491 | + return PolicyNotAccessibleException.newBuilder(PolicyId.of(withEntityId.getEntityId())) |
| 492 | + .dittoHeaders(policySignal.getDittoHeaders()) |
| 493 | + .build(); |
| 494 | + } |
| 495 | + default -> { |
| 496 | + LOGGER.error("Received signal for which no DittoRuntimeException due to lack of access " + |
| 497 | + "could be determined: {}", policySignal); |
| 498 | + return DittoInternalErrorException.newBuilder() |
| 499 | + .dittoHeaders(policySignal.getDittoHeaders()) |
| 500 | + .build(); |
| 501 | + } |
| 502 | + } |
| 503 | + } |
| 504 | + |
| 505 | + private static CommandToExceptionRegistry<PolicyCommand<?>, DittoRuntimeException> |
| 506 | + getPolicyCommandDittoRuntimeExceptionCommandToExceptionRegistry(PolicyCommand<?> policyCommand) { |
| 507 | + final CommandToExceptionRegistry<PolicyCommand<?>, DittoRuntimeException> registry; |
| 508 | + if (policyCommand instanceof PolicyActionCommand) { |
| 509 | + registry = PolicyCommandToActionsExceptionRegistry.getInstance(); |
| 510 | + } else if (policyCommand instanceof PolicyModifyCommand) { |
| 511 | + registry = PolicyCommandToModifyExceptionRegistry.getInstance(); |
493 | 512 | } else { |
494 | | - LOGGER.error("Received signal for which no DittoRuntimeException due to lack of access " + |
495 | | - "could be determined: {}", policySignal); |
496 | | - return DittoInternalErrorException.newBuilder() |
497 | | - .dittoHeaders(policySignal.getDittoHeaders()) |
498 | | - .build(); |
| 513 | + registry = PolicyCommandToAccessExceptionRegistry.getInstance(); |
499 | 514 | } |
| 515 | + return registry; |
500 | 516 | } |
501 | 517 |
|
502 | 518 | /** |
|
0 commit comments