Search in sources :

Example 1 with MessagePart

use of com.eviware.soapui.model.iface.MessagePart in project convertigo by convertigo.

the class WsReference method createSoapTransaction.

private static XmlHttpTransaction createSoapTransaction(XmlSchema xmlSchema, WsdlInterface iface, WsdlOperation operation, Project project, HttpConnector httpConnector) throws ParserConfigurationException, SAXException, IOException, EngineException {
    XmlHttpTransaction xmlHttpTransaction = null;
    WsdlRequest request;
    String requestXml;
    String transactionName, comment;
    String operationName;
    if (operation != null) {
        comment = operation.getDescription();
        try {
            comment = (comment.equals("") ? operation.getBindingOperation().getDocumentationElement().getTextContent() : comment);
        } catch (Exception e) {
        }
        operationName = operation.getName();
        transactionName = StringUtils.normalize("C" + operationName);
        xmlHttpTransaction = new XmlHttpTransaction();
        xmlHttpTransaction.bNew = true;
        xmlHttpTransaction.setHttpVerb(HttpMethodType.POST);
        xmlHttpTransaction.setName(transactionName);
        xmlHttpTransaction.setComment(comment);
        // Set encoding (UTF-8 by default)
        xmlHttpTransaction.setEncodingCharSet("UTF-8");
        xmlHttpTransaction.setXmlEncoding("UTF-8");
        // Ignore SOAP elements in response
        xmlHttpTransaction.setIgnoreSoapEnveloppe(true);
        // Adds parameters
        XMLVector<XMLVector<String>> parameters = new XMLVector<XMLVector<String>>();
        XMLVector<String> xmlv;
        xmlv = new XMLVector<String>();
        xmlv.add(HeaderName.ContentType.value());
        xmlv.add(MimeType.TextXml.value());
        parameters.add(xmlv);
        xmlv = new XMLVector<String>();
        xmlv.add("Host");
        xmlv.add(httpConnector.getServer());
        parameters.add(xmlv);
        xmlv = new XMLVector<String>();
        xmlv.add("SOAPAction");
        // fix #4215 - SOAPAction header must be empty
        xmlv.add("");
        parameters.add(xmlv);
        xmlv = new XMLVector<String>();
        xmlv.add("user-agent");
        xmlv.add("Convertigo EMS " + Version.fullProductVersion);
        parameters.add(xmlv);
        xmlHttpTransaction.setHttpParameters(parameters);
        QName qname = null;
        boolean bRPC = false;
        String style = operation.getStyle();
        if (style.toUpperCase().equals("RPC"))
            bRPC = true;
        // Set SOAP response element
        if (bRPC) {
            try {
                MessagePart[] parts = operation.getDefaultResponseParts();
                if (parts.length > 0) {
                    String ename = parts[0].getName();
                    if (parts[0].getPartType().name().equals("CONTENT")) {
                        MessagePart.ContentPart mpcp = (MessagePart.ContentPart) parts[0];
                        qname = mpcp.getSchemaType().getName();
                        if (qname != null) {
                            // response is based on an element defined with a type
                            // operationResponse element name; element name; element type
                            String responseQName = operationName + "Response;" + ename + ";" + "{" + qname.getNamespaceURI() + "}" + qname.getLocalPart();
                            xmlHttpTransaction.setResponseElementQName(responseQName);
                        }
                    }
                }
            } catch (Exception e) {
            }
        } else {
            try {
                qname = operation.getResponseBodyElementQName();
                if (qname != null) {
                    QName refName = new QName(qname.getNamespaceURI(), qname.getLocalPart());
                    xmlHttpTransaction.setXmlElementRefAffectation(new XmlQName(refName));
                }
            } catch (Exception e) {
            }
        }
        // Create request/response
        request = operation.addNewRequest("Test" + transactionName);
        requestXml = operation.createRequest(true);
        request.setRequestContent(requestXml);
        // responseXml = operation.createResponse(true);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document requestDoc = db.parse(new InputSource(new StringReader(requestXml)));
        // Document responseDoc = db.parse(new InputSource(new StringReader(responseXml)));
        Element enveloppe = requestDoc.getDocumentElement();
        String soapenvNamespace = enveloppe.getNamespaceURI();
        // Retrieve variables
        Element header = (Element) requestDoc.getDocumentElement().getElementsByTagNameNS(soapenvNamespace, "Header").item(0);
        Element body = (Element) requestDoc.getDocumentElement().getElementsByTagNameNS(soapenvNamespace, "Body").item(0);
        // System.out.println(XMLUtils.prettyPrintDOM(requestDoc));
        // Extract variables
        List<RequestableHttpVariable> variables = new ArrayList<RequestableHttpVariable>();
        extractSoapVariables(xmlSchema, variables, header, null, false, null);
        extractSoapVariables(xmlSchema, variables, body, null, false, null);
        // Serialize request/response into template xml files
        String projectName = project.getName();
        String connectorName = httpConnector.getName();
        String templateDir = Engine.projectDir(projectName) + "/soap-templates/" + connectorName;
        File dir = new File(templateDir);
        if (!dir.exists())
            dir.mkdirs();
        String requestTemplateName = "/soap-templates/" + connectorName + "/" + xmlHttpTransaction.getName() + ".xml";
        String requestTemplate = Engine.PROJECTS_PATH + "/" + projectName + requestTemplateName;
        xmlHttpTransaction.setRequestTemplate(requestTemplateName);
        saveTemplate(requestDoc, requestTemplate);
        // Adds variables
        for (RequestableHttpVariable variable : variables) {
            // System.out.println("adding "+ variable.getName());
            xmlHttpTransaction.add(variable);
        }
        xmlHttpTransaction.hasChanged = true;
    }
    return xmlHttpTransaction;
}
Also used : InputSource(org.xml.sax.InputSource) RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) XMLVector(com.twinsoft.convertigo.beans.common.XMLVector) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XmlHttpTransaction(com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction) XmlQName(com.twinsoft.convertigo.beans.common.XmlQName) QName(javax.xml.namespace.QName) MessagePart(com.eviware.soapui.model.iface.MessagePart) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) EngineException(com.twinsoft.convertigo.engine.EngineException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XmlQName(com.twinsoft.convertigo.beans.common.XmlQName) WsdlRequest(com.eviware.soapui.impl.wsdl.WsdlRequest) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) File(java.io.File)

Aggregations

WsdlRequest (com.eviware.soapui.impl.wsdl.WsdlRequest)1 MessagePart (com.eviware.soapui.model.iface.MessagePart)1 XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)1 XmlQName (com.twinsoft.convertigo.beans.common.XmlQName)1 XmlHttpTransaction (com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction)1 RequestableHttpVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpVariable)1 EngineException (com.twinsoft.convertigo.engine.EngineException)1 File (java.io.File)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1