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 5ffbcae5529e CAMEL-24878: camel-kamelet-main - resolving an http: 
resource must not fetch it
5ffbcae5529e is described below

commit 5ffbcae5529ecbe5effd13d3b5dca2e1d353163c
Author: Jiří Ondrušek <[email protected]>
AuthorDate: Mon Sep 21 19:18:51 2026 +0200

    CAMEL-24878: camel-kamelet-main - resolving an http: resource must not 
fetch it
    
    Since CAMEL-24852 the CLI resource loader called exists() on every
    resolved resource before deciding whether to look it up next to the
    route files. For an http: resource exists() is a GET, so rest-openapi
    read its specification twice and the camel-kamelets OpenApiIT failed.
    
    The scheme is now checked first: only a classpath: or file: resource
    is probed and looked up in the fallback directories; any other scheme
    returns the resolved resource untouched. A regression test resolves an
    http: resource against a local HttpServer and asserts nothing was
    fetched until the stream is read.
    
    Closes #26706
    
    Co-authored-by: Claude <[email protected]>
---
 .../DependencyDownloaderResourceLoader.java        |  5 ++--
 .../DependencyDownloaderResourceLoaderTest.java    | 34 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderResourceLoader.java
 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderResourceLoader.java
index 29ccb8645a50..95873a464766 100644
--- 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderResourceLoader.java
+++ 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderResourceLoader.java
@@ -63,8 +63,9 @@ public class DependencyDownloaderResourceLoader extends 
DefaultResourceLoader {
             }
         }
         Resource answer = super.resolveResource(uri);
-        boolean exists = answer != null && answer.exists();
-        if (!exists && ("classpath".equals(scheme) || "file".equals(scheme))) {
+        // the scheme is checked before exists(): only a classpath: or file: 
resource is looked up next to the
+        // routes, and on an http: resource exists() is a GET (rest-openapi 
read its specification twice)
+        if (("classpath".equals(scheme) || "file".equals(scheme)) && (answer 
== null || !answer.exists())) {
             String path = StringHelper.after(uri, ":");
             // strip leading double slash
             if (path.startsWith("//")) {
diff --git 
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderResourceLoaderTest.java
 
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderResourceLoaderTest.java
index 23d4b3f074e4..f0825520445b 100644
--- 
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderResourceLoaderTest.java
+++ 
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderResourceLoaderTest.java
@@ -16,10 +16,15 @@
  */
 package org.apache.camel.main.download;
 
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
+import com.sun.net.httpserver.HttpServer;
 import org.apache.camel.impl.engine.SimpleCamelContext;
 import org.apache.camel.spi.Resource;
 import org.junit.jupiter.api.Test;
@@ -93,4 +98,33 @@ public class DependencyDownloaderResourceLoaderTest {
         Resource resource = loader.resolveResource("classpath:mapping.groovy");
         assertEquals("from source dir", new 
String(resource.getInputStream().readAllBytes()));
     }
+
+    /** Resolving an http: resource must not fetch it: only classpath: and 
file: resources are probed and looked up. */
+    @Test
+    void anHttpResourceIsNotFetchedWhenResolved() throws Exception {
+        AtomicInteger requests = new AtomicInteger();
+        HttpServer server = HttpServer.create(new 
InetSocketAddress("localhost", 0), 0);
+        server.createContext("/openapi.json", exchange -> {
+            requests.incrementAndGet();
+            byte[] body = "{}".getBytes(StandardCharsets.UTF_8);
+            exchange.sendResponseHeaders(200, body.length);
+            try (OutputStream out = exchange.getResponseBody()) {
+                out.write(body);
+            }
+        });
+        server.start();
+        try {
+            SimpleCamelContext context = new SimpleCamelContext();
+            DependencyDownloaderResourceLoader loader
+                    = new DependencyDownloaderResourceLoader(context, null, 
List.of(routes.toString()));
+
+            Resource resource
+                    = loader.resolveResource("http://localhost:"; + 
server.getAddress().getPort() + "/openapi.json");
+            assertEquals(0, requests.get(), "resolving fetched the resource 
(rest-openapi then read its specification twice)");
+            assertEquals("{}", new 
String(resource.getInputStream().readAllBytes()));
+            assertEquals(1, requests.get());
+        } finally {
+            server.stop(0);
+        }
+    }
 }

Reply via email to