This is an automated email from the ASF dual-hosted git repository.
Croway 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 4f05994be921 CAMEL-24541: camel-djl - guard ImageNetUtil against class
names without a space
4f05994be921 is described below
commit 4f05994be9215608033068c7acfee7e25b4f5ef8
Author: Andrea Cosentino <[email protected]>
AuthorDate: Tue Sep 1 11:10:43 2026 +0200
CAMEL-24541: camel-djl - guard ImageNetUtil against class names without a
space
extractClassName did body.best().getClassName().split(",")[0].split(" ",
2)[1],
assuming every ImageNet class name has the "<id> <label>" shape. A class
name
without a space (a bare id, a custom label, a malformed entry) makes split
return
a single element, so [1] throws ArrayIndexOutOfBoundsException. Guard on
the split
length and fall back to the whole token when there is no space.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: Andrea Cosentino <[email protected]>
---
.../apache/camel/component/djl/ImageNetUtil.java | 7 ++-
.../camel/component/djl/ImageNetUtilTest.java | 51 ++++++++++++++++++++++
2 files changed, 57 insertions(+), 1 deletion(-)
diff --git
a/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/ImageNetUtil.java
b/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/ImageNetUtil.java
index aa96028c472f..fd3831d0941a 100644
---
a/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/ImageNetUtil.java
+++
b/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/ImageNetUtil.java
@@ -37,7 +37,12 @@ public class ImageNetUtil {
public void extractClassName(Exchange exchange) {
Classifications body =
exchange.getMessage().getBody(Classifications.class);
- String className = body.best().getClassName().split(",")[0].split(" ",
2)[1];
+ // ImageNet class names look like "n01440764 tench, Tinca tinca": keep
the label after the id and
+ // before the first comma. Fall back to the whole token when there is
no space, so a class name
+ // without the expected "<id> <label>" shape does not throw
ArrayIndexOutOfBoundsException.
+ String firstLabel = body.best().getClassName().split(",")[0];
+ String[] parts = firstLabel.split(" ", 2);
+ String className = parts.length > 1 ? parts[1] : parts[0];
exchange.getMessage().setBody(className);
}
diff --git
a/components/camel-ai/camel-djl/src/test/java/org/apache/camel/component/djl/ImageNetUtilTest.java
b/components/camel-ai/camel-djl/src/test/java/org/apache/camel/component/djl/ImageNetUtilTest.java
new file mode 100644
index 000000000000..46ca64923c19
--- /dev/null
+++
b/components/camel-ai/camel-djl/src/test/java/org/apache/camel/component/djl/ImageNetUtilTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.djl;
+
+import java.util.List;
+
+import ai.djl.modality.Classifications;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class ImageNetUtilTest {
+
+ private Exchange exchangeWithClassName(String className) {
+ Exchange exchange = new DefaultExchange(new DefaultCamelContext());
+ exchange.getMessage().setBody(new Classifications(List.of(className),
List.of(1.0)));
+ return exchange;
+ }
+
+ @Test
+ void extractsTheLabelFromAnImageNetClassName() {
+ Exchange exchange = exchangeWithClassName("n01440764 tench, Tinca
tinca");
+ new ImageNetUtil().extractClassName(exchange);
+ assertEquals("tench", exchange.getMessage().getBody(String.class));
+ }
+
+ @Test
+ void classNameWithoutSpaceDoesNotThrow() {
+ Exchange exchange = exchangeWithClassName("tench");
+ assertDoesNotThrow(() -> new
ImageNetUtil().extractClassName(exchange));
+ assertEquals("tench", exchange.getMessage().getBody(String.class));
+ }
+}