001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.reactive.result.method.annotation;
018
019import java.util.Map;
020
021import org.springframework.core.MethodParameter;
022import org.springframework.core.ReactiveAdapterRegistry;
023import org.springframework.http.HttpHeaders;
024import org.springframework.util.MultiValueMap;
025import org.springframework.web.bind.annotation.RequestHeader;
026import org.springframework.web.reactive.BindingContext;
027import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport;
028import org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver;
029import org.springframework.web.server.ServerWebExchange;
030
031/**
032 * Resolves {@link Map} method arguments annotated with {@code @RequestHeader}.
033 * For individual header values annotated with {@code @RequestHeader} see
034 * {@link RequestHeaderMethodArgumentResolver} instead.
035 *
036 * <p>The created {@link Map} contains all request header name/value pairs.
037 * The method parameter type may be a {@link MultiValueMap} to receive all
038 * values for a header, not only the first one.
039 *
040 * @author Rossen Stoyanchev
041 * @since 5.0
042 * @see RequestHeaderMethodArgumentResolver
043 */
044public class RequestHeaderMapMethodArgumentResolver extends HandlerMethodArgumentResolverSupport
045                implements SyncHandlerMethodArgumentResolver {
046
047        public RequestHeaderMapMethodArgumentResolver(ReactiveAdapterRegistry adapterRegistry) {
048                super(adapterRegistry);
049        }
050
051
052        @Override
053        public boolean supportsParameter(MethodParameter param) {
054                return checkAnnotatedParamNoReactiveWrapper(param, RequestHeader.class, this::allParams);
055        }
056
057        private boolean allParams(RequestHeader annotation, Class<?> type) {
058                return Map.class.isAssignableFrom(type);
059        }
060
061
062        @Override
063        public Object resolveArgumentValue(
064                        MethodParameter methodParameter, BindingContext context, ServerWebExchange exchange) {
065
066                boolean isMultiValueMap = MultiValueMap.class.isAssignableFrom(methodParameter.getParameterType());
067                HttpHeaders headers = exchange.getRequest().getHeaders();
068                return (isMultiValueMap ? headers : headers.toSingleValueMap());
069        }
070
071}