You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2024/04/16 12:59:56 UTC
(camel) 03/05: (chores) camel-core: cleaned up duplicated code dealing with variable repositories
This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 782c7e527cbb339c1c7caaa29cc535cfade4f25a
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Mon Apr 15 13:46:55 2024 +0200
(chores) camel-core: cleaned up duplicated code dealing with variable repositories
---
.../org/apache/camel/support/ExchangeHelper.java | 107 ++++++++++-----------
1 file changed, 50 insertions(+), 57 deletions(-)
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
index a73db916db9..88674c95fa6 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
@@ -1088,29 +1088,34 @@ public final class ExchangeHelper {
*/
public static void setVariable(Exchange exchange, String name, Object value) {
VariableRepository repo = null;
+ final String id = getRepositoryId(name);
+ if (id != null) {
+ repo = getVariableRepository(exchange, id);
+ name = resolveRepositoryName(exchange, name, id);
+ }
+ final VariableAware va = getVariableAware(exchange, repo);
+ va.setVariable(name, value);
+ }
+
+ private static String getRepositoryId(String name) {
String id = StringHelper.before(name, ":");
// header and exchange is reserved
- if ("header".equals(id) || "exchange".equals(id)) {
+ if (isReserved(id)) {
id = null;
}
- if (id != null) {
- VariableRepositoryFactory factory
- = exchange.getContext().getCamelContextExtension().getContextPlugin(VariableRepositoryFactory.class);
- repo = factory.getVariableRepository(id);
- if (repo == null) {
- throw new IllegalArgumentException("VariableRepository with id: " + id + " does not exist");
- }
- name = StringHelper.after(name, ":");
- // special for route, where we need to enrich the name with current route id if none given
- if ("route".equals(id) && !name.contains(":")) {
- String prefix = getAtRouteId(exchange);
- if (prefix != null) {
- name = prefix + ":" + name;
- }
+ return id;
+ }
+
+ private static String resolveRepositoryName(Exchange exchange, String name, String id) {
+ name = StringHelper.after(name, ":");
+ // special for route, where we need to enrich the name with current route id if none given
+ if ("route".equals(id) && !name.contains(":")) {
+ String prefix = getAtRouteId(exchange);
+ if (prefix != null) {
+ name = prefix + ":" + name;
}
}
- VariableAware va = repo != null ? repo : exchange;
- va.setVariable(name, value);
+ return name;
}
/**
@@ -1123,28 +1128,12 @@ public final class ExchangeHelper {
*/
public static void setVariableFromMessageBodyAndHeaders(Exchange exchange, String name, Message message) {
VariableRepository repo = null;
- String id = StringHelper.before(name, ":");
- // header and exchange is reserved
- if ("header".equals(id) || "exchange".equals(id)) {
- id = null;
- }
+ final String id = getRepositoryId(name);
if (id != null) {
- VariableRepositoryFactory factory
- = exchange.getContext().getCamelContextExtension().getContextPlugin(VariableRepositoryFactory.class);
- repo = factory.getVariableRepository(id);
- if (repo == null) {
- throw new IllegalArgumentException("VariableRepository with id: " + id + " does not exist");
- }
- name = StringHelper.after(name, ":");
- // special for route, where we need to enrich the name with current route id if none given
- if ("route".equals(id) && !name.contains(":")) {
- String prefix = getAtRouteId(exchange);
- if (prefix != null) {
- name = prefix + ":" + name;
- }
- }
+ repo = getVariableRepository(exchange, id);
+ name = resolveRepositoryName(exchange, name, id);
}
- VariableAware va = repo != null ? repo : exchange;
+ final VariableAware va = getVariableAware(exchange, repo);
// set body and headers as variables
Object body = message.getBody();
@@ -1189,31 +1178,35 @@ public final class ExchangeHelper {
*/
public static Object getVariable(Exchange exchange, String name) {
VariableRepository repo = null;
- String id = StringHelper.before(name, ":");
- // header and exchange is reserved
- if ("header".equals(id) || "exchange".equals(id)) {
- id = null;
- }
+ final String id = getRepositoryId(name);
if (id != null) {
- VariableRepositoryFactory factory
- = exchange.getContext().getCamelContextExtension().getContextPlugin(VariableRepositoryFactory.class);
- repo = factory.getVariableRepository(id);
- if (repo == null) {
- throw new IllegalArgumentException("VariableRepository with id: " + id + " does not exist");
- }
- name = StringHelper.after(name, ":");
- // special for route, where we need to enrich the name with current route id if none given
- if ("route".equals(id) && !name.contains(":")) {
- String prefix = getAtRouteId(exchange);
- if (prefix != null) {
- name = prefix + ":" + name;
- }
- }
+ repo = getVariableRepository(exchange, id);
+
+ name = resolveRepositoryName(exchange, name, id);
}
- VariableAware va = repo != null ? repo : exchange;
+ final VariableAware va = getVariableAware(exchange, repo);
return va.getVariable(name);
}
+ private static VariableRepository getVariableRepository(Exchange exchange, String id) {
+ VariableRepositoryFactory factory
+ = exchange.getContext().getCamelContextExtension().getContextPlugin(VariableRepositoryFactory.class);
+ VariableRepository repo = factory.getVariableRepository(id);
+ if (repo == null) {
+ throw new IllegalArgumentException("VariableRepository with id: " + id + " does not exist");
+ }
+ return repo;
+ }
+
+ private static boolean isReserved(String id) {
+ return "header".equals(id) || "exchange".equals(id);
+ }
+
+ private static VariableAware getVariableAware(Exchange exchange, VariableRepository repo) {
+ VariableAware va = repo != null ? repo : exchange;
+ return va;
+ }
+
/**
* Gets the variable, converted to the given type
*