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 11:07:20 UTC

(camel) branch main updated: CAMEL-20178: camel-jbang - Transform message to support options from CLI

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 fd28d91ec0c CAMEL-20178: camel-jbang - Transform message to support options from CLI
fd28d91ec0c is described below

commit fd28d91ec0cbc62670bfc520eb55548ac24c91e1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Dec 4 12:07:03 2023 +0100

    CAMEL-20178: camel-jbang - Transform message to support options from CLI
---
 .../modules/ROOT/pages/camel-jbang.adoc            | 23 ++++++++++++++++++++--
 .../camel/cli/connector/LocalCliConnector.java     | 20 +++++++++++++++++--
 .../commands/action/TransformMessageAction.java    | 18 +++++++++++++++++
 3 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index c9507ffec48..f924544e46b 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -2435,6 +2435,17 @@ 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
 
+You can configure options that the XSLT component format should use, such as shown below:
+
+[source,bash]
+----
+$ camel transform message --body=file:sample.xml --component=xslt --template=file:mystyle.xsl --option=output=bytes --pretty --watch
+----
+
+TIP: You can specify multiple options by repeating the `--option` argument.
+
+NOTE: The transform message with component is limited as some components requires configuring complex options that cannot be set from command line.
+
 === Transforming message using Data Formats
 
 Some data-formats can also be used for message transformation such as Base64, Csv, Flatpack.
@@ -2476,8 +2487,16 @@ This will then output:
 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.
+You can configure options that the CSV data format should use, such as shown below:
+
+[source,bash]
+----
+$ camel transform message --body=file:daltons.csv --dataformat=csv --option=captureHeaderRecord=true
+----
+
+TIP: You can specify multiple options by repeating the `--option` argument.
+
+NOTE: The transform message with dataformat is limited as some dataformat requires configuring complex options that cannot be set from command line.
 
 == 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 8b4ee075c4a..998ea23689c 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
@@ -79,6 +79,7 @@ import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.StopWatch;
+import org.apache.camel.util.URISupport;
 import org.apache.camel.util.concurrent.ThreadHelper;
 import org.apache.camel.util.json.JsonArray;
 import org.apache.camel.util.json.JsonObject;
@@ -287,12 +288,13 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
         String body = Jsoner.unescape(root.getString("body"));
         InputStream is = null;
         Object b = body;
-        Map<String, Object> map = null;
         if (body.startsWith("file:")) {
             File file = new File(body.substring(5));
             is = new FileInputStream(file);
             b = IOHelper.loadText(is);
         }
+        final Object inputBody = b;
+        Map<String, Object> map = null;
         Collection<JsonObject> headers = root.getCollection("headers");
         if (headers != null) {
             map = new LinkedHashMap<>();
@@ -300,8 +302,16 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
                 map.put(jo.getString("key"), jo.getString("value"));
             }
         }
-        final Object inputBody = b;
         final Map<String, Object> inputHeaders = map;
+        Map<String, Object> map2 = null;
+        Collection<JsonObject> options = root.getCollection("options");
+        if (options != null) {
+            map2 = new LinkedHashMap<>();
+            for (JsonObject jo : options) {
+                map2.put(jo.getString("key"), jo.getString("value"));
+            }
+        }
+        final Map<String, Object> componentOptions = map2;
         Exchange out = camelContext.getCamelContextExtension().getExchangeFactory().create(false);
         try {
             if (source != null) {
@@ -396,6 +406,9 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
                 if (euf.propertyNames().contains("contentCache")) {
                     uri = uri + "?contentCache=false";
                 }
+                if (componentOptions != null) {
+                    uri = URISupport.appendParametersToURI(uri, componentOptions);
+                }
                 out = producer.send(uri, out);
             } else if (dataformat != null) {
                 // transform via dataformat
@@ -405,6 +418,9 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
                     out.getMessage().setHeaders(inputHeaders);
                 }
                 String uri = "dataformat:" + dataformat + ":unmarshal";
+                if (componentOptions != null) {
+                    uri = URISupport.appendParametersToURI(uri, componentOptions);
+                }
                 out = producer.send(uri, out);
             } else {
                 // transform via 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 033c3e0c4f8..e33c822e5b3 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
@@ -79,6 +79,10 @@ public class TransformMessageAction extends ActionWatchCommand {
                         description = "The template to use for message transformation (prefix with file: to refer to loading message body from file)")
     private String template;
 
+    @CommandLine.Option(names = { "--option" },
+                        description = "Option for component or dataformat (key=value)")
+    List<String> options;
+
     @CommandLine.Option(names = {
             "--output" },
                         description = "File to store output. If none provide then output is printed to console.")
@@ -202,6 +206,20 @@ public class TransformMessageAction extends ActionWatchCommand {
             }
             root.put("headers", arr);
         }
+        if (options != null) {
+            JsonArray arr = new JsonArray();
+            for (String h : options) {
+                JsonObject jo = new JsonObject();
+                if (!h.contains("=")) {
+                    System.out.println("Option must be in key=value format, was: " + h);
+                    return 0;
+                }
+                jo.put("key", StringHelper.before(h, "="));
+                jo.put("value", StringHelper.after(h, "="));
+                arr.add(jo);
+            }
+            root.put("options", arr);
+        }
         File f = getActionFile(Long.toString(pid));
         try {
             IOHelper.writeText(root.toJson(), f);