gnodet commented on code in PR #22358:
URL: https://github.com/apache/camel/pull/22358#discussion_r3050512087
##########
core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java:
##########
@@ -1303,4 +1307,52 @@ public static <T> T getBodyAndResetStreamCache(Exchange
exchange, Class<T> type)
return exchange.getMessage().getBody(type);
}
+ /**
+ * Returns true if a response message has been explicitly set on this
exchange.
+ * <p>
+ * This is a non-deprecated equivalent of {@link Exchange#hasOut()},
provided as a utility until a proper
+ * {@code hasResponse()} method is added to the {@link Exchange} API.
+ */
+ public static boolean hasResponse(Exchange exchange) {
+ return exchange.getIn() != exchange.getMessage();
+ }
+
+ /**
+ * Returns the response message if one has been explicitly set, null
otherwise. Unlike the deprecated
+ * {@link Exchange#getOut()}, this does NOT lazily create an empty message.
+ * <p>
+ * This is provided as a utility until a proper {@code getResponse()}
method is added to the {@link Exchange} API.
+ */
+ public static Message getResponse(Exchange exchange) {
+ return hasResponse(exchange) ? exchange.getMessage() : null;
+ }
+
+ /**
+ * Sets the response message on this exchange. Unlike {@link
Exchange#getOut()} which lazily creates an empty
+ * message on read, this makes response creation explicit.
+ * <p>
+ * This is a non-deprecated equivalent of {@link
Exchange#setOut(Message)}, provided as a utility until a proper
+ * {@code setResponse(Message)} method is added to the {@link Exchange}
API.
+ */
+ @SuppressWarnings("deprecation")
+ public static void setResponse(Exchange exchange, Message response) {
+ exchange.setOut(response);
+ }
+
+ /**
+ * Creates a new empty response message on the exchange and returns it.
This is the non-deprecated equivalent of the
+ * lazy-creation side effect of {@link Exchange#getOut()}.
+ * <p>
+ * Typical usage:
+ *
+ * <pre>
+ * Message response = ExchangeHelper.createResponse(exchange);
+ * response.setBody(result);
+ * </pre>
+ */
+ public static Message createResponse(Exchange exchange) {
+ setResponse(exchange, new DefaultMessage(exchange.getContext()));
+ return exchange.getMessage();
+ }
Review Comment:
**Foundation: four new utility methods**
These four methods (`hasResponse`, `getResponse`, `setResponse`,
`createResponse`) are the non-deprecated equivalents of `hasOut()`, `getOut()`,
`setOut()`. They live in `ExchangeHelper` as a stepping stone — the plan is to
eventually promote them to the `Exchange` API itself.
Key semantic difference from `getOut()`: `getResponse()` returns `null` when
no response exists (no lazy creation side-effect). For code that needs the old
lazy-creation behavior, use `createResponse()`.
_Claude Code on behalf of Guillaume Nodet_
##########
components/camel-bean/src/main/java/org/apache/camel/component/bean/MethodInfo.java:
##########
@@ -395,16 +395,12 @@ public AccessibleObject getStaticPart() {
private void fillResult(Exchange exchange, Object result) {
LOG.trace("Setting bean invocation result : {}", result);
- // the bean component forces OUT if the MEP is OUT capable
- boolean out = exchange.hasOut() ||
ExchangeHelper.isOutCapable(exchange);
- Message old;
- if (out) {
- old = exchange.getOut();
- // propagate headers
-
exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
- } else {
- old = exchange.getIn();
+ // the bean component forces OUT if the MEP is OUT capable;
+ // in.copy() both creates the OUT and propagates headers in one step
+ if (ExchangeHelper.isOutCapable(exchange) &&
!ExchangeHelper.hasResponse(exchange)) {
+ ExchangeHelper.setResponse(exchange, exchange.getIn().copy());
}
+ Message old = exchange.getMessage();
Review Comment:
**Pattern B: `in.copy()` replaces `getOut()` + manual header propagation**
The old code did:
```java
boolean out = exchange.hasOut() || ExchangeHelper.isOutCapable(exchange);
if (out) {
old = exchange.getOut(); // lazily creates empty OUT
exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
// manual propagation
} else {
old = exchange.getIn();
}
```
Now `in.copy()` both creates the OUT and propagates headers in one step.
The guard `!hasResponse(exchange)` avoids overwriting if an OUT already exists
from an earlier processor in the chain.
_Claude Code on behalf of Guillaume Nodet_
--
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]