001/* 002 * Copyright 2002-2016 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.method.annotation; 018 019import java.util.Iterator; 020import java.util.LinkedHashMap; 021import java.util.Map; 022 023import org.springframework.core.MethodParameter; 024import org.springframework.http.HttpHeaders; 025import org.springframework.lang.Nullable; 026import org.springframework.util.LinkedMultiValueMap; 027import org.springframework.util.MultiValueMap; 028import org.springframework.web.bind.annotation.RequestHeader; 029import org.springframework.web.bind.support.WebDataBinderFactory; 030import org.springframework.web.context.request.NativeWebRequest; 031import org.springframework.web.method.support.HandlerMethodArgumentResolver; 032import org.springframework.web.method.support.ModelAndViewContainer; 033 034/** 035 * Resolves {@link Map} method arguments annotated with {@code @RequestHeader}. 036 * For individual header values annotated with {@code @RequestHeader} see 037 * {@link RequestHeaderMethodArgumentResolver} instead. 038 * 039 * <p>The created {@link Map} contains all request header name/value pairs. 040 * The method parameter type may be a {@link MultiValueMap} to receive all 041 * values for a header, not only the first one. 042 * 043 * @author Arjen Poutsma 044 * @author Rossen Stoyanchev 045 * @since 3.1 046 */ 047public class RequestHeaderMapMethodArgumentResolver implements HandlerMethodArgumentResolver { 048 049 @Override 050 public boolean supportsParameter(MethodParameter parameter) { 051 return (parameter.hasParameterAnnotation(RequestHeader.class) && 052 Map.class.isAssignableFrom(parameter.getParameterType())); 053 } 054 055 @Override 056 public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, 057 NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { 058 059 Class<?> paramType = parameter.getParameterType(); 060 if (MultiValueMap.class.isAssignableFrom(paramType)) { 061 MultiValueMap<String, String> result; 062 if (HttpHeaders.class.isAssignableFrom(paramType)) { 063 result = new HttpHeaders(); 064 } 065 else { 066 result = new LinkedMultiValueMap<>(); 067 } 068 for (Iterator<String> iterator = webRequest.getHeaderNames(); iterator.hasNext();) { 069 String headerName = iterator.next(); 070 String[] headerValues = webRequest.getHeaderValues(headerName); 071 if (headerValues != null) { 072 for (String headerValue : headerValues) { 073 result.add(headerName, headerValue); 074 } 075 } 076 } 077 return result; 078 } 079 else { 080 Map<String, String> result = new LinkedHashMap<>(); 081 for (Iterator<String> iterator = webRequest.getHeaderNames(); iterator.hasNext();) { 082 String headerName = iterator.next(); 083 String headerValue = webRequest.getHeader(headerName); 084 if (headerValue != null) { 085 result.put(headerName, headerValue); 086 } 087 } 088 return result; 089 } 090 } 091 092}