Solon v3.5.1

路由拦截器 RouterInterceptor

</> markdown

前置参考:

1、接口声明

package org.noear.solon.core.route;

import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;
import org.noear.solon.core.wrap.ParamWrap;
import org.noear.solon.lang.Nullable;

@FunctionalInterface
public interface RouterInterceptor {
    /**
     * 路径匹配模式
     */
    default PathRule pathPatterns() {
        //null 表示全部
        return null;
    }

    /**
     * 执行拦截
     */
    void doIntercept(Context ctx,
                     @Nullable Handler mainHandler,
                     RouterInterceptorChain chain) throws Throwable;

    /**
     * 提交参数(MethodWrap::invokeByAspect 执行前调用)
     */
    default void postArguments(Context ctx, ParamWrap[] args, Object[] vals) throws Throwable {

    }

    /**
     * 提交结果(action / render 执行前调用)
     */
    default Object postResult(Context ctx, @Nullable Object result) throws Throwable {
        return result;
    }
}

2、定制参考1

public class CrossInterceptor extends AbstractCross<CrossInterceptor> implements RouterInterceptor {
    private PathRule pathRule;

    /**
     * 设置路径匹配模式
     *
     * @since 3.0
     */
    public CrossInterceptor pathPatterns(String... patterns) {
        this.pathRule = new PathRule().include(patterns);
        return this;
    }

    @Override
    public PathRule pathPatterns() {
        return pathRule;
    }

    @Override
    public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {
        doHandle(ctx);

        if (ctx.getHandled() == false) {
            chain.doIntercept(ctx, mainHandler);
        }
    }
}