Skip to content

Commit

Permalink
Move shared methods to PreEnforcer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
DerSchwilk committed May 24, 2022
1 parent efc4ec5 commit 17d9ecd
Showing 1 changed file with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@
*/
package org.eclipse.ditto.policies.enforcement;

import java.text.MessageFormat;
import java.util.Optional;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;

import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import javax.annotation.Nullable;

import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.DittoHeadersSettable;
import org.eclipse.ditto.base.model.headers.WithDittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.internal.utils.akka.controlflow.WithSender;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLogger;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
Expand All @@ -34,13 +39,42 @@
*
* @param <T> the type of the signals to pre-enforce.
*/
public interface PreEnforcer<T extends DittoHeadersSettable<?>> extends Function<T, CompletionStage<T>>{
public interface PreEnforcer<T extends DittoHeadersSettable<?>> extends Function<T, CompletionStage<T>> {

/**
* Logger of pre-enforcers.
*/
DittoLogger LOGGER = DittoLoggerFactory.getLogger(PreEnforcer.class);

/**
* Safe cast the message to a signal
*
* @param message the message to be cast.
* @return the signal.
*/
default Signal<?> getMessageAsSignal(@Nullable final WithDittoHeaders message) {
if (message instanceof Signal) {
return (Signal<?>) message;
}
if (null == message) {
// just in case
LOGGER.error("Given message is null!");
throw DittoInternalErrorException.newBuilder().build();
}
final String msgPattern = "Message of type <{0}> is not a signal!";
throw new IllegalArgumentException(MessageFormat.format(msgPattern, message.getClass()));
}

/**
* Extracts the {@code EntityId} of a signal.
*
* @param signal the signal to retrieve the {@code EntityId} from.
* @return the {@code EntityId}.
*/
default Optional<EntityId> extractEntityRelatedSignalId(Signal<?> signal) {
return WithEntityId.getEntityIdOfType(EntityId.class, signal);
}

/**
* Perform pre-enforcement with error handling.
*
Expand Down

0 comments on commit 17d9ecd

Please sign in to comment.