001/* 002 * Copyright 2002-2020 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.rsocket.annotation.support; 018 019import io.rsocket.RSocket; 020import reactor.core.publisher.Mono; 021 022import org.springframework.core.MethodParameter; 023import org.springframework.messaging.Message; 024import org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver; 025import org.springframework.messaging.rsocket.RSocketRequester; 026import org.springframework.util.Assert; 027 028/** 029 * Resolves arguments of type {@link RSocket} that can be used for making 030 * requests to the remote peer. 031 * 032 * @author Rossen Stoyanchev 033 * @since 5.2 034 */ 035public class RSocketRequesterMethodArgumentResolver implements HandlerMethodArgumentResolver { 036 037 /** 038 * Message header name that is expected to have the {@link RSocket} to 039 * initiate new interactions to the remote peer with. 040 */ 041 public static final String RSOCKET_REQUESTER_HEADER = "rsocketRequester"; 042 043 044 @Override 045 public boolean supportsParameter(MethodParameter parameter) { 046 Class<?> type = parameter.getParameterType(); 047 return (RSocketRequester.class.equals(type) || RSocket.class.isAssignableFrom(type)); 048 } 049 050 @Override 051 public Mono<Object> resolveArgument(MethodParameter parameter, Message<?> message) { 052 Object headerValue = message.getHeaders().get(RSOCKET_REQUESTER_HEADER); 053 Assert.notNull(headerValue, "Missing '" + RSOCKET_REQUESTER_HEADER + "'"); 054 055 Assert.isInstanceOf(RSocketRequester.class, headerValue, "Expected header value of type RSocketRequester"); 056 RSocketRequester requester = (RSocketRequester) headerValue; 057 058 Class<?> type = parameter.getParameterType(); 059 if (RSocketRequester.class.equals(type)) { 060 return Mono.just(requester); 061 } 062 else if (RSocket.class.isAssignableFrom(type)) { 063 return Mono.justOrEmpty(requester.rsocket()); 064 } 065 else { 066 return Mono.error(new IllegalArgumentException("Unexpected parameter type: " + parameter)); 067 } 068 } 069 070}