gnodet commented on code in PR #22358:
URL: https://github.com/apache/camel/pull/22358#discussion_r3049972842
##########
core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java:
##########
@@ -1303,4 +1307,36 @@ 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();
+ }
+
+ /**
Review Comment:
**Foundation: three new utility methods**
These three methods (`hasResponse`, `getResponse`, `setResponse`) centralize
all deprecated `Exchange.hasOut()`/`getOut()`/`setOut()` usage into a single
`@SuppressWarnings("deprecation")` call site.
Key semantic difference from the old API:
- `getResponse()` returns **null** when no OUT exists (unlike `getOut()`
which lazily creates an empty message)
- `hasResponse()` uses identity check (`getIn() != getMessage()`) which is
equivalent to `hasOut()`
- `setResponse()` is a direct wrapper around `setOut()`
##########
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());
Review Comment:
**Pattern B: `in.copy()` replaces `getOut()` + manual header propagation**
The old code did:
```java
old = exchange.getOut();
exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
```
`in.copy()` achieves both in one step — creates the OUT and propagates all
IN headers/body.
##########
core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java:
##########
@@ -442,7 +442,11 @@ private static void copyFromOutMessage(Exchange result,
Exchange source, boolean
*/
public static Message getResultMessage(Exchange exchange) {
if (exchange.getPattern().isOutCapable()) {
- return exchange.getOut();
+ // explicitly create response if none exists (preserves old
getOut() lazy-creation semantics)
+ if (!hasResponse(exchange)) {
Review Comment:
**Explicit response creation preserves `getOut()` lazy-creation semantics**
`getResultMessage()` is the one place where we intentionally want
lazy-creation behavior (callers expect a non-null writable message). We
preserve this by explicitly creating a response if none exists.
--
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]