gnodet commented on code in PR #22358:
URL: https://github.com/apache/camel/pull/22358#discussion_r3015630055
##########
components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXProcessor.java:
##########
@@ -62,12 +61,7 @@ public void process(Exchange exchange) throws Exception {
reader.setContentHandler(handler);
// InputSource is ignored anyway
reader.parse((InputSource) null);
- if (ExchangeHelper.isOutCapable(exchange)) {
- exchange.getOut().setHeaders(exchange.getIn().getHeaders());
- exchange.getOut().setBody(handler);
- } else {
- exchange.getIn().setBody(handler);
- }
+ exchange.getMessage().setBody(handler);
Review Comment:
The old code also did
`exchange.getOut().setHeaders(exchange.getIn().getHeaders())` in the OutCapable
branch, which is redundant since `getOut()` already copies headers from IN.
With `getMessage()` we just set the body on the current message.
##########
components/camel-fop/src/main/java/org/apache/camel/component/fop/FopProducer.java:
##########
@@ -64,10 +64,8 @@ public void process(Exchange exchange) throws Exception {
Source src = exchange.getIn().getBody(StreamSource.class);
OutputStream out = transform(userAgent, outputFormat, src);
- exchange.getOut().setBody(out);
-
- // propagate headers
- exchange.getOut().setHeaders(headers);
+ exchange.getMessage().setBody(out);
+ exchange.getMessage().setHeaders(headers);
Review Comment:
**Pattern: removed redundant header propagation**
The old code saved `exchange.getIn().getHeaders()` into a local var, then
did `exchange.getOut().setHeaders(headers)` to propagate them. Since
`getMessage()` returns the IN message (which already has those headers), the
explicit header propagation is unnecessary. We just set the new body and
restore headers (which were captured earlier before the body was consumed).
##########
components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConsumer.java:
##########
@@ -466,12 +460,8 @@ public void messageReceived(IoSession session, Object
object) throws Exception {
}
// should session be closed after complete?
- Boolean close;
- if (ExchangeHelper.isOutCapable(exchange)) {
- close =
exchange.getOut().getHeader(MinaConstants.MINA_CLOSE_SESSION_WHEN_COMPLETE,
Boolean.class);
- } else {
- close =
exchange.getIn().getHeader(MinaConstants.MINA_CLOSE_SESSION_WHEN_COMPLETE,
Boolean.class);
- }
+ Boolean close
Review Comment:
Same pattern: `isOutCapable ? getOut().getHeader() : getIn().getHeader()` →
`getMessage().getHeader()`. The `getMessage()` API handles this conditional
automatically.
##########
components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaPayloadHelper.java:
##########
@@ -65,9 +65,8 @@ public static void setOut(Exchange exchange, Object payload) {
if (payload instanceof DefaultExchangeHolder) {
DefaultExchangeHolder.unmarshal(exchange, (DefaultExchangeHolder)
payload);
} else {
- // normal transfer using the body only and preserve the headers
- exchange.getOut().setHeaders(exchange.getIn().getHeaders());
- exchange.getOut().setBody(payload);
+ // normal transfer using the body only
+ exchange.getMessage().setBody(payload);
Review Comment:
The old `setOut()` method created an OUT message, manually copied headers
from IN, then set the body. With `getMessage()`, we work on the current message
(IN) which already has its headers — just set the body.
##########
components/camel-reactive-streams/src/main/java/org/apache/camel/component/reactive/streams/util/UnwrapStreamProcessor.java:
##########
@@ -79,7 +79,9 @@ private void addData() {
Exchange copy = (Exchange) body;
exchange.setException(copy.getException());
exchange.setIn(copy.getIn());
- if (copy.hasOut()) {
+ @SuppressWarnings("deprecation")
Review Comment:
**Pattern: exchange reconstruction — kept deprecated API with
`@SuppressWarnings`**
This code reconstructs a full exchange by copying IN, OUT, properties, and
exception from a received exchange. This inherently requires the in/out
distinction: if the source exchange has a separate OUT message, it must be
transferred as-is to preserve the exchange state. This cannot be safely
replaced with `getMessage()` without losing the OUT message semantics. The
`@SuppressWarnings("deprecation")` annotation is the correct approach here.
##########
components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredProducer.java:
##########
@@ -80,24 +80,19 @@ public void execute(StatementWrapper ps) throws
SQLException, DataAccessExceptio
exchange.getIn().setHeader(SqlStoredConstants.SQL_STORED_UPDATE_COUNT, total);
} else {
Object result = ps.executeStatement();
- // preserve headers first, so we can override the
SQL_ROW_COUNT and SQL_UPDATE_COUNT headers
- // if statement returns them
-
exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
if (result != null) {
- if (getEndpoint().isNoop()) {
-
exchange.getOut().setBody(exchange.getIn().getBody());
- } else if (getEndpoint().getOutputHeader() != null) {
-
exchange.getOut().setBody(exchange.getIn().getBody());
-
exchange.getOut().setHeader(getEndpoint().getOutputHeader(), result);
- } else {
- exchange.getOut().setBody(result);
+ if (!getEndpoint().isNoop()) {
Review Comment:
**Refactored logic with redundant header copy removed**
The old code: (1) copied IN headers to OUT
(`getOut().getHeaders().putAll(getIn().getHeaders())`), (2) for noop, set body
from IN to OUT, (3) for outputHeader, set body from IN + set header, (4)
otherwise set result as body. Since `getMessage()` returns IN (which already
has all headers and body), for `noop=true` we now do nothing (body stays
as-is), and for `outputHeader` we just set the header without re-setting the
body.
##########
components/camel-ai/camel-chatscript/src/main/java/org/apache/camel/component/chatscript/ChatScriptProducer.java:
##########
@@ -50,7 +50,7 @@ public void process(Exchange exchange) throws Exception {
inputMessage.setBotName(endpoint.getBotName());
String response = this.endpoint.getBot().sendChat(inputMessage);
inputMessage.setReply(response);
- exchange.getOut().setBody(inputMessage);
+ exchange.getMessage().setBody(inputMessage);
Review Comment:
**Pattern: simple `getOut().setBody()` → `getMessage().setBody()`**
`getOut()` lazily creates a new OUT message (copying headers from IN).
`getMessage()` returns the current message (OUT if one exists, otherwise IN).
Since no OUT was previously created here, `getOut()` was creating an OUT just
to set a body on it — `getMessage()` achieves the same result without the
unnecessary OUT message creation.
Same pattern applies to: `BeanIODataFormat`, `BindyFixedLengthDataFormat`,
`PGPKeyAccessDataFormat`, `HL7DataFormat`, `JcrProducer`,
`Soap11/12DataFormatAdapter`, `SoapDataFormat`, `RocketMQReplyManagerSupport`,
`SinkConverter`.
##########
components/camel-mock/src/main/java/org/apache/camel/component/mock/MockExpressionClause.java:
##########
@@ -125,7 +125,7 @@ public T outMessage(final Function<Message, Object>
function) {
return delegate.expression(new ExpressionAdapter() {
@Override
public Object evaluate(Exchange exchange) {
- return function.apply(exchange.getOut());
+ return function.apply(exchange.getMessage());
Review Comment:
These methods (`outMessage()`, `outBody()`, `outBodyAs()`) use `getOut()` to
access the response message. `getMessage()` is the correct replacement — it
returns the response (OUT) if one exists, otherwise the request (IN). All 5
`getOut()` calls in this file follow this same pattern.
##########
components/camel-xpath/src/main/java/org/apache/camel/language/xpath/XPathBuilder.java:
##########
@@ -679,8 +679,8 @@ private XPathFunction createOutBodyFunction() {
return new XPathFunction() {
@SuppressWarnings("rawtypes")
public Object evaluate(List list) throws XPathFunctionException {
- if (exchange.get() != null && exchange.get().hasOut()) {
- return exchange.get().getOut().getBody();
+ if (exchange.get() != null) {
Review Comment:
Same `hasOut()` guard removal pattern. The old code returned `null` when no
OUT existed; now `getMessage()` returns IN, and `getBody()` on IN is valid. The
XPath function `$out:body` now returns the current message body rather than
null — which is the correct modern behavior since the in/out distinction is
deprecated.
##########
components/camel-jooq/src/main/java/org/apache/camel/component/jooq/JooqProducer.java:
##########
@@ -71,8 +70,7 @@ public void process(Exchange exchange) {
result = context.fetch(querySQL);
}
- Message target = exchange.getPattern().isOutCapable() ?
exchange.getOut() : exchange.getIn();
- target.setBody(result);
+ exchange.getMessage().setBody(result);
Review Comment:
**Pattern: `isOutCapable ? getOut() : getIn()` → `getMessage()`**
`getMessage()` is the direct replacement for this conditional pattern — it
returns the OUT message if one exists, otherwise the IN message. This is
exactly what the old conditional was doing manually.
Same pattern in: `StAXProcessor`, `SpringWebserviceProducer`.
##########
components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbProducer.java:
##########
@@ -99,13 +98,11 @@ private void doQuery(Exchange exchange, String
dataBaseName) {
String query = calculateQuery(exchange);
Query influxdbQuery = new Query(query, dataBaseName);
QueryResult resultSet = connection.query(influxdbQuery);
- MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
exchange.getMessage().setBody(resultSet);
Review Comment:
**Pattern: removed `MessageHelper.copyHeaders(getIn(), getOut(), true)`**
`getOut()` already copies headers from IN when the OUT message is lazily
created. The explicit `MessageHelper.copyHeaders` was redundant. With
`getMessage()`, we work directly on the IN message which already has all
headers.
Same pattern in: `InfluxDb2Producer`.
##########
components/camel-xmlsecurity/src/main/java/org/apache/camel/component/xmlsecurity/processor/XmlSignerProcessor.java:
##########
@@ -196,30 +196,21 @@ public XmlSignerConfiguration getConfiguration() {
@Override
public void process(Exchange exchange) throws Exception {
- try {
- LOG.debug("XML signature generation started using algorithm {} and
canonicalization method {}", getConfiguration()
- .getSignatureAlgorithm(),
getConfiguration().getCanonicalizationMethod().getAlgorithm());
-
- // lets setup the out message before we invoke the signing
- // so that it can mutate it if necessary
- Message out = exchange.getOut();
- out.copyFrom(exchange.getIn());
-
- Document outputDoc = sign(out);
-
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- XmlSignatureHelper.transformNonTextNodeToOutputStream(outputDoc,
outStream, omitXmlDeclaration(out),
- getConfiguration().getOutputXmlEncoding());
- byte[] data = outStream.toByteArray();
- out.setBody(data);
- setOutputEncodingToMessageHeader(out);
- clearMessageHeaders(out);
- LOG.debug("XML signature generation finished");
- } catch (Exception e) {
- // remove OUT message, as an exception occurred
- exchange.setOut(null);
- throw e;
- }
+ LOG.debug("XML signature generation started using algorithm {} and
canonicalization method {}", getConfiguration()
+ .getSignatureAlgorithm(),
getConfiguration().getCanonicalizationMethod().getAlgorithm());
+
+ Message out = exchange.getMessage();
Review Comment:
**Pattern: removed try/catch with `exchange.setOut(null)` on error**
The old code created an OUT message upfront, copied IN into it, then worked
on it. On exception, it set `exchange.setOut(null)` to clean up the
partially-modified OUT. With `getMessage()`, we work directly on the current
message — if an exception occurs, it propagates naturally without needing
cleanup of a dangling OUT message. The `sign(out)` method reads from the
message but only sets the body at the end (line 211), so an exception leaves
the message in its original state.
##########
components/camel-xmlsecurity/src/main/java/org/apache/camel/component/xmlsecurity/processor/XmlVerifierProcessor.java:
##########
@@ -80,16 +80,9 @@ public XmlVerifierConfiguration getConfiguration() {
public void process(Exchange exchange) throws Exception {
InputStream stream =
exchange.getIn().getMandatoryBody(InputStream.class);
try {
- // lets setup the out message before we invoke the signing
- // so that it can mutate it if necessary
- Message out = exchange.getOut();
- out.copyFrom(exchange.getIn());
+ Message out = exchange.getMessage();
Review Comment:
Same pattern as `XmlSignerProcessor`: removed `getOut()` + `copyFrom(in)` +
try/catch with `setOut(null)`. The `verify()` method modifies the message
in-place, so working on `getMessage()` directly is safe. The catch block that
cleared OUT on error is no longer needed.
##########
components/camel-spring-parent/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceProducer.java:
##########
@@ -107,13 +106,8 @@ public void doWithMessage(WebServiceMessage
requestMessage) throws IOException,
@Override
public void doWithMessage(WebServiceMessage
responseMessage) throws IOException, TransformerException {
SoapMessage soapMessage = (SoapMessage)
responseMessage;
- if (ExchangeHelper.isOutCapable(exchange)) {
-
exchange.getOut().copyFromWithNewBody(exchange.getIn(),
soapMessage.getPayloadSource());
-
populateHeaderAndAttachmentsFromResponse(exchange.getOut(AttachmentMessage.class),
soapMessage);
- } else {
-
exchange.getIn().setBody(soapMessage.getPayloadSource());
-
populateHeaderAndAttachmentsFromResponse(exchange.getIn(AttachmentMessage.class),
soapMessage);
- }
+
exchange.getMessage().setBody(soapMessage.getPayloadSource());
Review Comment:
The old code had two branches: OutCapable →
`getOut().copyFromWithNewBody(getIn(), payload)` + populate headers/attachments
on OUT, else → `getIn().setBody(payload)` + populate on IN. Since
`getMessage()` returns the current message, we just set the body and populate
headers/attachments on it — achieving the same result regardless of exchange
pattern.
##########
components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaProducer.java:
##########
@@ -184,12 +183,8 @@ protected void doProcess(Exchange exchange) throws
Exception {
maybeDisconnectOnTimeout();
throw new ExchangeTimedOutException(exchange, timeout);
} else {
- // set the result on either IN or OUT on the original exchange
depending on its pattern
- if (ExchangeHelper.isOutCapable(exchange)) {
- MinaPayloadHelper.setOut(exchange, handler.getMessage());
- } else {
- MinaPayloadHelper.setIn(exchange, handler.getMessage());
- }
+ // set the result on the exchange
+ MinaPayloadHelper.setIn(exchange, handler.getMessage());
Review Comment:
The old code conditionally called `setOut()` or `setIn()` based on
`isOutCapable`. Since `setIn()` sets the body on the current IN message, and
the exchange will route the result correctly regardless, we can always use
`setIn()` here.
##########
components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java:
##########
@@ -72,13 +72,10 @@ public void process(Exchange exchange) throws Exception {
}
SshResult result = SshHelper.sendExecCommand(headers, command,
endpoint, client);
- // propagate headers
- exchange.getOut().getHeaders().putAll(in.getHeaders());
-
// store result
- exchange.getOut().setBody(result.getStdout());
- exchange.getOut().setHeader(SshConstants.EXIT_VALUE,
result.getExitValue());
- exchange.getOut().setHeader(SshConstants.STDERR,
result.getStderr());
+ exchange.getMessage().setBody(result.getStdout());
Review Comment:
**Pattern: removed explicit `getOut().getHeaders().putAll(in.getHeaders())`**
The old code created an OUT message via `getOut()` (which already copies
headers from IN), then redundantly copied headers again with `putAll`. With
`getMessage()`, headers are already present on the IN message. Same pattern in
`SqlStoredProducer`.
##########
components/camel-crypto/src/main/java/org/apache/camel/component/crypto/processor/SigningProcessor.java:
##########
@@ -43,9 +43,7 @@ public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
clearMessageHeaders(in);
- Message out = exchange.getOut();
- out.copyFrom(in);
- out.setHeader(config.getSignatureHeaderName(), new
Base64().encode(signature));
+ exchange.getMessage().setHeader(config.getSignatureHeaderName(), new
Base64().encode(signature));
Review Comment:
**Pattern: removed redundant `getOut()` + `copyFrom(in)` + header set**
The old code did:
```java
Message out = exchange.getOut(); // creates OUT, copies headers from IN
out.copyFrom(in); // copies body+headers from IN to OUT again
out.setHeader(..., signature); // sets the signature header
```
`getMessage()` returns IN directly (since no OUT exists yet), which already
has all the headers and body. We just need to set the signature header. The
`copyFrom` was redundant because `getOut()` already copies headers, and then
`copyFrom` copies everything again.
##########
components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConsumer.java:
##########
@@ -440,20 +439,15 @@ public void messageReceived(IoSession session, Object
object) throws Exception {
// If there's a response to send, send it.
//
boolean disconnect =
getEndpoint().getConfiguration().isDisconnect();
Review Comment:
**Pattern: removed `hasOut()` guard before reading response**
The old code checked `exchange.hasOut()` to decide whether to read from OUT
or IN. Since at this point in the consumer flow the exchange has been processed
and `getMessage()` returns the current result message (OUT if the processor set
one, IN otherwise), we can just use `getIn()` via the helper — which reads the
current message state.
##########
components/camel-xpath/src/main/java/org/apache/camel/language/xpath/MessageVariableResolver.java:
##########
@@ -85,12 +85,10 @@ public Object resolveVariable(QName name) {
answer = in.getBody();
}
} else if (uri.equals(OUT_NAMESPACE)) {
- if (exchange.get().hasOut()) {
- Message out = exchange.get().getOut();
- answer = out.getHeader(localPart);
- if (answer == null && localPart.equals("body")) {
- answer = out.getBody();
- }
+ Message out = exchange.get().getMessage();
Review Comment:
**Pattern: removed `hasOut()` guard**
The old code checked `exchange.hasOut()` and only read from OUT if it
existed. With `getMessage()`, we get the current message (OUT if it exists, IN
otherwise) — so the guard is unnecessary. The behavior is equivalent: if OUT
exists, we read from it; if not, we read from IN.
##########
components/camel-mvel/src/main/java/org/apache/camel/language/mvel/RootObject.java:
##########
@@ -55,7 +55,7 @@ public Message getRequest() {
@Deprecated
public Message getResponse() {
- return exchange.getOut();
+ return exchange.getMessage();
Review Comment:
**Pattern: deprecated `getResponse()` method**
The `getResponse()` method is itself `@Deprecated`. Using `getMessage()`
instead of `getOut()` is appropriate since both the method and the underlying
API are deprecated. Same change in `ognl/RootObject` and `spel/RootObject`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]