This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 47e2e3e CAMEL-16824: camel-jpa - Do not lose headers when MEP is out
capable.
47e2e3e is described below
commit 47e2e3ebbf726b57b14170ecaefc301137fe4b11
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Jul 30 08:37:08 2021 +0200
CAMEL-16824: camel-jpa - Do not lose headers when MEP is out capable.
---
.../apache/camel/component/jpa/JpaProducer.java | 19 +++++-
.../jpa/JpaProducerPreserveHeadersTest.java | 70 ++++++++++++++++++++++
2 files changed, 87 insertions(+), 2 deletions(-)
diff --git
a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
index 3008622..f83d1be 100644
---
a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
+++
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
@@ -29,6 +29,7 @@ import org.apache.camel.Expression;
import org.apache.camel.Message;
import org.apache.camel.language.simple.SimpleLanguage;
import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.ExchangeHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.TransactionStatus;
@@ -184,8 +185,15 @@ public class JpaProducer extends DefaultProducer {
entityManager.joinTransaction();
}
+ Message target;
+ if (ExchangeHelper.isOutCapable(exchange)) {
+ target = exchange.getOut();
+ // preserve headers
+ target.getHeaders().putAll(exchange.getIn().getHeaders());
+ } else {
+ target = exchange.getIn();
+ }
Object answer = isUseExecuteUpdate() ? query.executeUpdate() :
query.getResultList();
- Message target = exchange.getPattern().isOutCapable() ?
exchange.getOut() : exchange.getIn();
target.setBody(answer);
if (getEndpoint().isFlushOnSend()) {
@@ -234,7 +242,14 @@ public class JpaProducer extends DefaultProducer {
Object answer =
entityManager.find(getEndpoint().getEntityType(), key);
LOG.debug("Find: {} -> {}", key, answer);
- Message target = exchange.getPattern().isOutCapable() ?
exchange.getOut() : exchange.getIn();
+ Message target;
+ if (ExchangeHelper.isOutCapable(exchange)) {
+ target = exchange.getOut();
+ // preserve headers
+
target.getHeaders().putAll(exchange.getIn().getHeaders());
+ } else {
+ target = exchange.getIn();
+ }
target.setBody(answer);
if (getEndpoint().isFlushOnSend()) {
diff --git
a/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerPreserveHeadersTest.java
b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerPreserveHeadersTest.java
new file mode 100644
index 0000000..e3686ad
--- /dev/null
+++
b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerPreserveHeadersTest.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor.jpa;
+
+import java.util.List;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.examples.SendEmail;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class JpaProducerPreserveHeadersTest extends AbstractJpaTest {
+ protected static final String SELECT_ALL_STRING = "select x from " +
SendEmail.class.getName() + " x";
+
+ @Test
+ public void testRouteJpa() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ mock.expectedHeaderReceived("foo", 123);
+
+ // insert new record
+ template.sendBody("jpa://" + SendEmail.class.getName(), new
SendEmail("[email protected]"));
+
+ // query the record as InOut
+ template.requestBodyAndHeader("direct:start", null, "foo", 123);
+
+ assertMockEndpointsSatisfied();
+
+ SendEmail se = (SendEmail)
mock.getReceivedExchanges().get(0).getMessage().getBody(List.class).get(0);
+ assertEquals("[email protected]", se.getAddress());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct:start")
+ .to("jpa://" + SendEmail.class.getName() + "?query=" +
SELECT_ALL_STRING)
+ .to("mock:result");
+ }
+ };
+ }
+
+ @Override
+ protected String routeXml() {
+ return "org/apache/camel/processor/jpa/springJpaRouteTest.xml";
+ }
+
+ @Override
+ protected String selectAllString() {
+ return SELECT_ALL_STRING;
+ }
+
+}