Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

private ChatScriptMessage buildMessage(Object body) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private static Object processExchange(AvroConsumer consumer, Protocol.Message me
}

if (ExchangeHelper.isOutCapable(exchange)) {
response = exchange.getOut().getBody();
response = exchange.getMessage().getBody();
} else {
response = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ public boolean process(final Exchange exchange, final AsyncCallback callback) {
public void handleResult(Object result) {
// got result from avro, so set it on the exchange and invoke the callback
try {
// propagate headers
exchange.getOut().setHeaders(exchange.getIn().getHeaders());
exchange.getOut().setBody(result);
exchange.getMessage().setBody(result);
} finally {
callback.done(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ private String readImage(final Exchange exchange, final InputStream stream) thro
final Result result = reader.decode(bitmap, readerHintMap);

// write the found barcode format into the header
exchange.getOut().setHeader(Barcode.BARCODE_FORMAT, result.getBarcodeFormat());
exchange.getMessage().setHeader(Barcode.BARCODE_FORMAT, result.getBarcodeFormat());

if (result.getResultMetadata() != null) {
result.getResultMetadata().forEach((k, v) -> {
exchange.getOut().setHeader(k.toString(), v);
exchange.getMessage().setHeader(k.toString(), v);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Comment on lines 395 to +403
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern B: in.copy() replaces getOut() + manual header propagation

The old code did:

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


// create a new message container, so we do not drag specialized message objects along
// but that is only needed if the old message is a specialized message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.camel.ExchangePattern;
import org.apache.camel.Expression;
import org.apache.camel.ExpressionIllegalSyntaxException;
import org.apache.camel.Message;
import org.apache.camel.NoSuchBeanException;
import org.apache.camel.Predicate;
import org.apache.camel.RuntimeCamelException;
Expand Down Expand Up @@ -368,15 +369,17 @@ private static Object invokeBean(BeanHolder beanHolder, String beanName, String
// force to use InOut to retrieve the result on the OUT message
resultExchange.setPattern(ExchangePattern.InOut);
processor.process(resultExchange);
// the response is always stored in OUT
result = resultExchange.hasOut() ? resultExchange.getOut().getBody() : null;
// the bean component creates an OUT for non-void methods on OUT-capable exchanges,
// so getResponse() returns null for void methods (no result) and the OUT for non-void
Message response = ExchangeHelper.getResponse(resultExchange);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern E: getResponse() returns null for void methods

The bean component only creates an OUT for non-void methods on OUT-capable exchanges. getResponse() returns null when no OUT exists (void method case), unlike getOut() which would have lazily created an empty one. The null check below handles this correctly.

result = response != null ? response.getBody() : null;

// propagate properties and headers from result
if (resultExchange.hasProperties()) {
exchange.getProperties().putAll(resultExchange.getProperties());
}
if (resultExchange.hasOut() && resultExchange.getOut().hasHeaders()) {
exchange.getIn().getHeaders().putAll(resultExchange.getOut().getHeaders());
if (response != null && response.hasHeaders()) {
exchange.getIn().getHeaders().putAll(response.getHeaders());
}

// propagate exceptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private List<Object> readModels(Exchange exchange, InputStream stream) throws Ex
Object readObject;
while ((readObject = in.read()) != null) {
if (readObject instanceof BeanIOHeader beanioheader) {
exchange.getOut().getHeaders().putAll(beanioheader.getHeaders());
exchange.getMessage().getHeaders().putAll(beanioheader.getHeaders());
}
results.add(readObject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public Object unmarshal(Exchange exchange, InputStream inputStream) throws Excep

if (!factory.skipHeader()) {
Map<String, Object> headerObjMap = createModel(headerFactory, line, count.intValue());
exchange.getOut().setHeader(CAMEL_BINDY_FIXED_LENGTH_HEADER, headerObjMap);
exchange.getMessage().setHeader(CAMEL_BINDY_FIXED_LENGTH_HEADER, headerObjMap);
}
}

Expand Down Expand Up @@ -242,7 +242,7 @@ public Object unmarshal(Exchange exchange, InputStream inputStream) throws Excep
if (factory.hasFooter()) {
if (!factory.skipFooter()) {
Map<String, Object> footerObjMap = createModel(footerFactory, thisLine, count.intValue());
exchange.getOut().setHeader(CAMEL_BINDY_FIXED_LENGTH_FOOTER, footerObjMap);
exchange.getMessage().setHeader(CAMEL_BINDY_FIXED_LENGTH_FOOTER, footerObjMap);
}
} else {
model = createModel(factory, thisLine, count.intValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.camel.coap;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.support.DefaultProducer;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
Expand Down Expand Up @@ -80,12 +79,11 @@ public void process(Exchange exchange) throws Exception {
}

if (response != null) {
CoAPHelper.convertCoapResponseToMessage(response, exchange.getOut());
CoAPHelper.convertCoapResponseToMessage(response, exchange.getMessage());
}

if (method.equalsIgnoreCase(CoAPConstants.METHOD_PING)) {
Message resp = exchange.getOut();
resp.setBody(pingResponse);
exchange.getMessage().setBody(pingResponse);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public void push(ServerSession remote, ServerMessage cometdMessage) throws Excep
ServerChannel channel = getBayeux().getChannel(channelName);
ServerSession serverSession = getServerSession();

ServerMessage.Mutable outMessage = binding.createCometdMessage(channel, serverSession, exchange.getOut());
ServerMessage.Mutable outMessage
= binding.createCometdMessage(channel, serverSession, exchange.getMessage());
remote.deliver(serverSession, outMessage, Promise.noop());
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public void setupResources() {
}

@Override
@SuppressWarnings("deprecation")
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
Expand All @@ -140,11 +139,11 @@ public void configure() throws URISyntaxException {

from(uri)
.setExchangePattern(ExchangePattern.InOut)
.process(exchange -> exchange.getOut().setBody("reply: " + exchange.getIn().getBody()));
.process(exchange -> exchange.getMessage().setBody("reply: " + exchange.getIn().getBody()));

from(uriSSL)
.setExchangePattern(ExchangePattern.InOut)
.process(exchange -> exchange.getOut().setBody("reply SSL: " + exchange.getIn().getBody()));
.process(exchange -> exchange.getMessage().setBody("reply SSL: " + exchange.getIn().getBody()));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void marshal(Exchange exchange, Object graph, OutputStream outputStream)
"Cannot PGP encrypt message. No public encryption key found for the User Ids " + userids
+ " in the public keyring. Either specify other User IDs or add correct public keys to the keyring.");
}
exchange.getOut().setHeader(NUMBER_OF_ENCRYPTION_KEYS, Integer.valueOf(keys.size()));
exchange.getMessage().setHeader(NUMBER_OF_ENCRYPTION_KEYS, Integer.valueOf(keys.size()));

InputStream input = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph);

Expand Down Expand Up @@ -336,7 +336,7 @@ protected List<PGPSignatureGenerator> createSignatureGenerator(Exchange exchange
return null;
}

exchange.getOut().setHeader(NUMBER_OF_SIGNING_KEYS, Integer.valueOf(sigSecretKeysWithPrivateKeyAndUserId.size()));
exchange.getMessage().setHeader(NUMBER_OF_SIGNING_KEYS, Integer.valueOf(sigSecretKeysWithPrivateKeyAndUserId.size()));

List<PGPSignatureGenerator> sigGens = new ArrayList<>();
for (PGPSecretKeyAndPrivateKeyAndUserId sigSecretKeyWithPrivateKeyAndUserId : sigSecretKeysWithPrivateKeyAndUserId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

protected Signature initSignatureService(Exchange exchange) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.camel.ExchangePattern;
import org.apache.camel.ExchangeTimedOutException;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.support.ExchangeHelper;
import org.apache.cxf.continuations.Continuation;
import org.apache.cxf.continuations.ContinuationProvider;
import org.apache.cxf.jaxrs.JAXRSInvoker;
Expand Down Expand Up @@ -170,7 +171,7 @@ private org.apache.camel.Exchange prepareExchange(
cxfExchange.put(org.apache.camel.Exchange.class, camelExchange);

if (response != null) {
camelExchange.getOut().setBody(response);
ExchangeHelper.createResponse(camelExchange).setBody(response);
}
CxfRsBinding binding = endpoint.getBinding();
binding.populateExchangeFromCxfRsRequest(cxfExchange, camelExchange, method, paramArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ protected void invokeHttpClient(Exchange exchange) throws Exception {

private static void setResponse(Exchange exchange, Object response, CxfRsBinding binding, int statesCode) throws Exception {
LOG.trace("Response body = {}", response);
exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
exchange.getMessage().setBody(binding.bindResponseToCamelBody(response, exchange));
exchange.getMessage().getHeaders().putAll(binding.bindResponseHeadersToCamelHeaders(response, exchange));
exchange.getMessage().setHeader(CxfConstants.HTTP_RESPONSE_CODE, statesCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.cxf.common.message.CxfConstants;
import org.apache.camel.support.ExchangeHelper;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand Down Expand Up @@ -63,10 +64,7 @@ public void testDipatchMessageOneway() throws Exception {
Exchange exchange = sendJaxWsDispatchMessage(name, true);
assertEquals(false, exchange.isFailed(), "The request should be handled sucessfully");

org.apache.camel.Message response = exchange.getOut();
assertNotNull(response, "The response message must not be null");

assertNull(response.getBody(), "The response body must be null");
assertFalse(ExchangeHelper.hasResponse(exchange), "The oneway response must not have a response message");
}

protected Exchange sendJaxWsDispatchMessage(final String name, final boolean oneway) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
import org.apache.camel.component.cxf.common.CxfPayload;
import org.apache.camel.component.cxf.common.message.CxfConstants;
import org.apache.camel.component.cxf.converter.CxfPayloadConverter;
import org.apache.camel.support.ExchangeHelper;
import org.apache.cxf.binding.soap.SoapHeader;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
Expand Down Expand Up @@ -67,10 +68,7 @@ public void testDispatchPayloadOneway() throws Exception {
Exchange exchange = sendJaxWsDispatchPayload(name, true);
assertEquals(false, exchange.isFailed(), "The request should be handled sucessfully");

org.apache.camel.Message response = exchange.getOut();
assertNotNull(response, "The response must not be null");

assertNull(response.getBody(), "The response must be null");
assertFalse(ExchangeHelper.hasResponse(exchange), "The oneway response must not have a response message");
}

private Exchange sendJaxWsDispatchPayload(final String name, final boolean oneway) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.camel.component.cxf.common.header.CxfHeaderHelper;
import org.apache.camel.component.cxf.transport.message.DefaultCxfMessageMapper;
import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.cxf.Bus;
Expand Down Expand Up @@ -260,10 +261,13 @@ protected ConduitInitiator getConduitInitiator() {
}

protected void propagateResponseHeadersToCamel(Message outMessage, Exchange camelExchange) {
if (!ExchangeHelper.hasResponse(camelExchange)) {
ExchangeHelper.createResponse(camelExchange);
}
// copy the camel in message header to the out message
camelExchange.getOut().getHeaders().putAll(camelExchange.getIn().getHeaders());
camelExchange.getMessage().getHeaders().putAll(camelExchange.getIn().getHeaders());
CxfHeaderHelper.propagateCxfToCamel(headerFilterStrategy, outMessage,
camelExchange.getOut(), camelExchange);
camelExchange.getMessage(), camelExchange);
}

/**
Expand All @@ -289,9 +293,9 @@ private void commitOutputMessage() throws IOException {
}
OutputStream outputStream = outMessage.getContent(OutputStream.class);
if (outputStream instanceof CachedOutputStream cachedOutputStream) {
camelExchange.getOut().setBody(cachedOutputStream.getInputStream());
camelExchange.getMessage().setBody(cachedOutputStream.getInputStream());
} else {
camelExchange.getOut().setBody(outputStream);
camelExchange.getMessage().setBody(outputStream);
}
LOG.debug("send the response message: {}", outputStream);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ public void propagateResponseHeadersToCamel(
return;
}

Map<String, Object> camelHeaders = exchange.getOut().getHeaders();
// copy the in message header to out message
camelHeaders.putAll(exchange.getIn().getHeaders());
Map<String, Object> camelHeaders = exchange.getMessage().getHeaders();

Map<String, List<String>> cxfHeaders = CastUtils.cast((Map<?, ?>) cxfMessage.get(Message.PROTOCOL_HEADERS));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ public void writeOutput(Exchange exchange, ExecResult result) {
ObjectHelper.notNull(exchange, "exchange");
ObjectHelper.notNull(result, "result");

if (exchange.getPattern().isOutCapable()) {
writeOutputInMessage(exchange.getOut(), result);
exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
} else {
writeOutputInMessage(exchange.getIn(), result);
}
writeOutputInMessage(exchange.getMessage(), result);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePropertyKey;
import org.apache.camel.WrappedFile;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
Expand Down Expand Up @@ -133,8 +134,8 @@ private GenericFileMessage<T> commonBindToExchange(Exchange exchange) {
headers = exchange.getMessage().hasHeaders() ? exchange.getMessage().getHeaders() : null;
// force storing on IN as that is what Camel expects
exchange.setIn(msg);
if (exchange.hasOut()) {
exchange.setOut(null);
if (ExchangeHelper.hasResponse(exchange)) {
ExchangeHelper.setResponse(exchange, null);
}

// preserve any existing (non file) headers, before we re-populate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ 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);
}

private String getOutputFormat(Exchange exchange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public void process(Exchange exchange) throws Exception {

Issue finalIssue = issueService.createIssue(getRepository(), issue);

// copy the header of in message to the out message
exchange.getOut().copyFrom(exchange.getIn());
exchange.getOut().setBody(finalIssue);
exchange.getMessage().setBody(finalIssue);
}

}
Loading
Loading