This is an automated email from the ASF dual-hosted git repository.

tsato 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 af7134feccc camel-djl - Add a converter: Image -> byte[] for saving 
output to file
af7134feccc is described below

commit af7134feccc83bf414c4c32236deef21d6bfa6ce
Author: Tadayoshi Sato <sato.tadayo...@gmail.com>
AuthorDate: Tue Jul 23 17:30:39 2024 +0900

    camel-djl - Add a converter: Image -> byte[] for saving output to file
---
 .../org/apache/camel/catalog/components/djl.json        |  3 ++-
 .../apache/camel/component/djl/DJLConverterLoader.java  |  2 ++
 .../META-INF/org/apache/camel/component/djl/djl.json    |  3 ++-
 .../org/apache/camel/component/djl/DJLConstants.java    |  3 +++
 .../org/apache/camel/component/djl/DJLConverter.java    | 17 +++++++++++++++++
 .../camel/component/djl/CvImageGenerationTest.java      | 11 ++---------
 .../builder/endpoint/dsl/DJLEndpointBuilderFactory.java | 13 +++++++++++++
 7 files changed, 41 insertions(+), 11 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/djl.json
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/djl.json
index cc6c624650b..4c024b5de69 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/djl.json
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/djl.json
@@ -27,7 +27,8 @@
     "autowiredEnabled": { "index": 1, "kind": "property", "displayName": 
"Autowired Enabled", "group": "advanced", "label": "advanced", "required": 
false, "type": "boolean", "javaType": "boolean", "deprecated": false, 
"autowired": false, "secret": false, "defaultValue": true, "description": 
"Whether autowiring is enabled. This is used for automatic autowiring options 
(the option must be marked as autowired) by looking up in the registry to find 
if there is a single instance of matching t [...]
   },
   "headers": {
-    "CamelDjlInput": { "index": 0, "kind": "header", "displayName": "", 
"group": "producer", "label": "", "required": false, "javaType": "", 
"deprecated": false, "deprecationNote": "", "autowired": false, "secret": 
false, "description": "The input data used for prediction", "constantName": 
"org.apache.camel.component.djl.DJLConstants#INPUT" }
+    "CamelDjlInput": { "index": 0, "kind": "header", "displayName": "", 
"group": "producer", "label": "", "required": false, "javaType": "", 
"deprecated": false, "deprecationNote": "", "autowired": false, "secret": 
false, "description": "The input data used for prediction", "constantName": 
"org.apache.camel.component.djl.DJLConstants#INPUT" },
+    "CamelDjlFileType": { "index": 1, "kind": "header", "displayName": "", 
"group": "producer", "label": "", "required": false, "javaType": "", 
"deprecated": false, "deprecationNote": "", "autowired": false, "secret": 
false, "description": "The file type of the message body data. It is used when 
the body is converted to bytes.", "constantName": 
"org.apache.camel.component.djl.DJLConstants#FILE_TYPE" }
   },
   "properties": {
     "application": { "index": 0, "kind": "path", "displayName": "Application", 
"group": "producer", "label": "", "required": true, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", 
"autowired": false, "secret": false, "description": "Application name" },
diff --git 
a/components/camel-ai/camel-djl/src/generated/java/org/apache/camel/component/djl/DJLConverterLoader.java
 
b/components/camel-ai/camel-djl/src/generated/java/org/apache/camel/component/djl/DJLConverterLoader.java
index 871c79ac82d..c87a2dbbb8b 100644
--- 
a/components/camel-ai/camel-djl/src/generated/java/org/apache/camel/component/djl/DJLConverterLoader.java
+++ 
b/components/camel-ai/camel-djl/src/generated/java/org/apache/camel/component/djl/DJLConverterLoader.java
@@ -64,6 +64,8 @@ public final class DJLConverterLoader implements 
TypeConverterLoader, CamelConte
             (type, exchange, value) -> 
org.apache.camel.component.djl.DJLConverter.toImage((java.nio.file.Path) 
value));
         addTypeConverter(registry, ai.djl.modality.cv.Image[].class, 
ai.djl.modality.cv.output.DetectedObjects.class, false,
             (type, exchange, value) -> 
org.apache.camel.component.djl.DJLConverter.toImages((ai.djl.modality.cv.output.DetectedObjects)
 value, exchange));
