You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/08/06 10:29:40 UTC
[camel] branch camel-3.4.x updated: camel-core - Api component
avoid using reflection in api producer.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.4.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.4.x by this push:
new 06e0d2d camel-core - Api component avoid using reflection in api producer.
06e0d2d is described below
commit 06e0d2d05a0344825330b2ac4a8c1e714f4720cb
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Aug 6 10:14:26 2020 +0200
camel-core - Api component avoid using reflection in api producer.
---
.../support/component/AbstractApiProducer.java | 21 +++++++++++++++++----
1 file changed, 17 insertions(+), 4 deletions(-)
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java
index 093df1a..7cd0510 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java
@@ -24,6 +24,8 @@ import java.util.Set;
import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.spi.PropertyConfigurer;
+import org.apache.camel.spi.PropertyConfigurerGetter;
import org.apache.camel.support.DefaultAsyncProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -163,13 +165,24 @@ public abstract class AbstractApiProducer<E extends Enum<E> & ApiName, T>
private boolean processInBody(Exchange exchange, Map<String, Object> properties) {
final String inBodyProperty = endpoint.getInBody();
if (inBodyProperty != null) {
-
Object value = exchange.getIn().getBody();
if (value != null) {
try {
- value = endpoint.getCamelContext().getTypeConverter().mandatoryConvertTo(
- endpoint.getConfiguration().getClass().getDeclaredField(inBodyProperty).getType(),
- exchange, value);
+ // attempt to find out type via configurer so we avoid using reflection
+ PropertyConfigurer configurer = endpoint.getComponent().getEndpointPropertyConfigurer();
+ if (configurer instanceof PropertyConfigurerGetter) {
+ PropertyConfigurerGetter getter = (PropertyConfigurerGetter) configurer;
+ Map<String, Object> options = getter.getAllOptions(endpoint);
+ if (options.containsKey(inBodyProperty)) {
+ Class<?> type = (Class<?>) options.get(inBodyProperty);
+ value = endpoint.getCamelContext().getTypeConverter().mandatoryConvertTo(type, exchange, value);
+ }
+ } else {
+ // fallback to be reflection based
+ value = endpoint.getCamelContext().getTypeConverter().mandatoryConvertTo(
+ endpoint.getConfiguration().getClass().getDeclaredField(inBodyProperty).getType(),
+ exchange, value);
+ }
} catch (Exception e) {
exchange.setException(new RuntimeCamelException(String.format(
"Error converting value %s to property %s: %s", value, inBodyProperty, e.getMessage()), e));