gnodet commented on code in PR #22358:
URL: https://github.com/apache/camel/pull/22358#discussion_r3015630024


##########
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-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-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-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-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-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-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-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-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-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.



-- 
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]

Reply via email to