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

cc.shacocloud.mirage.web.bind.support.PathVariableMethodArgumentResolver Maven / Gradle / Ivy

package cc.shacocloud.mirage.web.bind.support;

import cc.shacocloud.mirage.utils.Utils;
import cc.shacocloud.mirage.web.HttpRequest;
import cc.shacocloud.mirage.web.bind.annotation.PathVariable;
import cc.shacocloud.mirage.web.bind.ValueConstants;
import io.vertx.core.Future;
import org.springframework.core.MethodParameter;
import org.springframework.util.StringUtils;

/**
 * 解析{@link PathVariable} 注解的方法参数
 */
public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(PathVariable.class);
    }

    @Override
    protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
        PathVariable ann = parameter.getParameterAnnotation(PathVariable.class);
        if (ann != null) return new PathVariableNamedValueInfo(ann);

        throw new IllegalStateException("不支持处理当前方法,请先执行 supportsParameter 方法进行判断:" + Utils.methodDescription(parameter.getDeclaringClass(), parameter.getMethod()));
    }

    @Override
    protected String resolveStringValue(String value) {
        return value;
    }

    @Override
    protected Future resolveName(String name, MethodParameter parameter, HttpRequest request) {
        String param = request.pathParam(name);

        if (StringUtils.hasText(param)) {
            return Future.succeededFuture(param);
        }

        return Future.succeededFuture();
    }

    private static class PathVariableNamedValueInfo extends NamedValueInfo {

        public PathVariableNamedValueInfo(PathVariable annotation) {
            super(annotation.name(), true, ValueConstants.DEFAULT_NONE);
        }

    }

}