|
| 1 | +/* |
| 2 | + * Copyright 2022 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.camel.outbound; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | + |
| 22 | +import org.apache.camel.CamelExecutionException; |
| 23 | +import org.apache.camel.Endpoint; |
| 24 | +import org.apache.camel.Exchange; |
| 25 | +import org.apache.camel.ExchangePattern; |
| 26 | +import org.apache.camel.ProducerTemplate; |
| 27 | + |
| 28 | +import org.springframework.expression.Expression; |
| 29 | +import org.springframework.expression.common.LiteralExpression; |
| 30 | +import org.springframework.expression.spel.support.StandardEvaluationContext; |
| 31 | +import org.springframework.integration.camel.support.CamelHeaderMapper; |
| 32 | +import org.springframework.integration.expression.ExpressionUtils; |
| 33 | +import org.springframework.integration.expression.ValueExpression; |
| 34 | +import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; |
| 35 | +import org.springframework.integration.mapping.HeaderMapper; |
| 36 | +import org.springframework.integration.support.AbstractIntegrationMessageBuilder; |
| 37 | +import org.springframework.lang.Nullable; |
| 38 | +import org.springframework.messaging.Message; |
| 39 | +import org.springframework.util.Assert; |
| 40 | +import org.springframework.util.StringUtils; |
| 41 | + |
| 42 | +/** |
| 43 | + * A {@link org.springframework.messaging.MessageHandler} for calling Apache Camel route |
| 44 | + * and produce (optionally) a reply. |
| 45 | + * <p> |
| 46 | + * In the async mode, the {@link ProducerTemplate#asyncSend(Endpoint, Exchange)} is used. |
| 47 | + * <p> |
| 48 | + * The request-reply behavior can be controlled via {@link ExchangePattern} configuration |
| 49 | + * or per message. By default, this handler works in an {@link ExchangePattern#InOnly} mode. |
| 50 | + * <p> |
| 51 | + * A default "mapping all headers" between Spring Integration and Apache Camel messages behavior |
| 52 | + * can be customized via {@link #setHeaderMapper(HeaderMapper)} option. |
| 53 | + * <p> |
| 54 | + * The target Apache Camel endpoint to call can be determined by the {@link #endpointUriExpression}. |
| 55 | + * By default, a {@link ProducerTemplate#getDefaultEndpoint()} is used. |
| 56 | + * |
| 57 | + * @author Artem Bilan |
| 58 | + * |
| 59 | + * @since 6.0 |
| 60 | + * |
| 61 | + * @see CamelHeaderMapper |
| 62 | + */ |
| 63 | +public class CamelMessageHandler extends AbstractReplyProducingMessageHandler { |
| 64 | + |
| 65 | + private final ProducerTemplate producerTemplate; |
| 66 | + |
| 67 | + private Expression exchangePatternExpression = new ValueExpression<>(ExchangePattern.InOnly); |
| 68 | + |
| 69 | + @Nullable |
| 70 | + private Expression endpointUriExpression; |
| 71 | + |
| 72 | + private HeaderMapper<org.apache.camel.Message> headerMapper = new CamelHeaderMapper(); |
| 73 | + |
| 74 | + @Nullable |
| 75 | + private Expression exchangePropertiesExpression; |
| 76 | + |
| 77 | + private StandardEvaluationContext evaluationContext; |
| 78 | + |
| 79 | + public CamelMessageHandler(ProducerTemplate producerTemplate) { |
| 80 | + Assert.notNull(producerTemplate, "'producerTemplate' must not be null"); |
| 81 | + this.producerTemplate = producerTemplate; |
| 82 | + } |
| 83 | + |
| 84 | + public void setEndpointUri(String endpointUri) { |
| 85 | + Assert.hasText(endpointUri, "'endpointUri' must not be empty"); |
| 86 | + setEndpointUriExpression(new LiteralExpression(endpointUri)); |
| 87 | + } |
| 88 | + |
| 89 | + public void setEndpointUriExpression(Expression endpointUriExpression) { |
| 90 | + Assert.notNull(endpointUriExpression, "'endpointUriExpression' must not be null"); |
| 91 | + this.endpointUriExpression = endpointUriExpression; |
| 92 | + } |
| 93 | + |
| 94 | + public void setExchangePattern(ExchangePattern exchangePattern) { |
| 95 | + Assert.notNull(exchangePattern, "'exchangePattern' must not be null"); |
| 96 | + setExchangePatternExpression(new ValueExpression<>(exchangePattern)); |
| 97 | + } |
| 98 | + |
| 99 | + public void setExchangePatternExpression(Expression exchangePatternExpression) { |
| 100 | + Assert.notNull(exchangePatternExpression, "'exchangePatternExpression' must not be null"); |
| 101 | + this.exchangePatternExpression = exchangePatternExpression; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Set a {@link HeaderMapper} to map request message headers into Apache Camel message headers and |
| 106 | + * back if request-reply exchange pattern is used. |
| 107 | + * @param headerMapper the {@link HeaderMapper} to use. |
| 108 | + */ |
| 109 | + public void setHeaderMapper(HeaderMapper<org.apache.camel.Message> headerMapper) { |
| 110 | + Assert.notNull(headerMapper, "'headerMapper' must not be null"); |
| 111 | + this.headerMapper = headerMapper; |
| 112 | + } |
| 113 | + |
| 114 | + public void setExchangeProperties(Map<String, Object> exchangeProperties) { |
| 115 | + Assert.notNull(exchangeProperties, "'exchangeProperties' must not be null"); |
| 116 | + setExchangePropertiesExpression(new ValueExpression<>(exchangeProperties)); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Set a SpEL expression to evaluate {@link org.apache.camel.Exchange} properties as a {@link Map}. |
| 121 | + * @param exchangePropertiesExpression the expression for exchange properties. |
| 122 | + */ |
| 123 | + public void setExchangePropertiesExpression(Expression exchangePropertiesExpression) { |
| 124 | + this.exchangePropertiesExpression = exchangePropertiesExpression; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + protected final void doInit() { |
| 129 | + this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(getBeanFactory()); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + protected Object handleRequestMessage(Message<?> requestMessage) { |
| 134 | + ExchangePattern exchangePattern = |
| 135 | + this.exchangePatternExpression.getValue(this.evaluationContext, requestMessage, ExchangePattern.class); |
| 136 | + |
| 137 | + Assert.notNull(exchangePattern, "'exchangePatternExpression' must not evaluate to null"); |
| 138 | + |
| 139 | + Endpoint endpoint = resolveEndpoint(requestMessage); |
| 140 | + Exchange exchange = prepareInExchange(endpoint, exchangePattern, requestMessage); |
| 141 | + |
| 142 | + if (isAsync()) { |
| 143 | + CompletableFuture<Exchange> result = this.producerTemplate.asyncSend(endpoint, exchange); |
| 144 | + return result.thenApply(resultExchange -> buildReply(exchangePattern, resultExchange)); |
| 145 | + } |
| 146 | + else { |
| 147 | + Exchange result = this.producerTemplate.send(endpoint, exchange); |
| 148 | + return buildReply(exchangePattern, result); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private Endpoint resolveEndpoint(Message<?> requestMessage) { |
| 153 | + String endpointUri = |
| 154 | + this.endpointUriExpression != null |
| 155 | + ? this.endpointUriExpression.getValue(this.evaluationContext, requestMessage, String.class) |
| 156 | + : null; |
| 157 | + |
| 158 | + if (StringUtils.hasText(endpointUri)) { |
| 159 | + return this.producerTemplate.getCamelContext().getEndpoint(endpointUri); |
| 160 | + } |
| 161 | + else { |
| 162 | + return this.producerTemplate.getDefaultEndpoint(); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @SuppressWarnings("unchecked") |
| 167 | + private Exchange prepareInExchange(Endpoint endpoint, ExchangePattern exchangePattern, Message<?> requestMessage) { |
| 168 | + Exchange exchange = endpoint.createExchange(exchangePattern); |
| 169 | + |
| 170 | + Map<String, Object> exchangeProperties = |
| 171 | + this.exchangePropertiesExpression != null |
| 172 | + ? this.exchangePropertiesExpression.getValue(this.evaluationContext, requestMessage, Map.class) |
| 173 | + : null; |
| 174 | + |
| 175 | + if (exchangeProperties != null) { |
| 176 | + for (Map.Entry<String, Object> property : exchangeProperties.entrySet()) { |
| 177 | + exchange.setProperty(property.getKey(), property.getValue()); |
| 178 | + } |
| 179 | + } |
| 180 | + org.apache.camel.Message in = exchange.getIn(); |
| 181 | + this.headerMapper.fromHeaders(requestMessage.getHeaders(), in); |
| 182 | + in.setBody(requestMessage.getPayload()); |
| 183 | + return exchange; |
| 184 | + } |
| 185 | + |
| 186 | + @Nullable |
| 187 | + private AbstractIntegrationMessageBuilder<?> buildReply(ExchangePattern exchangePattern, Exchange result) { |
| 188 | + if (result.isFailed()) { |
| 189 | + throw CamelExecutionException.wrapCamelExecutionException(result, result.getException()); |
| 190 | + } |
| 191 | + if (exchangePattern.isOutCapable()) { |
| 192 | + org.apache.camel.Message out = result.getMessage(); |
| 193 | + return getMessageBuilderFactory() |
| 194 | + .withPayload(out.getBody()) |
| 195 | + .copyHeaders(this.headerMapper.toHeaders(out)); |
| 196 | + } |
| 197 | + else { |
| 198 | + return null; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments