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);
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: 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.

}

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 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 @@ -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));
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: removed redundant getOut() + copyFrom(in) + header set

The old code did:

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.

}

protected Signature initSignatureService(Exchange exchange) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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: 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).

}

private String getOutputFormat(Exchange exchange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ public Object unmarshal(Exchange exchange, InputStream inputStream) throws Excep
String bodyAsString = new String(body, charsetName);
Message message = parser.parse(bodyAsString);

// add MSH fields as message out headers
// add MSH fields as message headers
Terser terser = new Terser(message);
for (Map.Entry<String, String> entry : HEADER_MAP.entrySet()) {
exchange.getOut().setHeader(entry.getKey(), terser.get(entry.getValue()));
exchange.getMessage().setHeader(entry.getKey(), terser.get(entry.getValue()));
}
exchange.getOut().setHeader(HL7_CONTEXT, hapiContext);
exchange.getOut().setHeader(Exchange.CHARSET_NAME, charsetName);
exchange.getMessage().setHeader(HL7_CONTEXT, hapiContext);
exchange.getMessage().setHeader(Exchange.CHARSET_NAME, charsetName);
return message;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.support.DefaultProducer;
import org.apache.camel.support.MessageHelper;
import org.apache.camel.util.ObjectHelper;
import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
Expand Down Expand Up @@ -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);
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: 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.

}

private void doPing(Exchange exchange) {
Pong result = connection.ping();
MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
exchange.getMessage().setBody(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.camel.component.influxdb2.data.Record;
import org.apache.camel.component.influxdb2.data.Records;
import org.apache.camel.support.DefaultProducer;
import org.apache.camel.support.MessageHelper;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -174,7 +173,6 @@ private void insertRecords(Exchange exchange, String orgName, String bucketName,

private void doPing(Exchange exchange) {
Boolean result = connection.ping();
MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
exchange.getMessage().setBody(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void process(Exchange exchange) throws Exception {
}
}
node.addMixin("mix:referenceable");
exchange.getOut().setBody(node.getIdentifier());
exchange.getMessage().setBody(node.getIdentifier());
session.save();
} else if (JcrConstants.JCR_GET_BY_ID.equals(operation)) {
Node node = session.getNodeByIdentifier(exchange.getIn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Message;
import org.apache.camel.support.DefaultProducer;
import org.apache.camel.util.ObjectHelper;
import org.jooq.Configuration;
Expand Down Expand Up @@ -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);
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: 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.

break;
case NONE:
DSLContext context = DSL.using(dbConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.camel.ExchangePropertyKey;
import org.apache.camel.Processor;
import org.apache.camel.support.DefaultConsumer;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.util.IOHelper;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.filterchain.IoFilter;
Expand Down Expand Up @@ -385,7 +384,7 @@ private Exchange createExchange(IoSession session, Object payload) {
exchange.getIn().setHeader(MinaConstants.MINA_IOSESSION, session);
exchange.getIn().setHeader(MinaConstants.MINA_LOCAL_ADDRESS, session.getLocalAddress());
exchange.getIn().setHeader(MinaConstants.MINA_REMOTE_ADDRESS, session.getRemoteAddress());
MinaPayloadHelper.setIn(exchange, payload);
MinaPayloadHelper.setPayload(exchange, payload);
return exchange;
}

Expand Down Expand Up @@ -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();
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: 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.

Object response;
if (exchange.hasOut()) {
response = MinaPayloadHelper.getOut(getEndpoint(), exchange);
} else {
response = MinaPayloadHelper.getIn(getEndpoint(), exchange);
}
Object response = MinaPayloadHelper.getResponsePayload(getEndpoint(), exchange);

boolean failed = exchange.isFailed();
if (failed && !getEndpoint().getConfiguration().isTransferExchange()) {
if (exchange.getException() != null) {
response = exchange.getException();
} else {
// failed and no exception, must be a fault
response = exchange.getOut().getBody();
response = exchange.getMessage().getBody();
}
}

Expand All @@ -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
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.

Same pattern: isOutCapable ? getOut().getHeader() : getIn().getHeader()getMessage().getHeader(). The getMessage() API handles this conditional automatically.

= exchange.getMessage().getHeader(MinaConstants.MINA_CLOSE_SESSION_WHEN_COMPLETE, Boolean.class);

// should we disconnect, the header can override the configuration
if (close != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private MinaPayloadHelper() {
//Utility Class
}

public static Object getIn(MinaEndpoint endpoint, Exchange exchange) {
public static Object getRequestPayload(MinaEndpoint endpoint, Exchange exchange) {
if (endpoint.getConfiguration().isTransferExchange()) {
// we should transfer the entire exchange over the wire (includes in/out)
return DefaultExchangeHolder.marshal(exchange);
Expand All @@ -42,32 +42,22 @@ public static Object getIn(MinaEndpoint endpoint, Exchange exchange) {
}
}

public static Object getOut(MinaEndpoint endpoint, Exchange exchange) {
public static Object getResponsePayload(MinaEndpoint endpoint, Exchange exchange) {
if (endpoint.getConfiguration().isTransferExchange()) {
// we should transfer the entire exchange over the wire (includes in/out)
return DefaultExchangeHolder.marshal(exchange);
} else {
// normal transfer using the body only
return exchange.getOut().getBody();
return exchange.getMessage().getBody();
}
}

public static void setIn(Exchange exchange, Object payload) {
public static void setPayload(Exchange exchange, Object payload) {
if (payload instanceof DefaultExchangeHolder) {
DefaultExchangeHolder.unmarshal(exchange, (DefaultExchangeHolder) payload);
} else {
// normal transfer using the body only
exchange.getIn().setBody(payload);
}
}

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);
exchange.getMessage().setBody(payload);
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.

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.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.camel.ExchangeTimedOutException;
import org.apache.camel.spi.CamelLogger;
import org.apache.camel.support.DefaultProducer;
import org.apache.camel.support.ExchangeHelper;
import org.apache.camel.util.IOHelper;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.filterchain.IoFilter;
Expand Down Expand Up @@ -136,7 +135,7 @@ protected void doProcess(Exchange exchange) throws Exception {
IOHelper.normalizeCharset(getEndpoint().getConfiguration().getCharsetName()));
}

Object body = MinaPayloadHelper.getIn(getEndpoint(), exchange);
Object body = MinaPayloadHelper.getRequestPayload(getEndpoint(), exchange);
if (body == null) {
noReplyLogger.log("No payload to send for exchange: " + exchange);
return; // exit early since nothing to write
Expand Down Expand Up @@ -184,12 +183,8 @@ protected void doProcess(Exchange exchange) throws Exception {
maybeDisconnectOnTimeout();
throw new ExchangeTimedOutException(exchange, timeout);
} else {
// set the result on either IN or OUT on the original exchange depending on its pattern
if (ExchangeHelper.isOutCapable(exchange)) {
MinaPayloadHelper.setOut(exchange, handler.getMessage());
} else {
MinaPayloadHelper.setIn(exchange, handler.getMessage());
}
// set the result on the exchange
MinaPayloadHelper.setPayload(exchange, handler.getMessage());
}
}
}
Expand All @@ -210,12 +205,7 @@ protected void maybeDisconnectOnDone(Exchange exchange) throws InterruptedExcept
}

// 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 = exchange.getMessage().getHeader(MinaConstants.MINA_CLOSE_SESSION_WHEN_COMPLETE, Boolean.class);

// should we disconnect, the header can override the configuration
boolean disconnect = getEndpoint().getConfiguration().isDisconnect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public T outMessage(final Function<Message, Object> function) {
return delegate.expression(new ExpressionAdapter() {
@Override
public Object evaluate(Exchange exchange) {
return function.apply(exchange.getOut());
return function.apply(exchange.getMessage());
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.

These methods (outMessage(), outBody(), outBodyAs()) use getOut() to access the response message. getMessage() is the correct replacement — it returns the response (OUT) if one exists, otherwise the request (IN). All 5 getOut() calls in this file follow this same pattern.

}
});
}
Expand Down Expand Up @@ -215,7 +215,7 @@ public T outBody(final Function<Object, Object> function) {
return delegate.expression(new ExpressionAdapter() {
@Override
public Object evaluate(Exchange exchange) {
return function.apply(exchange.getOut().getBody());
return function.apply(exchange.getMessage().getBody());
}
});
}
Expand All @@ -228,8 +228,8 @@ public T outBody(final BiFunction<Object, Map<String, Object>, Object> function)
@Override
public Object evaluate(Exchange exchange) {
return function.apply(
exchange.getOut().getBody(),
exchange.getOut().getHeaders());
exchange.getMessage().getBody(),
exchange.getMessage().getHeaders());
}
});
}
Expand All @@ -241,7 +241,7 @@ public <B> T outBody(Class<B> expectedType, final Function<B, Object> function)
return delegate.expression(new ExpressionAdapter() {
@Override
public Object evaluate(Exchange exchange) {
return function.apply(exchange.getOut().getBody(expectedType));
return function.apply(exchange.getMessage().getBody(expectedType));
}
});
}
Expand All @@ -254,8 +254,8 @@ public <B> T outBody(Class<B> expectedType, final BiFunction<B, Map<String, Obje
@Override
public Object evaluate(Exchange exchange) {
return function.apply(
exchange.getOut().getBody(expectedType),
exchange.getOut().getHeaders());
exchange.getMessage().getBody(expectedType),
exchange.getMessage().getHeaders());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public Message getRequest() {
return exchange.getIn();
}

@SuppressWarnings("deprecation")
@Deprecated
public Message getResponse() {
return exchange.getOut();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public Message getRequest() {
return exchange.getIn();
}

@SuppressWarnings("deprecation")
@Deprecated
public Message getResponse() {
return exchange.getOut();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ private void addData() {
Exchange copy = (Exchange) body;
exchange.setException(copy.getException());
exchange.setIn(copy.getIn());
if (copy.hasOut()) {
@SuppressWarnings("deprecation")
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: exchange reconstruction — kept deprecated API with @SuppressWarnings

This code reconstructs a full exchange by copying IN, OUT, properties, and exception from a received exchange. This inherently requires the in/out distinction: if the source exchange has a separate OUT message, it must be transferred as-is to preserve the exchange state. This cannot be safely replaced with getMessage() without losing the OUT message semantics. The @SuppressWarnings("deprecation") annotation is the correct approach here.

boolean hasOut = copy.hasOut();
if (hasOut) {
exchange.setOut(copy.getOut());
}
exchange.getProperties().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void processReply(ReplyHolder holder) {
}

private static void processReceivedReply(ReplyHolder holder) {
Message message = holder.getExchange().getOut();
Message message = holder.getExchange().getMessage();
MessageExt messageExt = holder.getMessageExt();
message.setBody(messageExt.getBody());
RocketMQMessageConverter.populateHeadersByMessageExt(message, messageExt);
Expand Down
Loading
Loading