|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.rsocket.dsl; |
| 18 | + |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +import org.reactivestreams.Publisher; |
| 22 | + |
| 23 | +import org.springframework.expression.Expression; |
| 24 | +import org.springframework.integration.dsl.MessageHandlerSpec; |
| 25 | +import org.springframework.integration.expression.FunctionExpression; |
| 26 | +import org.springframework.integration.expression.ValueExpression; |
| 27 | +import org.springframework.integration.rsocket.ClientRSocketConnector; |
| 28 | +import org.springframework.integration.rsocket.outbound.RSocketOutboundGateway; |
| 29 | +import org.springframework.messaging.Message; |
| 30 | +import org.springframework.messaging.rsocket.RSocketRequester; |
| 31 | +import org.springframework.messaging.rsocket.RSocketRequesterMethodArgumentResolver; |
| 32 | + |
| 33 | +/** |
| 34 | + * The {@link MessageHandlerSpec} implementation for the {@link RSocketOutboundGateway}. |
| 35 | + * |
| 36 | + * @author Artem Bilan |
| 37 | + * |
| 38 | + * @since 5.2 |
| 39 | + */ |
| 40 | +public class RSocketOutboundGatewaySpec extends MessageHandlerSpec<RSocketOutboundGatewaySpec, RSocketOutboundGateway> { |
| 41 | + |
| 42 | + RSocketOutboundGatewaySpec(Expression routeExpression) { |
| 43 | + this.target = new RSocketOutboundGateway(routeExpression); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Configure a {@link ClientRSocketConnector} for client side requests based on the connection |
| 48 | + * provided by the {@link ClientRSocketConnector#getRSocketRequester()}. |
| 49 | + * In case of server side, an {@link RSocketRequester} must be provided in the |
| 50 | + * {@link RSocketRequesterMethodArgumentResolver#RSOCKET_REQUESTER_HEADER} header of request message. |
| 51 | + * @param clientRSocketConnector the {@link ClientRSocketConnector} to use. |
| 52 | + * @return the spec |
| 53 | + * @see RSocketOutboundGateway#setClientRSocketConnector(ClientRSocketConnector) |
| 54 | + */ |
| 55 | + public RSocketOutboundGatewaySpec clientRSocketConnector(ClientRSocketConnector clientRSocketConnector) { |
| 56 | + this.target.setClientRSocketConnector(clientRSocketConnector); |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Configure a {@link RSocketOutboundGateway.Command} for RSocket request type. |
| 62 | + * @param command the {@link RSocketOutboundGateway.Command} to use. |
| 63 | + * @return the spec |
| 64 | + * @see RSocketOutboundGateway#setCommand(RSocketOutboundGateway.Command) |
| 65 | + */ |
| 66 | + public RSocketOutboundGatewaySpec command(RSocketOutboundGateway.Command command) { |
| 67 | + return command(new ValueExpression<>(command)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Configure a {@code Function} to evaluate a {@link RSocketOutboundGateway.Command} |
| 72 | + * for RSocket request type at runtime against a request message. |
| 73 | + * @param commandFunction the {@code Function} to use. |
| 74 | + * @param <P> the expected request message payload type. |
| 75 | + * @return the spec |
| 76 | + * @see RSocketOutboundGateway#setCommandExpression(Expression) |
| 77 | + */ |
| 78 | + public <P> RSocketOutboundGatewaySpec command(Function<Message<P>, ?> commandFunction) { |
| 79 | + return command(new FunctionExpression<>(commandFunction)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Configure a SpEL expression to evaluate a {@link RSocketOutboundGateway.Command} |
| 84 | + * for RSocket request type at runtime against a request message. |
| 85 | + * @param commandExpression the SpEL expression to use. |
| 86 | + * @return the spec |
| 87 | + * @see RSocketOutboundGateway#setCommandExpression(Expression) |
| 88 | + */ |
| 89 | + public RSocketOutboundGatewaySpec command(String commandExpression) { |
| 90 | + return command(PARSER.parseExpression(commandExpression)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Configure a SpEL expression to evaluate a {@link RSocketOutboundGateway.Command} |
| 95 | + * for RSocket request type at runtime against a request message. |
| 96 | + * @param commandExpression the SpEL expression to use. |
| 97 | + * @return the spec |
| 98 | + * @see RSocketOutboundGateway#setCommandExpression(Expression) |
| 99 | + */ |
| 100 | + public RSocketOutboundGatewaySpec command(Expression commandExpression) { |
| 101 | + this.target.setCommandExpression(commandExpression); |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Configure a type for a request {@link Publisher} elements. |
| 107 | + * @param publisherElementType the type of the request {@link Publisher} elements. |
| 108 | + * @return the spec |
| 109 | + * @see RSocketOutboundGateway#setPublisherElementType(Class) |
| 110 | + */ |
| 111 | + public RSocketOutboundGatewaySpec publisherElementType(Class<?> publisherElementType) { |
| 112 | + return publisherElementType(new ValueExpression<>(publisherElementType)); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Configure a {@code Function} to evaluate a request {@link Publisher} elements type at runtime against |
| 117 | + * a request message. |
| 118 | + * @param publisherElementTypeFunction the {@code Function} to evaluate a type for the request |
| 119 | + * {@link Publisher} elements. |
| 120 | + * @param <P> the expected request message payload type. |
| 121 | + * @return the spec |
| 122 | + * @see RSocketOutboundGateway#setPublisherElementTypeExpression(Expression) |
| 123 | + */ |
| 124 | + public <P> RSocketOutboundGatewaySpec publisherElementType(Function<Message<P>, ?> publisherElementTypeFunction) { |
| 125 | + return publisherElementType(new FunctionExpression<>(publisherElementTypeFunction)); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Configure a SpEL expression to evaluate a request {@link Publisher} elements type at runtime against |
| 130 | + * a request message. |
| 131 | + * @param publisherElementTypeExpression the expression to evaluate a type for the request |
| 132 | + * {@link Publisher} elements. |
| 133 | + * @return the spec |
| 134 | + * @see RSocketOutboundGateway#setPublisherElementTypeExpression(Expression) |
| 135 | + */ |
| 136 | + public RSocketOutboundGatewaySpec publisherElementType(String publisherElementTypeExpression) { |
| 137 | + return publisherElementType(PARSER.parseExpression(publisherElementTypeExpression)); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Configure a SpEL expression to evaluate a request {@link Publisher} elements type at runtime against |
| 142 | + * a request message. |
| 143 | + * @param publisherElementTypeExpression the expression to evaluate a type for the request |
| 144 | + * {@link Publisher} elements. |
| 145 | + * @return the spec |
| 146 | + * @see RSocketOutboundGateway#setPublisherElementTypeExpression(Expression) |
| 147 | + */ |
| 148 | + public RSocketOutboundGatewaySpec publisherElementType(Expression publisherElementTypeExpression) { |
| 149 | + this.target.setPublisherElementTypeExpression(publisherElementTypeExpression); |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Specify the expected response type for the RSocket response. |
| 155 | + * @param expectedResponseType The expected type. |
| 156 | + * @return the spec |
| 157 | + * @see RSocketOutboundGateway#setExpectedResponseType(Class) |
| 158 | + */ |
| 159 | + public RSocketOutboundGatewaySpec expectedResponseType(Class<?> expectedResponseType) { |
| 160 | + return expectedResponseType(new ValueExpression<>(expectedResponseType)); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Specify the {@code Function} to determine the type for the RSocket response. |
| 165 | + * @param expectedResponseTypeFunction The expected response type {@code Function}. |
| 166 | + * @param <P> the expected request message payload type. |
| 167 | + * @return the spec |
| 168 | + * @see RSocketOutboundGateway#setExpectedResponseTypeExpression(Expression) |
| 169 | + */ |
| 170 | + public <P> RSocketOutboundGatewaySpec expectedResponseType(Function<Message<P>, ?> expectedResponseTypeFunction) { |
| 171 | + return expectedResponseType(new FunctionExpression<>(expectedResponseTypeFunction)); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Specify the {@link Expression} to determine the type for the RSocket response. |
| 176 | + * @param expectedResponseTypeExpression The expected response type expression. |
| 177 | + * @return the spec |
| 178 | + * @see RSocketOutboundGateway#setExpectedResponseTypeExpression(Expression) |
| 179 | + */ |
| 180 | + public RSocketOutboundGatewaySpec expectedResponseType(String expectedResponseTypeExpression) { |
| 181 | + return expectedResponseType(PARSER.parseExpression(expectedResponseTypeExpression)); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Specify the {@link Expression} to determine the type for the RSocket response. |
| 186 | + * @param expectedResponseTypeExpression The expected response type expression. |
| 187 | + * @return the spec |
| 188 | + * @see RSocketOutboundGateway#setExpectedResponseTypeExpression(Expression) |
| 189 | + */ |
| 190 | + public RSocketOutboundGatewaySpec expectedResponseType(Expression expectedResponseTypeExpression) { |
| 191 | + this.target.setExpectedResponseTypeExpression(expectedResponseTypeExpression); |
| 192 | + return this; |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments