This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 89bad05f66f CAMEL-17760: consolidate copy-exchange-if-capable logic 89bad05f66f is described below commit 89bad05f66f1303d24d5a8d64c6b0dae90e659a6 Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Mon Jul 25 12:15:33 2022 +0200 CAMEL-17760: consolidate copy-exchange-if-capable logic --- .../github/producer/ClosePullRequestProducer.java | 7 ++--- .../producer/PullRequestCommentProducer.java | 7 ++--- .../component/jira/producer/AddIssueProducer.java | 9 ++----- .../camel/component/pubnub/PubNubProducer.java | 9 +++---- .../component/twitter/timeline/UserProducer.java | 9 +++---- .../org/apache/camel/support/ExchangeHelper.java | 30 ++++++++++++++++++++++ 6 files changed, 43 insertions(+), 28 deletions(-) diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java index 671b0945cf7..62bfe4f0cec 100644 --- a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java +++ b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/ClosePullRequestProducer.java @@ -22,6 +22,7 @@ import org.apache.camel.Exchange; import org.apache.camel.component.github.GitHubConstants; import org.apache.camel.component.github.GitHubEndpoint; import org.apache.camel.spi.Registry; +import org.apache.camel.support.ExchangeHelper; import org.eclipse.egit.github.core.PullRequest; import org.eclipse.egit.github.core.service.PullRequestService; @@ -55,11 +56,7 @@ public class ClosePullRequestProducer extends AbstractGitHubProducer { pullRequest = pullRequestService.editPullRequest(getRepository(), pullRequest); // support InOut - if (exchange.getPattern().isOutCapable()) { - // copy the header of in message to the out message - exchange.getOut().copyFrom(exchange.getIn()); - exchange.getOut().setBody(pullRequest); - } + ExchangeHelper.setOutBodyPatternAware(exchange, pullRequest); } } diff --git a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestCommentProducer.java b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestCommentProducer.java index c5899692acf..9b4a53b7dae 100644 --- a/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestCommentProducer.java +++ b/components/camel-github/src/main/java/org/apache/camel/component/github/producer/PullRequestCommentProducer.java @@ -20,6 +20,7 @@ import org.apache.camel.Exchange; import org.apache.camel.component.github.GitHubConstants; import org.apache.camel.component.github.GitHubEndpoint; import org.apache.camel.spi.Registry; +import org.apache.camel.support.ExchangeHelper; import org.eclipse.egit.github.core.Comment; import org.eclipse.egit.github.core.service.IssueService; import org.eclipse.egit.github.core.service.PullRequestService; @@ -78,11 +79,7 @@ public class PullRequestCommentProducer extends AbstractGitHubProducer { } // support InOut - if (exchange.getPattern().isOutCapable()) { - // copy the header of in message to the out message - exchange.getOut().copyFrom(exchange.getIn()); - exchange.getOut().setBody(response); - } + ExchangeHelper.setOutBodyPatternAware(exchange, response); } } diff --git a/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AddIssueProducer.java b/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AddIssueProducer.java index 007c69cfb26..3c21bc365c2 100644 --- a/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AddIssueProducer.java +++ b/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AddIssueProducer.java @@ -29,6 +29,7 @@ import com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder; import org.apache.camel.Exchange; import org.apache.camel.component.jira.JiraEndpoint; import org.apache.camel.support.DefaultProducer; +import org.apache.camel.support.ExchangeHelper; import org.apache.camel.util.ObjectHelper; import static org.apache.camel.component.jira.JiraConstants.*; @@ -120,13 +121,7 @@ public class AddIssueProducer extends DefaultProducer { } // support InOut - if (exchange.getPattern().isOutCapable()) { - // copy the header of in message to the out message - exchange.getMessage().copyFrom(exchange.getIn()); - exchange.getMessage().setBody(issue); - } else { - exchange.getIn().setBody(issue); - } + ExchangeHelper.setInOutBodyPatternAware(exchange, issue); } } diff --git a/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubProducer.java b/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubProducer.java index 9bba652db26..3d6107d4421 100644 --- a/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubProducer.java +++ b/components/camel-pubnub/src/main/java/org/apache/camel/component/pubnub/PubNubProducer.java @@ -33,6 +33,7 @@ import org.apache.camel.CamelException; import org.apache.camel.Exchange; import org.apache.camel.RuntimeCamelException; import org.apache.camel.support.DefaultAsyncProducer; +import org.apache.camel.support.ExchangeHelper; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -229,12 +230,8 @@ public class PubNubProducer extends DefaultAsyncProducer { } throw new RuntimeCamelException(status.getErrorData().getThrowable()); } - if (exchange.getPattern().isOutCapable()) { - exchange.getOut().copyFrom(exchange.getIn()); - exchange.getOut().setBody(body); - } else { - exchange.getIn().setBody(body); - } + + ExchangeHelper.setInOutBodyPatternAware(exchange, body); // signal exchange completion callback.done(false); diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/timeline/UserProducer.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/timeline/UserProducer.java index 3f8a9a6132e..3d74ce9cd4a 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/timeline/UserProducer.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/timeline/UserProducer.java @@ -19,6 +19,7 @@ package org.apache.camel.component.twitter.timeline; import org.apache.camel.Exchange; import org.apache.camel.component.twitter.TwitterEndpoint; import org.apache.camel.support.DefaultProducer; +import org.apache.camel.support.ExchangeHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import twitter4j.Status; @@ -54,12 +55,10 @@ public class UserProducer extends DefaultProducer { * Support the InOut exchange pattern in order to provide access to * the unique identifier for the published tweet which is returned in the response * by the Twitter REST API: https://dev.twitter.com/docs/api/1/post/statuses/update + * + * here we just copy the header of in message to the out message */ - if (exchange.getPattern().isOutCapable()) { - // here we just copy the header of in message to the out message - exchange.getOut().copyFrom(exchange.getIn()); - exchange.getOut().setBody(response); - } + ExchangeHelper.setOutBodyPatternAware(exchange, response); } private Status updateStatus(StatusUpdate status) throws Exception { diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java index dc348d88348..8fa17546906 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java @@ -1060,4 +1060,34 @@ public final class ExchangeHelper { UnitOfWork uow = exchange.getUnitOfWork(); return uow != null ? uow.getRoute() : null; } + + /** + * Sets the body in message in the exchange taking the exchange pattern into consideration. If the pattern is out + * capable, then the body is set outbound message. Otherwise it is set on the inbound message. + * + * @param exchange the exchange containing the message to set the body + * @param body the body to set + */ + public static void setInOutBodyPatternAware(Exchange exchange, Object body) { + if (exchange.getPattern().isOutCapable()) { + exchange.getOut().copyFrom(exchange.getIn()); + exchange.getOut().setBody(body); + } else { + exchange.getIn().setBody(body); + } + } + + /** + * Sets the body in message in the exchange taking the exchange pattern into consideration. If the pattern is out + * capable, then the body is set outbound message. Otherwise nothing is done. + * + * @param exchange the exchange containing the message to set the body + * @param body the body to set + */ + public static void setOutBodyPatternAware(Exchange exchange, Object body) { + if (exchange.getPattern().isOutCapable()) { + exchange.getOut().copyFrom(exchange.getIn()); + exchange.getOut().setBody(body); + } + } }