use of com.ibm.wsdl.DefinitionImpl in project convertigo by convertigo.
the class WebServiceServlet method generateWsdlForDocLiteral.
public static String generateWsdlForDocLiteral(String servletURI, String projectName) throws EngineException {
Engine.logEngine.debug("(WebServiceServlet) Generating WSDL...");
long timeStart = System.currentTimeMillis();
String locationPath = servletURI.substring(0, servletURI.indexOf("/.w"));
String targetNamespace = Project.CONVERTIGO_PROJECTS_NAMESPACEURI + projectName;
Project project = null;
// server mode
if (Engine.isEngineMode()) {
project = Engine.theApp.databaseObjectsManager.getProjectByName(projectName);
targetNamespace = project.getTargetNamespace();
} else // studio mode
{
project = Engine.objectsProvider.getProject(projectName);
targetNamespace = project.getTargetNamespace();
}
// Create WSDL definition
Definition definition = new DefinitionImpl();
definition.setExtensionRegistry(new PopulatedExtensionRegistry());
definition.setTargetNamespace(targetNamespace);
definition.setQName(new QName(targetNamespace, projectName));
definition.addNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");
definition.addNamespace("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
definition.addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
definition.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
definition.addNamespace(projectName + "_ns", targetNamespace);
// Add WSDL Types
Types types = definition.createTypes();
definition.setTypes(types);
// Add WSDL service
Service service = definition.createService();
service.setQName(new QName(targetNamespace, projectName));
definition.addService(service);
// Add WSDL binding
PortType portType = definition.createPortType();
portType.setQName(new QName(targetNamespace, projectName + "PortType"));
portType.setUndefined(false);
definition.addPortType(portType);
SOAPBindingImpl soapBinding = new SOAPBindingImpl();
soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");
soapBinding.setStyle("document");
Binding binding = definition.createBinding();
binding.setQName(new QName(targetNamespace, projectName + "SOAPBinding"));
binding.setUndefined(false);
definition.addBinding(binding);
binding.addExtensibilityElement(soapBinding);
binding.setPortType(portType);
// Add WSDL port
SOAPAddress soapAddress = new SOAPAddressImpl();
soapAddress.setLocationURI(locationPath + "/.wsl");
Port port = definition.createPort();
port.setName(projectName + "SOAP");
port.addExtensibilityElement(soapAddress);
port.setBinding(binding);
service.addPort(port);
// Add all WSDL operations
// remember qnames for accessibility
List<QName> partElementQNames = new ArrayList<QName>();
if (project != null) {
for (Connector connector : project.getConnectorsList()) {
for (Transaction transaction : connector.getTransactionsList()) {
if (transaction.isPublicAccessibility()) {
addWsdlOperation(definition, transaction, partElementQNames);
}
}
}
for (Sequence sequence : project.getSequencesList()) {
if (sequence.isPublicAccessibility()) {
addWsdlOperation(definition, sequence, partElementQNames);
}
}
}
// Add all schemas of project under WSDL Types
try {
// Retrieve the only needed schema objects map
XmlSchemaCollection xmlSchemaCollection = Engine.theApp.schemaManager.getSchemasForProject(projectName);
XmlSchema projectSchema = xmlSchemaCollection.getXmlSchema(targetNamespace)[0];
LinkedHashMap<QName, XmlSchemaObject> map = new LinkedHashMap<QName, XmlSchemaObject>();
XmlSchemaWalker dw = XmlSchemaWalker.newDependencyWalker(map, true, true);
for (QName qname : partElementQNames) {
dw.walkByElementRef(projectSchema, qname);
}
if (Engine.logEngine.isTraceEnabled()) {
String message = "";
for (QName qname : map.keySet()) {
message += "\n\t" + qname.toString();
}
Engine.logEngine.trace("(WebServiceServlet) needed schema objects :" + message);
}
// Read schemas into a new Collection in order to modify them
XmlSchemaCollection wsdlSchemaCollection = new XmlSchemaCollection();
for (XmlSchema xmlSchema : xmlSchemaCollection.getXmlSchemas()) {
String tns = xmlSchema.getTargetNamespace();
if (tns.equals(Constants.URI_2001_SCHEMA_XSD))
continue;
if (tns.equals(SchemaUtils.URI_SOAP_ENC))
continue;
if (wsdlSchemaCollection.schemaForNamespace(tns) == null) {
wsdlSchemaCollection.read(xmlSchema.getSchemaDocument(), xmlSchema.getSourceURI(), null);
}
}
// Modify schemas and add them
Map<String, Schema> schemaMap = new HashMap<String, Schema>();
for (XmlSchema xmlSchema : wsdlSchemaCollection.getXmlSchemas()) {
if (xmlSchema.getTargetNamespace().equals(Constants.URI_2001_SCHEMA_XSD))
continue;
if (xmlSchema.getTargetNamespace().equals(SchemaUtils.URI_SOAP_ENC))
continue;
String tns = xmlSchema.getTargetNamespace();
// Reduce schema to needed objects
reduceSchema(xmlSchema, map.keySet());
// Retrieve schema Element
Schema wsdlSchema = schemaMap.get(tns);
if (wsdlSchema == null) {
wsdlSchema = (Schema) definition.getExtensionRegistry().createExtension(Types.class, SchemaConstants.Q_ELEM_XSD_2001);
Element schemaElt = xmlSchema.getSchemaDocument().getDocumentElement();
// Remove 'schemaLocation' attribute on imports
NodeList importList = schemaElt.getElementsByTagNameNS(Constants.URI_2001_SCHEMA_XSD, "import");
if (importList.getLength() > 0) {
for (int i = 0; i < importList.getLength(); i++) {
Element importElt = (Element) importList.item(i);
importElt.removeAttribute("schemaLocation");
}
}
// Remove includes
NodeList includeList = schemaElt.getElementsByTagNameNS(Constants.URI_2001_SCHEMA_XSD, "include");
if (includeList.getLength() > 0) {
for (int i = 0; i < includeList.getLength(); i++) {
schemaElt.removeChild(includeList.item(i));
}
}
// Add schema Element
schemaMap.put(tns, wsdlSchema);
wsdlSchema.setElement(schemaElt);
types.addExtensibilityElement(wsdlSchema);
} else {
// case of schema include (same targetNamespace) or same schema
Element schemaElt = wsdlSchema.getElement();
// Add missing attributes
NamedNodeMap attributeMap = xmlSchema.getSchemaDocument().getDocumentElement().getAttributes();
for (int i = 0; i < attributeMap.getLength(); i++) {
Node node = attributeMap.item(i);
if (schemaElt.getAttributes().getNamedItem(node.getNodeName()) == null) {
schemaElt.setAttribute(node.getNodeName(), node.getNodeValue());
}
}
// Add children
NodeList children = xmlSchema.getSchemaDocument().getDocumentElement().getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
// Special cases
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) node;
// Do not add include
if (el.getTagName().endsWith("include"))
continue;
// Add import at first
if (el.getTagName().endsWith("import")) {
String ins = el.getAttribute("namespace");
if (!tns.equals(ins) && el.hasAttribute("schemaLocation")) {
if (!hasElement(schemaElt, el.getLocalName(), ins))
schemaElt.insertBefore(schemaElt.getOwnerDocument().importNode(el, true), schemaElt.getFirstChild());
continue;
}
}
// Else
if (!hasElement(schemaElt, el.getLocalName(), el.getAttribute("name")))
schemaElt.appendChild(schemaElt.getOwnerDocument().importNode(el, true));
} else {
// Others
schemaElt.appendChild(schemaElt.getOwnerDocument().importNode(node, true));
}
}
}
}
} catch (Exception e1) {
Engine.logEngine.error("An error occured while adding schemas for WSDL", e1);
}
// Write WSDL to string
WSDLWriter wsdlWriter = new WSDLWriterImpl();
StringWriter sw = new StringWriter();
try {
wsdlWriter.writeWSDL(definition, sw);
} catch (WSDLException e) {
Engine.logEngine.error("An error occured while generating WSDL", e);
}
String wsdl = sw.toString();
long timeStop = System.currentTimeMillis();
System.out.println("Wsdl for " + projectName + " | Times >> total : " + (timeStop - timeStart) + " ms");
return wsdl;
}
Aggregations