diff options
author | ramkumar <ramkumar@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-10 12:13:16 +0000 |
---|---|---|
committer | ramkumar <ramkumar@13f79535-47bb-0310-9956-ffa450edef68> | 2009-08-10 12:13:16 +0000 |
commit | 4155cfcbba228aea45c3bb8842018dbff61ef935 (patch) | |
tree | aa6249879b30ef071375d3b059ea5c3fa9fd992f /branches | |
parent | 18f417e28ce1122b8566073e46407f57eaf68066 (diff) |
Fixes for TUSCANY-2906 - to resolve wsdl:import and xsd:import using namespace
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@802758 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'branches')
6 files changed, 225 insertions, 32 deletions
diff --git a/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld-import.wsdl b/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld-import.wsdl new file mode 100644 index 0000000000..2920c43a31 --- /dev/null +++ b/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld-import.wsdl @@ -0,0 +1,57 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * 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.
+-->
+
+<wsdl:definitions targetNamespace="http://helloworld3"
+ xmlns:tns="http://helloworld3"
+ xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
+ xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ name="helloworld-import">
+
+ <wsdl:types>
+ <schema xmlns="http://www.w3.org/2001/XMLSchema"
+ xmlns:tns2="http://helloworld2"
+ targetNamespace="http://helloworld3"
+ elementFormDefault="qualified">
+
+ <xsd:import namespace="http://helloworld2" schemaLocation="../../helloworld.xsd" />
+
+ <element name="getGreetings" type="tns2:getGreetingsType"/>
+ <element name="getGreetingsResponse" type="tns2:getGreetingsResponseType"/>
+
+ </schema>
+ </wsdl:types>
+
+ <wsdl:message name="getGreetingsRequest">
+ <wsdl:part element="tns:getGreetings" name="parameters"/>
+ </wsdl:message>
+
+ <wsdl:message name="getGreetingsResponse">
+ <wsdl:part element="tns:getGreetingsResponse" name="parameters"/>
+ </wsdl:message>
+
+ <wsdl:portType name="HelloWorld">
+ <wsdl:operation name="getGreetings">
+ <wsdl:input message="tns:getGreetingsRequest" name="getGreetingsRequest"/>
+ <wsdl:output message="tns:getGreetingsResponse" name="getGreetingsResponse"/>
+ </wsdl:operation>
+ </wsdl:portType>
+
+</wsdl:definitions>
diff --git a/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld.xsd b/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld.xsd new file mode 100644 index 0000000000..d49e2ead99 --- /dev/null +++ b/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld.xsd @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<schema targetNamespace="http://helloworld2" + xmlns="http://www.w3.org/2001/XMLSchema" + xmlns:ns="http://helloworld2"> + + <complexType name="getGreetingsType"> + <sequence> + <element name="name" type="string"/> + </sequence> + </complexType> + + <complexType name="getGreetingsResponseType"> + <sequence> + <element name="getGreetingsReturn" type="string"/> + </sequence> + </complexType> +</schema>
\ No newline at end of file diff --git a/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworld.wsdl b/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworld.wsdl index 0a4d0d9908..90eeae3c8a 100644 --- a/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworld.wsdl +++ b/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworld.wsdl @@ -26,7 +26,7 @@ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name="helloworld">
- <wsdl:import namespace="http://helloworld3" location="../../META-INF/wsdl/helloworld-import.wsdl" />
+ <wsdl:import namespace="http://helloworld3" location="../../helloworld-import.wsdl" />
<wsdl:binding name="HelloWorldSoapBinding" type="tns3:HelloWorld">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
diff --git a/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworldws.composite b/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworldws.composite index 56b13e9d02..1af51162ac 100644 --- a/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworldws.composite +++ b/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworldws.composite @@ -24,7 +24,7 @@ <component name="HelloWorldServiceComponent">
<service name="HelloWorldService">
- <interface.wsdl interface="http://helloworld3#wsdl.interface(HelloWorld)" />
+ <interface.java interface="helloworld.HelloWorldService"/>
<binding.ws wsdlElement="http://helloworld#wsdl.port(HelloWorldService/HelloWorldSoapPort)" />
</service>
<implementation.java class="helloworld.HelloWorldImpl" />
diff --git a/branches/sca-java-1.5.1/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java b/branches/sca-java-1.5.1/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java index daddf4a747..d2ed43e7f0 100644 --- a/branches/sca-java-1.5.1/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java +++ b/branches/sca-java-1.5.1/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java @@ -46,6 +46,10 @@ import javax.wsdl.extensions.schema.Schema; import javax.wsdl.xml.WSDLLocator; import javax.wsdl.xml.WSDLReader; import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; import org.apache.tuscany.sca.contribution.Artifact; import org.apache.tuscany.sca.contribution.Contribution; @@ -58,6 +62,7 @@ import org.apache.tuscany.sca.contribution.resolver.ModelResolver; import org.apache.tuscany.sca.contribution.service.ContributionReadException; import org.apache.tuscany.sca.contribution.service.ContributionRuntimeException; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; +import org.apache.tuscany.sca.interfacedef.wsdl.impl.WSDLDefinitionImpl; import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; import org.apache.tuscany.sca.xsd.XSDFactory; import org.apache.tuscany.sca.xsd.XSDefinition; @@ -77,6 +82,7 @@ import org.xml.sax.InputSource; public class WSDLModelResolver implements ModelResolver { //Schema element names public static final String ELEM_SCHEMA = "schema"; + public static final QName WSDL11_IMPORT = new QName("http://schemas.xmlsoap.org/wsdl/", "import"); //Schema URI public static final String NS_URI_XSD_1999 = "http://www.w3.org/1999/XMLSchema"; @@ -150,10 +156,12 @@ public class WSDLModelResolver implements ModelResolver { private InputStream inputStream; private URL base; private String latestImportURI; + private Map<String, String> wsdlImports; - public WSDLLocatorImpl(URL base, InputStream is) { + public WSDLLocatorImpl(URL base, InputStream is, Map<String, String> imports) { this.base = base; this.inputStream = is; + this.wsdlImports = imports; } public void close() { @@ -177,13 +185,14 @@ public class WSDLModelResolver implements ModelResolver { } public InputSource getImportInputSource(String parentLocation, String importLocation) { - try { + try { + if (importLocation == null) throw new IllegalArgumentException("Required attribute 'location' is missing."); if (importLocation.trim().equals("")) throw new IllegalArgumentException("Required attribute 'location' is empty."); - + URL url = null; if (importLocation.startsWith("/")) { // The URI is relative to the contribution @@ -208,7 +217,26 @@ public class WSDLModelResolver implements ModelResolver { latestImportURI = url.toString(); return XMLDocumentHelper.getInputSource(url); } catch (IOException e) { - throw new ContributionRuntimeException(e); + // If we are not able to resolve the imports using location, then + // try resolving them using the namespace. + try { + if (! wsdlImports.isEmpty()) { + for (Artifact artifact : contribution.getArtifacts()) { + if (artifact.getModel() instanceof WSDLDefinitionImpl) { + String namespace = ((WSDLDefinitionImpl)artifact.getModel()).getNamespace(); + for (Map.Entry<String, String> entry : ((Map<String, String>)wsdlImports).entrySet()) { + if (entry.getKey().equals(namespace)) { + URL url = ((WSDLDefinitionImpl)artifact.getModel()).getLocation().toURL(); + return XMLDocumentHelper.getInputSource(url); + } + } + } + } + } + } catch (IOException ex) { + throw new ContributionRuntimeException(ex); + } + throw new ContributionRuntimeException(e); } } @@ -332,7 +360,7 @@ public class WSDLModelResolver implements ModelResolver { // Use non-sca mechanism to resolve the import location, if not // found then use the sca mechanism - private <T> T resolveImports (Class<T> modelClass, WSDLDefinition unresolved) { + private <T> T resolveImports (Class<T> modelClass, WSDLDefinition unresolved) throws ContributionReadException { WSDLDefinition resolved = null; if (unresolved.getDefinition() == null && unresolved.getLocation() != null) { @@ -350,11 +378,20 @@ public class WSDLModelResolver implements ModelResolver { return modelClass.cast(resolved); } } catch (ContributionReadException e) { - // Load the definition using sca mechanism. - resolved = resolveModel(WSDLDefinition.class, unresolved); - if (resolved != null && !resolved.isUnresolved()) { - return modelClass.cast(resolved); - } + // Resolve the wsdl definition using the namespace, by searching the + // contribution artifacts for wsdl definition for the given namespace. + for (Artifact artifact : contribution.getArtifacts()) { + if (artifact.getModel() instanceof WSDLDefinitionImpl) { + String namespace = ((WSDLDefinitionImpl)artifact.getModel()).getNamespace(); + if (unresolved.getNamespace().equals(namespace)) { + WSDLDefinition wsdlDefinition = (WSDLDefinition)artifact.getModel(); + if (wsdlDefinition.getDefinition() == null) { + loadDefinition(wsdlDefinition); + } + return modelClass.cast(wsdlDefinition); + } + } + } } } @@ -381,11 +418,14 @@ public class WSDLModelResolver implements ModelResolver { // FIXME: We need to decide if we should disable the import processing by WSDL4J // reader.setFeature("javax.wsdl.importDocuments", false); reader.setExtensionRegistry(wsdlExtensionRegistry); // use a custom registry - - WSDLLocatorImpl locator = new WSDLLocatorImpl(artifactURL, is); - Definition definition = reader.readWSDL(locator); + + // Collection of namespace,location for wsdl:import definition + Map<String, String> wsdlImports = indexRead(wsdlDef.getLocation().toURL()); + + WSDLLocatorImpl locator = new WSDLLocatorImpl(artifactURL, is, wsdlImports); + Definition definition = reader.readWSDL(locator); wsdlDef.setDefinition(definition); - + // If this definition imports any definitions from other namespaces, // set the correct WSDLDefinition import relationships. for (Map.Entry<String, List<javax.wsdl.Import>> entry : @@ -396,37 +436,43 @@ public class WSDLModelResolver implements ModelResolver { wsdlDefinition.setNamespace(entry.getKey()); WSDLDefinition resolved = null; for (javax.wsdl.Import imp : entry.getValue()) { - if (imp.getDefinition() == null) - throw new IllegalArgumentException("Required attribute 'location' is missing."); - + if (imp.getDefinition() == null) { + throw new IllegalArgumentException("Required attribute 'location' is missing."); + } try { - wsdlDefinition.setLocation(new URI(imp.getDefinition().getDocumentBaseURI())); + wsdlDefinition.setLocation(new URI(imp.getDefinition().getDocumentBaseURI())); resolved = resolveImports(WSDLDefinition.class, wsdlDefinition); if (!resolved.isUnresolved()) { - if (resolved.getDefinition().getDocumentBaseURI().equals(imp.getDefinition().getDocumentBaseURI())) { - // this WSDLDefinition contains the imported document - wsdlDef.getImportedDefinitions().add(resolved); - } else { - // this is a facade, so look in its imported definitions - for (WSDLDefinition def : resolved.getImportedDefinitions()) { - if (def.getDefinition().getDocumentBaseURI().equals(imp.getDefinition().getDocumentBaseURI())) { + if (resolved.getImportedDefinitions().isEmpty()) { + if (resolved.getDefinition().getTargetNamespace().equals(imp.getDefinition().getTargetNamespace())) { + // this WSDLDefinition contains the imported document + wsdlDef.getImportedDefinitions().add(resolved); + imp.setLocationURI(resolved.getURI().toString()); + } + } else { + // this is a facade, so look in its imported definitions + for (WSDLDefinition def : resolved.getImportedDefinitions()) { + if (def.getDefinition().getTargetNamespace().equals(imp.getDefinition().getTargetNamespace())) { wsdlDef.getImportedDefinitions().add(def); + imp.setLocationURI(def.getURI().toString()); break; } } - } + } } } catch (Exception e) { throw new ContributionReadException(e); } } } - } - + } + //Read inline schemas readInlineSchemas(wsdlDef, definition); } catch (WSDLException e) { throw new ContributionReadException(e); + } catch (XMLStreamException e) { + throw new ContributionReadException(e); } catch (IOException e) { throw new ContributionReadException(e); } @@ -513,5 +559,42 @@ public class WSDLModelResolver implements ModelResolver { } } } - + + + /** + * Read the namespace and location for the WSDL imports + * + * @param doc + * @return + * @throws IOException + * @throws XMLStreamException + */ + protected Map<String, String> indexRead(URL doc) throws IOException, XMLStreamException { + + Map<String, String> wsdlImports = new HashMap<String, String>(); + InputStream is = doc.openStream(); + try { + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + int eventType = reader.getEventType(); + int index = 0; + while (true) { + if (eventType == XMLStreamConstants.START_ELEMENT) { + if (WSDL11_IMPORT.equals(reader.getName())) { + String ns = reader.getAttributeValue(null, "namespace"); + String loc = reader.getAttributeValue(null, "location"); + wsdlImports.put(ns, loc); + } + } + if (reader.hasNext()) { + eventType = reader.next(); + } else { + break; + } + } + return wsdlImports; + } finally { + is.close(); + } + } } diff --git a/branches/sca-java-1.5.1/modules/xsd-xml/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java b/branches/sca-java-1.5.1/modules/xsd-xml/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java index 51b275d0ae..fb6e8eac20 100644 --- a/branches/sca-java-1.5.1/modules/xsd-xml/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java +++ b/branches/sca-java-1.5.1/modules/xsd-xml/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java @@ -37,6 +37,7 @@ import org.apache.tuscany.sca.contribution.service.ContributionRuntimeException; import org.apache.tuscany.sca.xsd.DefaultXSDFactory;
import org.apache.tuscany.sca.xsd.XSDFactory;
import org.apache.tuscany.sca.xsd.XSDefinition;
+import org.apache.tuscany.sca.xsd.impl.XSDefinitionImpl;
import org.apache.ws.commons.schema.XmlSchema;
import org.apache.ws.commons.schema.XmlSchemaCollection;
import org.apache.ws.commons.schema.XmlSchemaInclude;
@@ -314,7 +315,24 @@ public class XSDModelResolver implements ModelResolver { url = new URL(new URL(baseUri), schemaLocation);
}
return XMLDocumentHelper.getInputSource(url);
- } catch (IOException e) {
+ } catch (IOException e) {
+ // If we are not able to resolve the imports using location, then
+ // try resolving them using the namespace.
+ try {
+ for (Artifact artifact : contribution.getArtifacts()) {
+ if (artifact.getModel() instanceof XSDefinitionImpl) {
+ String artifactNamespace = ((XSDefinitionImpl)artifact.getModel()).getNamespace();
+ if (targetNamespace.equals(artifactNamespace)) {
+ URL artifactLocation = ((XSDefinitionImpl)artifact.getModel()).getLocation().toURL();
+ return XMLDocumentHelper.getInputSource(artifactLocation);
+ }
+ }
+ }
+ } catch (IOException ex) {
+ // Invalid URI; return a default InputSource so that the
+ // XmlSchema code will produce a useful diagnostic
+ return new InputSource(schemaLocation);
+ }
// Invalid URI; return a default InputSource so that the
// XmlSchema code will produce a useful diagnostic
return new InputSource(schemaLocation);
|