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

dev.vality.woody.api.interceptor.ext.ExtendableInterceptor Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
package dev.vality.woody.api.interceptor.ext;

import dev.vality.woody.api.interceptor.Interceptor;
import dev.vality.woody.api.trace.TraceData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ExtendableInterceptor implements Interceptor {

    private static final Logger LOG = LoggerFactory.getLogger(ExtendableInterceptor.class);

    private final InterceptorExtension[] extensions;
    private final BiFunction errContextResolver;

    public ExtendableInterceptor(List extensions,
                                 BiFunction errContextResolver) {
        this.extensions = extensions.toArray(new InterceptorExtension[0]);
        this.errContextResolver = errContextResolver;
    }

    public ExtendableInterceptor(List extensionBundles,
                                 BiFunction errContextResolver, boolean isClient,
                                 boolean isRequest) {
        this(extensionBundles.stream().map(bundle -> {
            ExtensionBundle.ContextBundle ctxBundle = isClient ? bundle.getClientBundle() : bundle.getServiceBundle();
            return isRequest ? ctxBundle.getRequestExtension() : ctxBundle.getResponseExtension();
        }).collect(Collectors.toList()), errContextResolver);
    }

    public ExtendableInterceptor(Function> primaryBundlesProvider,
                                 List secondaryBundles, boolean isClient, boolean isRequest) {
        this(concatBundleLists(primaryBundlesProvider.apply(isClient), secondaryBundles),
                (traceData, throwable) -> isClient, isClient, isRequest);
    }

    public static List concatBundleLists(List first, List second) {
        return Stream.concat(first.stream(), second.stream()).collect(Collectors.toList());
    }

    @Override
    public boolean intercept(TraceData traceData, Object providerContext, Object... contextParams) {
        try {
            LOG.trace("Intercept for multiple extensions");
            ExtensionContext extContext = createContext(traceData, providerContext, contextParams);
            initInterception(extContext);
            for (InterceptorExtension extension : extensions) {
                extension.apply(extContext);
            }
            finalizeInterception(extContext);
            return true;
        } catch (Exception e) {
            LOG.trace("Intercept extension error");
            return interceptError(traceData, e, errContextResolver.apply(traceData, e));
        }
    }

    protected ExtensionContext createContext(TraceData traceData, Object providerContext, Object[] contextParams) {
        return new ExtensionContext(traceData, providerContext, contextParams);
    }

    protected void initInterception(ExtensionContext extContext) {
    }

    protected void finalizeInterception(ExtensionContext extContext) {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy