001/*
002 * Copyright 2002-2019 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 reactor.core.publisher.Mono;
020
021import org.springframework.core.MethodParameter;
022import org.springframework.core.ReactiveAdapter;
023import org.springframework.core.ReactiveAdapterRegistry;
024import org.springframework.web.reactive.BindingContext;
025import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport;
026import org.springframework.web.server.ServerWebExchange;
027import org.springframework.web.server.WebSession;
028
029/**
030 * Resolves method argument value of type {@link WebSession}.
031 *
032 * @author Rossen Stoyanchev
033 * @since 5.2
034 * @see ServerWebExchangeMethodArgumentResolver
035 */
036public class WebSessionMethodArgumentResolver extends HandlerMethodArgumentResolverSupport {
037
038        // We need this resolver separate from ServerWebExchangeArgumentResolver which
039        // implements SyncHandlerMethodArgumentResolver.
040
041
042        public WebSessionMethodArgumentResolver(ReactiveAdapterRegistry adapterRegistry) {
043                super(adapterRegistry);
044        }
045
046
047        @Override
048        public boolean supportsParameter(MethodParameter parameter) {
049                return checkParameterType(parameter, WebSession.class::isAssignableFrom);
050        }
051
052        @Override
053        public Mono<Object> resolveArgument(
054                        MethodParameter parameter, BindingContext context, ServerWebExchange exchange) {
055
056                Mono<WebSession> session = exchange.getSession();
057                ReactiveAdapter adapter = getAdapterRegistry().getAdapter(parameter.getParameterType());
058                return (adapter != null ? Mono.just(adapter.fromPublisher(session)) : Mono.from(session));
059        }
060
061}