Skip to content

Commit

Permalink
refactor DefaultDittoHeadersValidator implementation to use failed co…
Browse files Browse the repository at this point in the history
…mpletion stages everywhere instead of throwing exceptions

Signed-off-by: Johannes Schneider <johannes.schneider@bosch.io>
  • Loading branch information
jokraehe committed Aug 29, 2022
1 parent 156f17e commit 74c40c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,35 @@ public DefaultDittoHeadersValidator(final ActorSystem actorSystem, final Config
@Override
public CompletionStage<DittoHeaders> validate(DittoHeaders dittoHeaders) {
checkNotNull(dittoHeaders, "dittoHeaders");
try {
validateAuthorizationContext(dittoHeaders);
if (dittoHeaders.isEntriesSizeGreaterThan(maxBytes)) {
throw DittoHeadersTooLargeException.newSizeLimitBuilder(maxBytes)
.dittoHeaders(dittoHeaders)
.build();
}
return CompletableFuture.completedStage(dittoHeaders);
} catch (DittoHeadersTooLargeException e) {
return CompletableFuture.failedStage(e);
return validateSize(dittoHeaders).thenCompose(this::validateAuthorizationContext);
}

/**
* Validates {@code dittoHeaders} against {@code maximum-size} defined in the extension configuration.
*
* @param dittoHeaders the headers to validate.
* @return a completion stage which completes successfully with the valid headers. Raises a
* {@link org.eclipse.ditto.base.model.exceptions.DittoHeadersTooLargeException} if {@code dittoHeaders} are not
* valid.
*/
public CompletionStage<DittoHeaders> validateSize(final DittoHeaders dittoHeaders) {
if (dittoHeaders.isEntriesSizeGreaterThan(maxBytes)) {
return CompletableFuture.failedStage(DittoHeadersTooLargeException.newSizeLimitBuilder(maxBytes)
.dittoHeaders(dittoHeaders)
.build());
}
return CompletableFuture.completedStage(dittoHeaders);
}

private void validateAuthorizationContext(final DittoHeaders dittoHeaders) {
private CompletionStage<DittoHeaders> validateAuthorizationContext(final DittoHeaders dittoHeaders) {
final int authSubjectsCount = dittoHeaders.getAuthorizationContext().getSize();
if (authSubjectsCount > maxAuthSubjects) {
throw DittoHeadersTooLargeException.newAuthSubjectsLimitBuilder(authSubjectsCount, maxAuthSubjects)
.dittoHeaders(dittoHeaders)
.build();
return CompletableFuture.failedStage(
DittoHeadersTooLargeException.newAuthSubjectsLimitBuilder(authSubjectsCount, maxAuthSubjects)
.dittoHeaders(dittoHeaders)
.build());
}
return CompletableFuture.completedStage(dittoHeaders);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import akka.actor.ActorSystem;
import akka.testkit.javadsl.TestKit;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -41,14 +40,10 @@ public final class DefaultDittoHeadersValidatorTest {

@Nullable
private static ActorSystem actorSystem;
@Nullable
private static Config extensionConfig;

@BeforeClass
public static void init() {
actorSystem = ActorSystem.create("AkkaTestSystem", ConfigFactory.load("test"));
extensionConfig = ScopedConfig.dittoExtension(actorSystem.settings().config())
.getConfig("ditto-headers-validator.extension-config");
}

@AfterClass
Expand All @@ -61,9 +56,8 @@ public static void tearDown() {
@Test
public void validationFailsForTooManyAuthSubjects() {
assert actorSystem != null;
assert extensionConfig != null;

final var underTest = new DefaultDittoHeadersValidator(actorSystem, extensionConfig);
final var underTest = DittoHeadersValidator.get(actorSystem, ScopedConfig.dittoExtension(actorSystem.settings().config()));

final var authorizationContext = AuthorizationContext.newInstance(
DittoAuthorizationContextType.PRE_AUTHENTICATED_HTTP,
Expand Down

0 comments on commit 74c40c2

Please sign in to comment.