Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3D6F4180D5 for ; Fri, 22 May 2015 19:19:31 +0000 (UTC) Received: (qmail 81365 invoked by uid 500); 22 May 2015 19:19:31 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 81025 invoked by uid 500); 22 May 2015 19:19:30 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 79588 invoked by uid 99); 22 May 2015 19:19:29 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 22 May 2015 19:19:29 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id ABEADE0282; Fri, 22 May 2015 19:19:29 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: dkulp@apache.org To: commits@cxf.apache.org Date: Fri, 22 May 2015 19:19:47 -0000 Message-Id: In-Reply-To: <4b8088be92c54f5482b711c2104fe775@git.apache.org> References: <4b8088be92c54f5482b711c2104fe775@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [19/20] cxf git commit: [CXF-6392] Schema imports are not handled correctly in generated WSDL and XSD files [CXF-6392] Schema imports are not handled correctly in generated WSDL and XSD files - proposed fix Closes #68 Closes #69 Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/a886f067 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/a886f067 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/a886f067 Branch: refs/heads/3.0.x-fixes Commit: a886f06714acd039e44b5a9eed42a69eff1484d5 Parents: 04d30cc Author: Tomas Hofman Authored: Wed May 6 16:48:18 2015 +0200 Committer: Daniel Kulp Committed: Fri May 22 14:38:36 2015 -0400 ---------------------------------------------------------------------- .../org/apache/cxf/frontend/WSDLGetUtils.java | 118 ++++++++++--------- 1 file changed, 60 insertions(+), 58 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/a886f067/rt/frontend/simple/src/main/java/org/apache/cxf/frontend/WSDLGetUtils.java ---------------------------------------------------------------------- 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 8ab1437..9213e67 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 @@ -72,7 +72,7 @@ import org.apache.cxf.wsdl11.ResourceManagerWSDLLocator; import org.apache.cxf.wsdl11.ServiceWSDLBuilder; /** - * + * */ public class WSDLGetUtils { @@ -83,9 +83,9 @@ public class WSDLGetUtils { private static final String WSDLS_KEY = WSDLGetUtils.class.getName() + ".WSDLs"; private static final String SCHEMAS_KEY = WSDLGetUtils.class.getName() + ".Schemas"; - + private static final Logger LOG = LogUtils.getL7dLogger(WSDLGetInterceptor.class); - + public WSDLGetUtils() { } @@ -96,10 +96,10 @@ public class WSDLGetUtils { EndpointInfo endpointInfo) { Map params = new HashMap(); params.put("wsdl", ""); - getDocument(message, base, - params, ctxUri, + getDocument(message, base, + params, ctxUri, endpointInfo); - + Map mp = CastUtils.cast((Map)endpointInfo.getService() .getProperty(WSDLS_KEY)); return mp.keySet(); @@ -110,19 +110,19 @@ public class WSDLGetUtils { EndpointInfo endpointInfo) { Map params = new HashMap(); params.put("wsdl", ""); - getDocument(message, base, - params, ctxUri, + getDocument(message, base, + params, ctxUri, endpointInfo); - + Map mp = CastUtils.cast((Map)endpointInfo.getService() .getProperty(SCHEMAS_KEY)); - + Map schemas = new HashMap(); for (Map.Entry ent : mp.entrySet()) { params.clear(); params.put("xsd", ent.getKey()); Document doc = getDocument(message, base, params, ctxUri, endpointInfo); - schemas.put(doc.getDocumentElement().getAttribute("targetNamespace"), + schemas.put(doc.getDocumentElement().getAttribute("targetNamespace"), buildUrl(base, ctxUri, "xsd=" + ent.getKey())); } return schemas; @@ -161,16 +161,25 @@ public class WSDLGetUtils { } return doc; } - - protected String mapUri(String base, Map smp, String loc, String xsd) + + protected String mapUri(Bus bus, String base, Map smp, String loc, String xsd) throws UnsupportedEncodingException { String key = loc; try { boolean absoluteLocUri = new URI(loc).isAbsolute(); - if (!absoluteLocUri && xsd != null) { + if (!absoluteLocUri && xsd != null) { // XSD request + // resolve requested location with relative import path key = new URI(xsd).resolve(loc).toString(); - } - if (!absoluteLocUri && xsd == null) { + + if (!smp.containsKey(URLDecoder.decode(key, "utf-8"))) { + // if the result is not known, check if we can resolve it into something known + String resolved = resolveWithCatalogs(OASISCatalogManager.getCatalogManager(bus), key, base); + if (smp.containsKey(URLDecoder.decode(resolved, "utf-8"))) { + // if it is resolvable, we can use it + return base + "?xsd=" + key.replace(" ", "%20"); + } + } + } else if (!absoluteLocUri && xsd == null) { // WSDL request key = new URI(".").resolve(loc).toString(); } } catch (URISyntaxException e) { @@ -190,6 +199,7 @@ public class WSDLGetUtils { Message message, String xsd, String wsdl) { + Bus bus = message.getExchange().getBus(); List elementList = null; try { @@ -197,7 +207,7 @@ public class WSDLGetUtils { "http://www.w3.org/2001/XMLSchema", "import"); for (Element el : elementList) { String sl = el.getAttribute("schemaLocation"); - sl = mapUri(base, smp, sl, xsd); + sl = mapUri(bus, base, smp, sl, xsd); if (sl != null) { el.setAttribute("schemaLocation", sl); } @@ -208,7 +218,7 @@ public class WSDLGetUtils { "include"); for (Element el : elementList) { String sl = el.getAttribute("schemaLocation"); - sl = mapUri(base, smp, sl, xsd); + sl = mapUri(bus, base, smp, sl, xsd); if (sl != null) { el.setAttribute("schemaLocation", sl); } @@ -218,7 +228,7 @@ public class WSDLGetUtils { "redefine"); for (Element el : elementList) { String sl = el.getAttribute("schemaLocation"); - sl = mapUri(base, smp, sl, xsd); + sl = mapUri(bus, base, smp, sl, xsd); if (sl != null) { el.setAttribute("schemaLocation", sl); } @@ -393,7 +403,7 @@ public class WSDLGetUtils { : CastUtils.cast(types.getExtensibilityElements(), ExtensibilityElement.class)) { if (el instanceof Schema) { Schema see = (Schema)el; - updateSchemaImports(bus, see, see.getDocumentBaseURI(), doneSchemas, base, null); + updateSchemaImports(bus, see, see.getDocumentBaseURI(), doneSchemas, base); } } } @@ -450,8 +460,7 @@ public class WSDLGetUtils { Schema schema, String docBase, Map doneSchemas, - String base, - String parent) { + String base) { OASISCatalogManager catalogs = OASISCatalogManager.getCatalogManager(bus); Collection> imports = CastUtils.cast((Collection)schema.getImports().values()); for (List lst : imports) { @@ -476,36 +485,23 @@ public class WSDLGetUtils { String resolvedSchemaLocation = resolveWithCatalogs(catalogs, start, base); if (resolvedSchemaLocation == null) { resolvedSchemaLocation = resolveWithCatalogs(catalogs, imp.getSchemaLocationURI(), base); - } + } if (resolvedSchemaLocation == null) { try { //check to see if it's already in a URL format. If so, leave it. new URL(start); } catch (MalformedURLException e) { if (doneSchemas.put(decodedStart, imp) == null) { - try { - //CHECKSTYLE:OFF:NestedIfDepth - if (!(new URI(decodedStart).isAbsolute()) && parent != null) { - resolvedSchemaLocation = new URI(parent).resolve(decodedStart).toString(); - decodedStart = URLDecoder.decode(resolvedSchemaLocation, "utf-8"); - doneSchemas.put(resolvedSchemaLocation, imp); - } - //CHECKSTYLE:ON:NestedIfDepth - } catch (URISyntaxException ex) { - // ignore - } catch (UnsupportedEncodingException ex) { - // ignore - } + putResolvedSchemaLocationIfRelative(doneSchemas, decodedStart, imp); updateSchemaImports(bus, imp.getReferencedSchema(), docBase, - doneSchemas, base, decodedStart); + doneSchemas, base); } } } else { if (doneSchemas.put(decodedStart, imp) == null) { doneSchemas.put(resolvedSchemaLocation, imp); doneSchemas.put(imp.getSchemaLocationURI(), imp); - updateSchemaImports(bus, imp.getReferencedSchema(), docBase, - doneSchemas, base, decodedStart); + updateSchemaImports(bus, imp.getReferencedSchema(), docBase, doneSchemas, base); } } } @@ -538,8 +534,8 @@ public class WSDLGetUtils { new URL(start); } catch (MalformedURLException e) { if (doneSchemas.put(decodedStart, included) == null) { - updateSchemaImports(bus, included.getReferencedSchema(), - docBase, doneSchemas, base, decodedStart); + putResolvedSchemaLocationIfRelative(doneSchemas, decodedStart, included); + updateSchemaImports(bus, included.getReferencedSchema(), docBase, doneSchemas, base); } } } @@ -547,7 +543,7 @@ public class WSDLGetUtils { || !doneSchemas.containsKey(resolvedSchemaLocation)) { doneSchemas.put(decodedStart, included); doneSchemas.put(resolvedSchemaLocation, included); - updateSchemaImports(bus, included.getReferencedSchema(), docBase, doneSchemas, base, decodedStart); + updateSchemaImports(bus, included.getReferencedSchema(), docBase, doneSchemas, base); } } } @@ -576,21 +572,8 @@ public class WSDLGetUtils { new URL(start); } catch (MalformedURLException e) { if (doneSchemas.put(decodedStart, included) == null) { - try { - //CHECKSTYLE:OFF:NestedIfDepth - if (!(new URI(decodedStart).isAbsolute()) && parent != null) { - resolvedSchemaLocation = new URI(parent).resolve(decodedStart).toString(); - decodedStart = URLDecoder.decode(resolvedSchemaLocation, "utf-8"); - doneSchemas.put(resolvedSchemaLocation, included); - } - //CHECKSTYLE:ON:NestedIfDepth - } catch (URISyntaxException ex) { - // ignore - } catch (UnsupportedEncodingException ex) { - // ignore - } - updateSchemaImports(bus, included.getReferencedSchema(), - docBase, doneSchemas, base, decodedStart); + putResolvedSchemaLocationIfRelative(doneSchemas, decodedStart, included); + updateSchemaImports(bus, included.getReferencedSchema(), docBase, doneSchemas, base); } } } @@ -598,12 +581,31 @@ public class WSDLGetUtils { || !doneSchemas.containsKey(resolvedSchemaLocation)) { doneSchemas.put(decodedStart, included); doneSchemas.put(resolvedSchemaLocation, included); - updateSchemaImports(bus, included.getReferencedSchema(), docBase, doneSchemas, base, decodedStart); + updateSchemaImports(bus, included.getReferencedSchema(), docBase, doneSchemas, base); } } } } + /** + * If given decodedStart is relative path, resolves a real location of given schema and puts it into schema map. + * + * @param doneSchemas schema map + * @param decodedStart path referencing schema + * @param schemaReference referenced schema + */ + private void putResolvedSchemaLocationIfRelative(Map doneSchemas, String decodedStart, + SchemaReference schemaReference) { + try { + if (!(new URI(decodedStart).isAbsolute())) { + String resolved = schemaReference.getReferencedSchema().getDocumentBaseURI(); + doneSchemas.put(resolved, schemaReference); + } + } catch (URISyntaxException ex) { + // ignore + } + } + private String findSchemaLocation(Map doneSchemas, SchemaReference imp, String docBase) {