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 2023/12/04 09:59:12 UTC
(camel) 01/02: CAMEL-20178: camel-jbang - Transform message to support components data formats
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
commit d44fba2fd9ef2d2a832beaaa3aae4809723f759f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Dec 4 10:58:52 2023 +0100
CAMEL-20178: camel-jbang - Transform message to support components data formats
---
.../modules/ROOT/pages/camel-jbang.adoc | 43 ++++++++++++++++++++++
.../camel/cli/connector/LocalCliConnector.java | 10 +++++
.../commands/action/TransformMessageAction.java | 26 +++++++++----
3 files changed, 71 insertions(+), 8 deletions(-)
diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index fa67359692b..c9507ffec48 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -2435,6 +2435,49 @@ You can then edit the `mystyle.xsl` file, and save the file, and watch the termi
You can find this example at: https://github.com/apache/camel-kamelets-examples/tree/main/jbang/xslt-transform
+=== Transforming message using Data Formats
+
+Some data-formats can also be used for message transformation such as Base64, Csv, Flatpack.
+
+TIP: You can use `camel catalog dataformat --filter=transform` to see which components can be transformation.
+
+Given the below CSV file in the `daltons.csv`:
+
+[source,text]
+----
+Jack Dalton, 115, mad at Averell
+Joe Dalton, 105, calming Joe
+William Dalton, 105, keeping Joe from killing Averell
+Averell Dalton, 80, playing with Rantanplan
+Lucky Luke, 120, capturing the Daltons
+----
+
+Then you can transform this via Camel CSV dataformat from its form into Java objects (unmarshal).
+This will NOT transform the CSV into another format such as XML, but allows you to quickly try out the dataformat,
+and be able to inspect the Java object inside Camel.
+
+[source,bash]
+----
+$ camel transform message --body=file:daltons.csv --dataformat=csv
+----
+
+This will then output:
+
+[source,text]
+----
+2023-12-04 10:53:45.578 55793 --- Message transformed (success) (176ms)
+ Exchange (DefaultExchange) InOut 6673987D34F3B54-0000000000000000
+ Message (DefaultMessage)
+ Body (ArrayList) (size: 5 bytes: 224)
+ [[Jack Dalton, 115, mad at Averell], [Joe Dalton, 105, calming Joe], [William Dalton, 105, keeping Joe from killing Averell], [Averell Dalton, 80,
+ playing with Rantanplan], [Lucky Luke, 120, capturing the Daltons]]
+----
+
+As you can see Camel CSV dataformat will then unarmshal the input (from CSV file) to a `java.util.ArrayList` object
+with 5 elements (one per row), and each row is another List of the columns.
+
+NOTE: The transform message with dataformat is limited as some dataformat requires additional configuration that is not possible
+from the Camel JBang CLI.
== Listing what Camel components is available
diff --git a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
index df593973039..8b4ee075c4a 100644
--- a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
+++ b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
@@ -279,6 +279,7 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
String source = root.getString("source");
String language = root.getString("language");
String component = root.getString("component");
+ String dataformat = root.getString("dataformat");
String template = Jsoner.unescape(root.getStringOrDefault("template", ""));
if (component == null && template.startsWith("file:")) {
template = "resource:" + template;
@@ -396,6 +397,15 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
uri = uri + "?contentCache=false";
}
out = producer.send(uri, out);
+ } else if (dataformat != null) {
+ // transform via dataformat
+ out.setPattern(ExchangePattern.InOut);
+ out.getMessage().setBody(inputBody);
+ if (inputHeaders != null) {
+ out.getMessage().setHeaders(inputHeaders);
+ }
+ String uri = "dataformat:" + dataformat + ":unmarshal";
+ out = producer.send(uri, out);
} else {
// transform via language
Language lan = camelContext.resolveLanguage(language);
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/TransformMessageAction.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/TransformMessageAction.java
index 140d6193a70..033c3e0c4f8 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/TransformMessageAction.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/TransformMessageAction.java
@@ -69,6 +69,11 @@ public class TransformMessageAction extends ActionWatchCommand {
description = "The component to use for message transformation")
private String component;
+ @CommandLine.Option(names = {
+ "--dataformat" },
+ description = "The dataformat to use for message transformation")
+ private String dataformat;
+
@CommandLine.Option(names = {
"--template" },
description = "The template to use for message transformation (prefix with file: to refer to loading message body from file)")
@@ -116,14 +121,16 @@ public class TransformMessageAction extends ActionWatchCommand {
@Override
public Integer doCall() throws Exception {
- // either source or language/template is required
- if (source == null && template == null && language == null && component == null) {
- System.err.println("Either source or template and language/component must be configured");
- return -1;
- }
- if (source == null && (template == null || language == null && component == null)) {
- System.err.println("Both template and language/component must be configured");
- return -1;
+ if (dataformat == null) {
+ // either source or language/template is required
+ if (source == null && template == null && language == null && component == null) {
+ System.err.println("Either source or template and one of language/component must be configured");
+ return -1;
+ }
+ if (source == null && (template == null || language == null && component == null)) {
+ System.err.println("Both template and one of language/component must be configured");
+ return -1;
+ }
}
Integer exit;
@@ -174,6 +181,9 @@ public class TransformMessageAction extends ActionWatchCommand {
if (component != null) {
root.put("component", component);
}
+ if (dataformat != null) {
+ root.put("dataformat", dataformat);
+ }
if (template != null) {
root.put("template", Jsoner.escape(template));
}