This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/simple in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 4c5b504158be50775479f2b7189ecb81997debfa Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Wed Nov 18 10:46:32 2026 +0000 Reuse the import key for schemas served through the catalog fallback --- .../java/org/apache/cxf/frontend/WSDLGetUtils.java | 31 +++++++++--- .../org/apache/cxf/frontend/WSDLGetUtilsTest.java | 59 ++++++++++++++++++++++ 2 files changed, 83 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 2394513a99f..5b6c04268a5 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) { + synchronized (smp) { + for (Map.Entry<String, SchemaReference> entry : smp.entrySet()) { + if (entry.getValue() == ref && entry.getKey().startsWith("_import")) { + return entry.getKey(); + } + } + int count = 1; + while (smp.containsKey("_import" + count + ".xsd")) { + count++; + } + String key = "_import" + count + ".xsd"; + smp.put(key, 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..5949c1deb2f --- /dev/null +++ b/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/WSDLGetUtilsTest.java @@ -0,0 +1,59 @@ +/** + * 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.Map; +import java.util.concurrent.ConcurrentHashMap; + +import javax.wsdl.extensions.schema.SchemaReference; + +import com.ibm.wsdl.extensions.schema.SchemaReferenceImpl; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +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()); + } +}
