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.Collections;
020import java.util.Map;
021
022import org.springframework.core.MethodParameter;
023import org.springframework.core.ReactiveAdapterRegistry;
024import org.springframework.util.StringUtils;
025import org.springframework.web.bind.annotation.PathVariable;
026import org.springframework.web.reactive.BindingContext;
027import org.springframework.web.reactive.HandlerMapping;
028import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport;
029import org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver;
030import org.springframework.web.server.ServerWebExchange;
031
032/**
033 * Resolver for {@link Map} method arguments also annotated with
034 * {@link PathVariable @PathVariable} where the annotation does not specify a
035 * path variable name. The resulting {@link Map} argument is a coyp of all URI
036 * template name-value pairs.
037 *
038 * @author Rossen Stoyanchev
039 * @since 5.0
040 * @see PathVariableMethodArgumentResolver
041 */
042public class PathVariableMapMethodArgumentResolver extends HandlerMethodArgumentResolverSupport
043                implements SyncHandlerMethodArgumentResolver {
044
045        public PathVariableMapMethodArgumentResolver(ReactiveAdapterRegistry adapterRegistry) {
046                super(adapterRegistry);
047        }
048
049
050        @Override
051        public boolean supportsParameter(MethodParameter parameter) {
052                return checkAnnotatedParamNoReactiveWrapper(parameter, PathVariable.class, this::allVariables);
053        }
054
055        private boolean allVariables(PathVariable pathVariable, Class<?> type) {
056                return (Map.class.isAssignableFrom(type) && !StringUtils.hasText(pathVariable.value()));
057        }
058
059
060        @Override
061        public Object resolveArgumentValue(
062                        MethodParameter methodParameter, BindingContext context, ServerWebExchange exchange) {
063
064                String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
065                return exchange.getAttributeOrDefault(name, Collections.emptyMap());
066        }
067
068}