Search in sources :

Example 1 with DefinitionImpl

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;
}
Also used : SOAPAddressImpl(com.ibm.wsdl.extensions.soap.SOAPAddressImpl) Types(javax.wsdl.Types) Connector(com.twinsoft.convertigo.beans.core.Connector) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Port(javax.wsdl.Port) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Schema(javax.wsdl.extensions.schema.Schema) WSDLElement(javax.wsdl.WSDLElement) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) OMNode(org.apache.axiom.om.OMNode) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) WSDLWriter(javax.wsdl.xml.WSDLWriter) LinkedHashMap(java.util.LinkedHashMap) DefinitionImpl(com.ibm.wsdl.DefinitionImpl) XmlSchemaWalker(com.twinsoft.convertigo.engine.util.XmlSchemaWalker) XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) StringWriter(java.io.StringWriter) Binding(javax.wsdl.Binding) NamedNodeMap(org.w3c.dom.NamedNodeMap) WSDLException(javax.wsdl.WSDLException) QName(javax.xml.namespace.QName) NodeList(org.w3c.dom.NodeList) Definition(javax.wsdl.Definition) Service(javax.wsdl.Service) Sequence(com.twinsoft.convertigo.beans.core.Sequence) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) ServletException(javax.servlet.ServletException) SOAPException(javax.xml.soap.SOAPException) FileNotFoundException(java.io.FileNotFoundException) EngineException(com.twinsoft.convertigo.engine.EngineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) WSDLException(javax.wsdl.WSDLException) Project(com.twinsoft.convertigo.beans.core.Project) PopulatedExtensionRegistry(com.ibm.wsdl.extensions.PopulatedExtensionRegistry) Transaction(com.twinsoft.convertigo.beans.core.Transaction) XmlSchema(org.apache.ws.commons.schema.XmlSchema) SOAPAddress(javax.wsdl.extensions.soap.SOAPAddress) WSDLWriterImpl(com.ibm.wsdl.xml.WSDLWriterImpl) SOAPBindingImpl(com.ibm.wsdl.extensions.soap.SOAPBindingImpl) PortType(javax.wsdl.PortType)

Aggregations

DefinitionImpl (com.ibm.wsdl.DefinitionImpl)1 PopulatedExtensionRegistry (com.ibm.wsdl.extensions.PopulatedExtensionRegistry)1 SOAPAddressImpl (com.ibm.wsdl.extensions.soap.SOAPAddressImpl)1 SOAPBindingImpl (com.ibm.wsdl.extensions.soap.SOAPBindingImpl)1 WSDLWriterImpl (com.ibm.wsdl.xml.WSDLWriterImpl)1 Connector (com.twinsoft.convertigo.beans.core.Connector)1 Project (com.twinsoft.convertigo.beans.core.Project)1 Sequence (com.twinsoft.convertigo.beans.core.Sequence)1 Transaction (com.twinsoft.convertigo.beans.core.Transaction)1 EngineException (com.twinsoft.convertigo.engine.EngineException)1 XmlSchemaWalker (com.twinsoft.convertigo.engine.util.XmlSchemaWalker)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ServletException (javax.servlet.ServletException)1 Binding (javax.wsdl.Binding)1