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: five new utility methods**
These five methods (`hasResponse`, `getResponse`, `setResponse`,
`createResponse`, `createResponseFromInput`) 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 differences from `getOut()`:
- `getResponse()` returns `null` when no response exists (no lazy creation
side-effect)
- `createResponse()` creates a new empty `DefaultMessage` — for header
isolation
- `createResponseFromInput()` creates a response by copying the IN message —
for header propagation
_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]