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

cc.shacocloud.mirage.web.bind.support.RequestHeaderMethodArgumentResolver 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.RequestHeader;
import cc.shacocloud.mirage.web.http.HttpHeaderMap;
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import org.springframework.core.MethodParameter;

import java.util.List;

/**
 * 解析{@link RequestHeader} 注解的方法参数。
 * 

* 支持三种方法参数定义类型: *

  • MultiMap类型,即当前请求的所有请求头{@link HttpHeaderMap}
  • *
  • List类型,即当前请求头指定名称的所有值
  • *
  • 其他,即当前请求头指定名称的第一个值
  • */ public class RequestHeaderMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(RequestHeader.class); } @Override protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) { RequestHeader ann = parameter.getParameterAnnotation(RequestHeader.class); if (ann != null) return new HeaderNamedValueInfo(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) { HttpHeaderMap headers = request.headers(); Class parameterType = parameter.getParameterType(); // MultiMap 为当前所有请求头 if (MultiMap.class.isAssignableFrom(parameterType)) { return Future.succeededFuture(headers); } // List 为指定名称请求头的所有值 if (List.class.isAssignableFrom(parameterType)) { return Future.succeededFuture(headers.getAll(name)); } // 其他 则为指定名称请求头的第一个值 return Future.succeededFuture(headers.get(name)); } private static class HeaderNamedValueInfo extends NamedValueInfo { public HeaderNamedValueInfo(RequestHeader annotation) { super(annotation.name(), annotation.required(), annotation.defaultValue()); } } }