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.messaging.handler.annotation.support;
018
019import java.lang.reflect.Method;
020import java.util.Map;
021
022import org.springframework.core.MethodParameter;
023import org.springframework.messaging.Message;
024import org.springframework.messaging.MessageHeaders;
025import org.springframework.messaging.handler.annotation.Headers;
026import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
027import org.springframework.messaging.support.MessageHeaderAccessor;
028import org.springframework.util.ReflectionUtils;
029
030/**
031 * Resolves the following method parameters:
032 * <ul>
033 * <li>Parameters assignable to {@link Map} annotated with {@link Headers @Headers}
034 * <li>Parameters of type {@link MessageHeaders}
035 * <li>Parameters assignable to {@link MessageHeaderAccessor}
036 * </ul>
037 *
038 * @author Rossen Stoyanchev
039 * @since 4.0
040 */
041public class HeadersMethodArgumentResolver implements HandlerMethodArgumentResolver {
042
043        @Override
044        public boolean supportsParameter(MethodParameter parameter) {
045                Class<?> paramType = parameter.getParameterType();
046                return ((parameter.hasParameterAnnotation(Headers.class) && Map.class.isAssignableFrom(paramType)) ||
047                                MessageHeaders.class == paramType || MessageHeaderAccessor.class.isAssignableFrom(paramType));
048        }
049
050        @Override
051        public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
052                Class<?> paramType = parameter.getParameterType();
053                if (Map.class.isAssignableFrom(paramType)) {
054                        return message.getHeaders();
055                }
056                else if (MessageHeaderAccessor.class == paramType) {
057                        MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
058                        return (accessor != null ? accessor : new MessageHeaderAccessor(message));
059                }
060                else if (MessageHeaderAccessor.class.isAssignableFrom(paramType)) {
061                        MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
062                        if (accessor != null && paramType.isAssignableFrom(accessor.getClass())) {
063                                return accessor;
064                        }
065                        else {
066                                Method method = ReflectionUtils.findMethod(paramType, "wrap", Message.class);
067                                if (method == null) {
068                                        throw new IllegalStateException(
069                                                        "Cannot create accessor of type " + paramType + " for message " + message);
070                                }
071                                return ReflectionUtils.invokeMethod(method, null, message);
072                        }
073                }
074                else {
075                        throw new IllegalStateException(
076                                        "Unexpected method parameter type " + paramType + "in method " + parameter.getMethod() + ". "
077                                        + "@Headers method arguments must be assignable to java.util.Map.");
078                }
079        }
080
081}