All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.soffa.service.actions.DefaultActionDispatcher Maven / Gradle / Ivy

package io.soffa.service.actions;

import io.soffa.service.core.Action;
import io.soffa.service.core.RequestContext;
import io.soffa.commons.exceptions.TechnicalException;
import lombok.AllArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.inject.Named;
import java.util.Set;

@Named
@AllArgsConstructor
public class DefaultActionDispatcher implements ActionDispatcher {

    private Set> registry;

    @SuppressWarnings("unchecked")
    @Override
    public  O dispatch(Class> actionClass, I request)  {
        RequestContext context = (RequestContext)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return dispatch(actionClass, request, context);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  O dispatch(Class> actionClass, I request, RequestContext context)  {

        Action impl = null;
        for (Action act : registry) {
            if (actionClass.isAssignableFrom(act.getClass())) {
                impl = (Action) act;
                break;
            }
        }

        if (impl == null) {
            throw new TechnicalException("Unable to find implementation for action: {0}", actionClass.getName());
        }
        return impl.handle(request, context);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy