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

coheigea pushed a commit to branch 3.6.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.6.x-fixes by this push:
     new 08f4c13fee9 Reuse the import key for schemas served through the 
catalog fallback (#3739)
08f4c13fee9 is described below

commit 08f4c13fee9075760eeb22684a5eae6b4c77743b
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Wed Nov 18 14:25:58 2026 +0000

    Reuse the import key for schemas served through the catalog fallback (#3739)
    
    * Reuse the import key for schemas served through the catalog fallback
    
    * Review comments
    
    (cherry picked from commit e8f9a1bf28aae28d8124f16934f252eebf22d154)
---
 .../java/org/apache/cxf/frontend/WSDLGetUtils.java |  31 ++++--
 .../org/apache/cxf/frontend/WSDLGetUtilsTest.java  | 111 +++++++++++++++++++++
 2 files changed, 135 insertions(+), 7 deletions(-)

diff --git 
a/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetUtils.java 
b/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetUtils.java
index beac0c02ba5..344779106cf 100644
--- a/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetUtils.java
+++ b/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetUtils.java
@@ -211,13 +211,8 @@ public class WSDLGetUtils {
             }
             if (ref != null) {
                 // we are able to map this, but for some reason the default 
key passed in cannot
-                // be used for a direct lookup, we need to create a unique 
import key
-                int count = 1;
-                while (smp.containsKey("_import" + count + ".xsd")) {
-                    count++;
-                }
-                key = "_import" + count + ".xsd";
-                smp.put(key, ref);
+                // be used for a direct lookup, so we need a unique import key 
for it
+                key = getImportKey(smp, ref);
             }
         }
         if (ref != null) {
@@ -226,6 +221,28 @@ public class WSDLGetUtils {
         return null;
     }
 
+    /**
+     * Get the import key for a schema that can't be looked up directly. The 
key already created for the
+     * schema is reused, so that serving the same document repeatedly doesn't 
keep adding new keys to the
+     * map, which lives as long as the service.
+     */
+    private static String getImportKey(Map<String, SchemaReference> smp, 
SchemaReference ref) {
+        for (Map.Entry<String, SchemaReference> entry : smp.entrySet()) {
+            if (entry.getValue() == ref && 
entry.getKey().startsWith("_import")) {
+                return entry.getKey();
+            }
+        }
+        // claim a free key atomically, so that concurrent requests can't be 
given the same key for
+        // different schemas
+        for (int count = 1;; count++) {
+            String key = "_import" + count + ".xsd";
+            SchemaReference existing = smp.putIfAbsent(key, ref);
+            if (existing == null || existing == ref) {
+                return key;
+            }
+        }
+    }
+
     @Deprecated
     protected void updateDoc(Document doc,
                              String base,
diff --git 
a/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/WSDLGetUtilsTest.java
 
b/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/WSDLGetUtilsTest.java
new file mode 100644
index 00000000000..e32606aff9d
--- /dev/null
+++ 
b/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/WSDLGetUtilsTest.java
@@ -0,0 +1,111 @@
+/**
+ * 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.cxf.frontend;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import javax.wsdl.extensions.schema.SchemaReference;
+
+import com.ibm.wsdl.extensions.schema.SchemaReferenceImpl;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+public class WSDLGetUtilsTest {
+
+    private static final String BASE = "http://localhost:8080/service";;
+    private static final String DOCUMENT = "http://localhost/schemas/a.xsd";;
+
+    @Test
+    public void testImportKeyReused() throws Exception {
+        // b.xsd can only be found relative to the document it is imported 
from, not by its own key
+        Map<String, SchemaReference> smp = new ConcurrentHashMap<>();
+        smp.put("http://localhost/schemas/b.xsd";, new SchemaReferenceImpl());
+        smp.put("http://localhost/schemas/c.xsd";, new SchemaReferenceImpl());
+        WSDLGetUtils utils = new WSDLGetUtils();
+
+        String link = utils.mapUri(null, BASE, smp, "b.xsd", null, DOCUMENT);
+        assertEquals(BASE + "?xsd=_import1.xsd", link);
+
+        // serving the same import again uses the same key, rather than adding 
a new one each time
+        for (int i = 0; i < 10; i++) {
+            assertEquals(link, utils.mapUri(null, BASE, smp, "b.xsd", null, 
DOCUMENT));
+        }
+        assertEquals(3, smp.size());
+
+        // a different schema gets its own key
+        assertEquals(BASE + "?xsd=_import2.xsd", utils.mapUri(null, BASE, smp, 
"c.xsd", null, DOCUMENT));
+        assertEquals(BASE + "?xsd=_import2.xsd", utils.mapUri(null, BASE, smp, 
"c.xsd", null, DOCUMENT));
+        assertEquals(4, smp.size());
+    }
+
+    @Test
+    public void testImportKeysConcurrent() throws Exception {
+        // schemas served concurrently must never be given the same key
+        int schemas = 20;
+        Map<String, SchemaReference> smp = new ConcurrentHashMap<>();
+        List<SchemaReference> refs = new ArrayList<>();
+        for (int i = 0; i < schemas; i++) {
+            SchemaReference ref = new SchemaReferenceImpl();
+            refs.add(ref);
+            smp.put("http://localhost/schemas/s"; + i + ".xsd", ref);
+        }
+        WSDLGetUtils utils = new WSDLGetUtils();
+
+        int threads = 8;
+        ExecutorService executor = Executors.newFixedThreadPool(threads);
+        try {
+            CountDownLatch start = new CountDownLatch(1);
+            List<Future<List<String>>> results = new ArrayList<>();
+            for (int t = 0; t < threads; t++) {
+                results.add(executor.submit(() -> {
+                    start.await();
+                    List<String> links = new ArrayList<>();
+                    for (int i = 0; i < schemas; i++) {
+                        links.add(utils.mapUri(null, BASE, smp, "s" + i + 
".xsd", null, DOCUMENT));
+                    }
+                    return links;
+                }));
+            }
+            start.countDown();
+
+            for (Future<List<String>> result : results) {
+                List<String> links = result.get();
+                for (int i = 0; i < schemas; i++) {
+                    String key = links.get(i).substring((BASE + 
"?xsd=").length());
+                    assertSame(refs.get(i), smp.get(key));
+                }
+            }
+        } finally {
+            executor.shutdown();
+        }
+        // at most a few extra keys from requests that raced to create the 
first one for a schema
+        assertTrue(smp.size() <= schemas + schemas * threads);
+    }
+}

Reply via email to