/* * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.web.reactive.result.method.annotation; import java.util.Map; import reactor.core.publisher.Mono; import org.springframework.core.MethodParameter; import org.springframework.http.HttpHeaders; import org.springframework.ui.ModelMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver; import org.springframework.web.server.ServerWebExchange; /** * Resolves {@link Map} method arguments annotated with {@code @RequestHeader}. * For individual header values annotated with {@code @RequestHeader} see * {@link RequestHeaderMethodArgumentResolver} instead. * * <p>The created {@link Map} contains all request header name/value pairs. * The method parameter type may be a {@link MultiValueMap} to receive all * values for a header, not only the first one. * * @author Rossen Stoyanchev * @see RequestHeaderMethodArgumentResolver */ public class RequestHeaderMapMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { return (parameter.hasParameterAnnotation(RequestHeader.class) && Map.class.isAssignableFrom(parameter.getParameterType())); } @Override public Mono<Object> resolveArgument(MethodParameter parameter, ModelMap model, ServerWebExchange exchange) { HttpHeaders headers = exchange.getRequest().getHeaders(); if (MultiValueMap.class.isAssignableFrom(parameter.getParameterType())) { return Mono.just(headers); } else { return Mono.just(headers.toSingleValueMap()); } } }