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.messaging.handler.annotation.support;
018
019import java.util.Map;
020
021import org.springframework.core.MethodParameter;
022import org.springframework.core.convert.ConversionService;
023import org.springframework.messaging.Message;
024import org.springframework.messaging.MessageHandlingException;
025import org.springframework.messaging.handler.annotation.DestinationVariable;
026import org.springframework.messaging.handler.annotation.ValueConstants;
027
028/**
029 * Resolves method parameters annotated with
030 * {@link org.springframework.messaging.handler.annotation.DestinationVariable @DestinationVariable}.
031 *
032 * @author Brian Clozel
033 * @since 4.0
034 */
035public class DestinationVariableMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver {
036
037        public static final String DESTINATION_TEMPLATE_VARIABLES_HEADER =
038                        DestinationVariableMethodArgumentResolver.class.getSimpleName() + ".templateVariables";
039
040
041        public DestinationVariableMethodArgumentResolver(ConversionService cs) {
042                super(cs, null);
043        }
044
045
046        @Override
047        public boolean supportsParameter(MethodParameter parameter) {
048                return parameter.hasParameterAnnotation(DestinationVariable.class);
049        }
050
051        @Override
052        protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
053                DestinationVariable annotation = parameter.getParameterAnnotation(DestinationVariable.class);
054                return new DestinationVariableNamedValueInfo(annotation);
055        }
056
057        @Override
058        protected Object resolveArgumentInternal(MethodParameter parameter, Message<?> message, String name)
059                        throws Exception {
060
061                @SuppressWarnings("unchecked")
062                Map<String, String> vars =
063                                (Map<String, String>) message.getHeaders().get(DESTINATION_TEMPLATE_VARIABLES_HEADER);
064                return (vars != null ? vars.get(name) : null);
065        }
066
067        @Override
068        protected void handleMissingValue(String name, MethodParameter parameter, Message<?> message) {
069                throw new MessageHandlingException(message, "Missing path template variable '" + name +
070                                "' for method parameter type [" + parameter.getParameterType() + "]");
071        }
072
073
074        private static class DestinationVariableNamedValueInfo extends NamedValueInfo {
075
076                private DestinationVariableNamedValueInfo(DestinationVariable annotation) {
077                        super(annotation.value(), true, ValueConstants.DEFAULT_NONE);
078                }
079        }
080
081}