use of com.eviware.soapui.impl.wsdl.WsdlRequest 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;
}
Aggregations