package org.jboss.webservice;
import org.jboss.logging.Logger;
import org.xml.sax.InputSource;
import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLLocator;
import javax.wsdl.xml.WSDLReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class WSDLDefinitionFactory
{
private static final Logger log = Logger.getLogger(WSDLDefinitionFactory.class);
public static final String FEATURE_IMPORT_DOCUMENTS = "javax.wsdl.importDocuments";
public static final String FEATURE_VERBOSE = "javax.wsdl.verbose";
private WSDLReader wsdlReader;
private WSDLDefinitionFactory() throws WSDLException
{
WSDLFactory wsdlFactory = WSDLFactory.newInstance();
wsdlReader = wsdlFactory.newWSDLReader();
}
public static WSDLDefinitionFactory newInstance() throws WSDLException
{
return new WSDLDefinitionFactory();
}
public void setFeature(String name, boolean value) throws IllegalArgumentException
{
wsdlReader.setFeature(name, value);
}
public Definition parse(URL wsdlLocation) throws WSDLException
{
if (wsdlLocation == null)
throw new IllegalArgumentException("URL cannot be null");
Definition wsdlDefinition = wsdlReader.readWSDL(new WSDLLocatorImpl(wsdlLocation));
return wsdlDefinition;
}
public static class WSDLLocatorImpl implements WSDLLocator
{
private URL wsdlURL;
private String latestImportURI;
public WSDLLocatorImpl(URL wsdlFile)
{
if (wsdlFile == null)
throw new IllegalArgumentException("WSDL file argument cannot be null");
this.wsdlURL = wsdlFile;
}
public InputSource getBaseInputSource()
{
log.debug("getBaseInputSource [wsdlUrl=" + wsdlURL + "]");
try
{
InputStream is = wsdlURL.openStream();
if (is == null)
throw new IllegalArgumentException("Cannot obtain wsdl from [" + wsdlURL + "]");
return new InputSource(is);
}
catch (IOException e)
{
throw new RuntimeException("Cannot access wsdl from [" + wsdlURL + "], " + e.getMessage());
}
}
public String getBaseURI()
{
return wsdlURL.toExternalForm();
}
public InputSource getImportInputSource(String parent, String resource)
{
log.debug("getImportInputSource [parent=" + parent + ",resource=" + resource + "]");
URL parentURL = null;
try
{
parentURL = new URL(parent);
}
catch (MalformedURLException e)
{
log.error("Not a valid URL: " + parent);
return null;
}
String wsdlImport = null;
String external = parentURL.toExternalForm();
if (resource.startsWith("http://") || resource.startsWith("https://"))
{
wsdlImport = resource;
}
else if (resource.startsWith("/"))
{
String beforePath = external.substring(0, external.indexOf(parentURL.getPath()));
wsdlImport = beforePath + resource;
}
else
{
String parentDir = external.substring(0, external.lastIndexOf("/"));
while (resource.startsWith("./"))
resource = resource.substring(2);
while (resource.startsWith("../"))
{
parentDir = parentDir.substring(0, parentDir.lastIndexOf("/"));
resource = resource.substring(3);
}
wsdlImport = parentDir + "/" + resource;
}
try
{
log.debug("Resolved to: " + wsdlImport);
InputStream is = new URL(wsdlImport).openStream();
if (is == null)
throw new IllegalArgumentException("Cannot import wsdl from [" + wsdlImport + "]");
latestImportURI = wsdlImport;
return new InputSource(is);
}
catch (IOException e)
{
throw new RuntimeException("Cannot access imported wsdl [" + wsdlImport + "], " + e.getMessage());
}
}
public String getLatestImportURI()
{
return latestImportURI;
}
}
}