+        addTypeConverter(registry, byte[].class, 
ai.djl.modality.cv.Image.class, false,
+            (type, exchange, value) -> 
org.apache.camel.component.djl.DJLConverter.toBytes((ai.djl.modality.cv.Image) 
value, exchange));
     }
 
     private static void addTypeConverter(TypeConverterRegistry registry, 
Class<?> toType, Class<?> fromType, boolean allowNull, 
SimpleTypeConverter.ConversionMethod method) {
diff --git 
a/components/camel-ai/camel-djl/src/generated/resources/META-INF/org/apache/camel/component/djl/djl.json
 
b/components/camel-ai/camel-djl/src/generated/resources/META-INF/org/apache/camel/component/djl/djl.json
index cc6c624650b..4c024b5de69 100644
--- 
a/components/camel-ai/camel-djl/src/generated/resources/META-INF/org/apache/camel/component/djl/djl.json
+++ 
b/components/camel-ai/camel-djl/src/generated/resources/META-INF/org/apache/camel/component/djl/djl.json
@@ -27,7 +27,8 @@
     "autowiredEnabled": { "index": 1, "kind": "property", "displayName": 
"Autowired Enabled", "group": "advanced", "label": "advanced", "required": 
false, "type": "boolean", "javaType": "boolean", "deprecated": false, 
"autowired": false, "secret": false, "defaultValue": true, "description": 
"Whether autowiring is enabled. This is used for automatic autowiring options 
(the option must be marked as autowired) by looking up in the registry to find 
if there is a single instance of matching t [...]
   },
   "headers": {
-    "CamelDjlInput": { "index": 0, "kind": "header", "displayName": "", 
"group": "producer", "label": "", "required": false, "javaType": "", 
"deprecated": false, "deprecationNote": "", "autowired": false, "secret": 
false, "description": "The input data used for prediction", "constantName": 
"org.apache.camel.component.djl.DJLConstants#INPUT" }
+    "CamelDjlInput": { "index": 0, "kind": "header", "displayName": "", 
"group": "producer", "label": "", "required": false, "javaType": "", 
"deprecated": false, "deprecationNote": "", "autowired": false, "secret": 
false, "description": "The input data used for prediction", "constantName": 
"org.apache.camel.component.djl.DJLConstants#INPUT" },
+    "CamelDjlFileType": { "index": 1, "kind": "header", "displayName": "", 
"group": "producer", "label": "", "required": false, "javaType": "", 
"deprecated": false, "deprecationNote": "", "autowired": false, "secret": 
false, "description": "The file type of the message body data. It is used when 
the body is converted to bytes.", "constantName": 
"org.apache.camel.component.djl.DJLConstants#FILE_TYPE" }
   },
   "properties": {
     "application": { "index": 0, "kind": "path", "displayName": "Application", 
"group": "producer", "label": "", "required": true, "type": "string", 
"javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", 
"autowired": false, "secret": false, "description": "Application name" },
diff --git 
a/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConstants.java
 
b/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConstants.java
index 52832d84812..b4307825c8f 100644
--- 
a/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConstants.java
+++ 
b/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConstants.java
@@ -24,4 +24,7 @@ import org.apache.camel.spi.Metadata;
 public interface DJLConstants {
     @Metadata(description = "The input data used for prediction")
     String INPUT = "CamelDjlInput";
+
+    @Metadata(description = "The file type of the message body data. It is 
used when the body is converted to bytes.")
+    String FILE_TYPE = "CamelDjlFileType";
 }
diff --git 
a/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConverter.java
 
b/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConverter.java
index 1533d6c2418..1a841cde174 100644
--- 
a/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConverter.java
+++ 
b/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConverter.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.djl;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -32,6 +33,7 @@ import ai.djl.modality.cv.output.DetectedObjects;
 import ai.djl.modality.cv.output.Rectangle;
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
+import org.apache.camel.TypeConversionException;
 
 /**
  * Converter methods to convert from / to DJL types.
@@ -81,6 +83,21 @@ public class DJLConverter {
                 .toArray(Image[]::new);
     }
 
+    @Converter
+    public static byte[] toBytes(Image image, Exchange exchange) throws 
IOException {
+        if (exchange == null || exchange.getMessage() == null
+                || exchange.getMessage().getHeader(DJLConstants.FILE_TYPE) == 
null) {
+            throw new TypeConversionException(
+                    image, Image.class,
+                    new IllegalStateException("File type must be provided via 
" + DJLConstants.FILE_TYPE + " header"));
+        }
+
+        String fileType = 
exchange.getMessage().getHeader(DJLConstants.FILE_TYPE, String.class);
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        image.save(os, fileType);
+        return os.toByteArray();
+    }
+
     @Converter
     public static Audio toAudio(byte[] bytes) throws IOException {
         return toAudio(new ByteArrayInputStream(bytes));
diff --git 
a/components/camel-ai/camel-djl/src/test/java/org/apache/camel/component/djl/CvImageGenerationTest.java
 
b/components/camel-ai/camel-djl/src/test/java/org/apache/camel/component/djl/CvImageGenerationTest.java
index 5ff283d3b72..cab9215fc5d 100644
--- 
a/components/camel-ai/camel-djl/src/test/java/org/apache/camel/component/djl/CvImageGenerationTest.java
+++ 
b/components/camel-ai/camel-djl/src/test/java/org/apache/camel/component/djl/CvImageGenerationTest.java
@@ -16,9 +16,6 @@
  */
 package org.apache.camel.component.djl;
 
-import java.io.ByteArrayOutputStream;
-
-import ai.djl.modality.cv.Image;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.test.junit5.CamelTestSupport;
 import org.junit.jupiter.api.BeforeAll;
@@ -48,12 +45,8 @@ public class CvImageGenerationTest extends CamelTestSupport {
                         
.to("djl:cv/image_generation?artifactId=ai.djl.pytorch:biggan-deep:0.0.1")
                         .split(body())
                         .log("image = ${body}")
-                        .process(exchange -> {
-                            var image = exchange.getIn().getBody(Image.class);
-                            var os = new ByteArrayOutputStream();
-                            image.save(os, "png");
-                            exchange.getIn().setBody(os.toByteArray());
-                        })
+                        .setHeader(DJLConstants.FILE_TYPE, constant("png"))
+                        .convertBodyTo(byte[].class)
                         
.to("file:target/output?fileName=CvImageGenerationTest-${date:now:ssSSS}.png")
                         .to("mock:result");
             }
diff --git 
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/DJLEndpointBuilderFactory.java
 
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/DJLEndpointBuilderFactory.java
index b4351102a4f..76941008226 100644
--- 
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/DJLEndpointBuilderFactory.java
+++ 
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/DJLEndpointBuilderFactory.java
@@ -260,6 +260,19 @@ public interface DJLEndpointBuilderFactory {
         public String djlInput() {
             return "CamelDjlInput";
         }
+        /**
+         * The file type of the message body data. It is used when the body is
+         * converted to bytes.
+         * 
+         * The option is a: {@code } type.
+         * 
+         * Group: producer
+         * 
+         * @return the name of the header {@code DjlFileType}.
+         */
+        public String djlFileType() {
+            return "CamelDjlFileType";
+        }
     }
     static DJLEndpointBuilder endpointBuilder(String componentName, String 
path) {
         class DJLEndpointBuilderImpl extends AbstractEndpointBuilder 
implements DJLEndpointBuilder, AdvancedDJLEndpointBuilder {

Reply via email to