import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.transform.stream.StreamResult;

import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.lookup.*;
import com.sap.aii.mappingtool.tf7.rt.*;

import javax.xml.transform.dom.DOMSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import javax.xml.parsers.*;

import org.w3c.dom.*;

/*
* * Chiamata del communication channel chName relativo al business system
* receiver busSys. * Il parametro emailValue e’ relativo allo specfico
* webservice e rappresenta il parametro d’ingresso allo stesso.
*/ public String SOAPinvoke(String chName, String busSys, String emailValue, Container container) {
AbstractTrace trace = container.getTrace();
String internalID = “”;
try {
/*
* * Pass the Business System and Communication Channel as input to the *
* getChannel(). * BS_SOAPLOOKUP – Business System *
* CC_Webservice_SOAP_CURRENCY_CONVERTOR – Receiver SOAP Adapter
*/ Channel channel = LookupService.getChannel(busSys, chName);
SystemAccessor accessor = LookupService.getSystemAccessor(channel);
InputStream inputStream = null;
/*
* * Construct the SOAP Request Message using the InputParameters * emailValue
* is the Input Parameter.
*/ /* LA SEGUENTE ISTRUZIONE E’ RELATIVA ALLO SPECIFICO WEBSERVICE */ Document req = createRequestPayload(
emailValue);
/* ============================================================ */ String SOAPxml = returnXML(req);
inputStream = new ByteArrayInputStream(SOAPxml.getBytes());
XmlPayload payload = LookupService.getXmlPayload(inputStream);
/*
* * The SOAP call is made here and the response obtained is in the *
* SOAPOutPayload.
*/ Payload SOAPOutPayload = accessor.call(payload);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream inp = SOAPOutPayload.getContent();
Document doc = builder.parse(inp);
/* LA SEGUENTE ISTRUZIONE E’ RELATIVA ALLO SPECIFICO WEBSERVICE */ internalID = returnResponse(doc);
/* ============================================================ */ } catch (Exception e) {
trace.addWarning(“Error” + e);
}
return internalID;
}

/*
* * Crea il payload di request per il webservice da invocare. * Questa funzione
* dipende dallo specifico webservice.
*/ private static Document createRequestPayload(String emailValue) throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
doc.setXmlStandalone(true);
Element el = doc.createElementNS(“http://sap.com/xi/SAPGlobal20/Global”, “glob:CustomerByElementsQuery_sync”);
Node nroot = doc.appendChild(el);
Node ncsbe = nroot.appendChild(doc.createElement(“CustomerSelectionByElements”));
el = doc.createElementNS(“http://sap.com/xi/AP/CustomerExtension/BYD/A1WY2”,
“a1w:ExternalUnicCode_EA8AE8AUBVHCSXVYS0FJ1R3ON”);
Node neuc = ncsbe.appendChild(el);
Node nsbt = neuc.appendChild(doc.createElement(“SelectionByText”));
Node niec = nsbt.appendChild(doc.createElement(“InclusionExclusionCode”));
niec.setTextContent(“I”);
Node nibtc = nsbt.appendChild(doc.createElement(“IntervalBoundaryTypeCode”));
nibtc.setTextContent(“1”);
Node nlbn = nsbt.appendChild(doc.createElement(“LowerBoundaryName”));
nlbn.setTextContent(emailValue);
Node npc = nroot.appendChild(doc.createElement(“ProcessingConditions”));
Node nqhui = npc.appendChild(doc.createElement(“QueryHitsUnlimitedIndicator”));
nqhui.setTextContent(“false”);
return doc;
}

/* * Ritorna la String relativa al DOM dell’XML. */ private static String returnXML(Document doc)
throws javax.xml.transform.TransformerFactoryConfigurationError, javax.xml.transform.TransformerException {
javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory.newInstance()
.newTransformer();
StringWriter output = new StringWriter();
javax.xml.transform.Source input = new DOMSource(doc);
transformer.transform(input, new StreamResult(output));
return output.toString();
}

/*
* * Ritorna il payload relativo alla risposta del webservice invocato. * Questa
* funzione dipende dallo specifico webservice.
*/ static private String returnResponse(Document doc) throws XPathExpressionException {
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();
Node nroot = ((NodeList) xpath.compile(“//InternalID/text()”).evaluate(doc, XPathConstants.NODESET)).item(0);
return nroot.getNodeValue();
}