Call Studio Java Examples
Create custom Java Code for CVP
Add the below two “external Jar” to the Library
- C:\Cisco\CallStudio\eclipse\plugins\com.audiumcorp.studio.library.framework\lib\framework.jar
- C:\Cisco\CallStudio\eclipse\plugins\com.audiumcorp.studio.library.framework\lib\servlet-api.jar
Convert a Number to a String type with an intger value
- Set a session variable myInt to be your number, such as the string “12.0” which gets returned from Set value
- The below Call Studio Action Java Class will retrieve this value and set the session variable “myString” to “12”
import com.audium.server.AudiumException; import com.audium.server.voiceElement.ActionElementBase; import com.audium.server.session.ActionElementData; /** * This class is called when a standard action has been configured to use a * Java class. Since this is a standard action element, it applies to a * specific application and does not have a configuration. As a result, the * only method needed in this class is the doAction method. */ public class ConvertNumToString extends ActionElementBase { /** * All action classes must implement this method. The data is retrieved * through the ActionElementData class. Unlike decisions, there is no need * to return anything as all actions have a single exit state. */ public void doAction(String name, ActionElementData data) throws AudiumException { float tempfloat = Float.parseFloat(data.getSessionData("myInt").toString()); int tempint = Math.round(tempfloat); String myString = Integer.toString(tempint); data.addToLog("myString", myString); data.setSessionData("myString", myString); } }
Write output to a text file
import com.audium.server.AudiumException; import com.audium.server.voiceElement.DecisionElementBase; import com.audium.server.session.DecisionElementData; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class DecideWriteCallback extends DecisionElementBase { public String doDecision(String paramString, DecisionElementData data) throws AudiumException { String filePath = data.getSessionData("textConfigPath").toString(); String filename = data.getSessionData("filename").toString(); String callbackdata = data.getSessionData("callbackdata").toString(); String fileWriteResponse = doCallbackWrite(filePath + filename ,callbackdata, data); if (fileWriteResponse.equalsIgnoreCase("FileError")){ return "Fail"; } return "Success"; } public String doCallbackWrite(String paramString1, String paramString2, DecisionElementData data) throws AudiumException { try { File localFile = new File(paramString1); BufferedWriter localBufferedWriter = new BufferedWriter( new FileWriter(localFile)); localBufferedWriter.write(paramString2); localBufferedWriter.close(); } catch (IOException localIOException) { data.addToLog("Callback-FILE-WRITE-ERROR", localIOException.toString()); return "FileError"; } return "ok"; } }
Simulate a delay
// Simple Action Element to Simulate a Delay // Gerard O'Rourke // date: 25/10/2017 import com.audium.server.AudiumException; import com.audium.server.voiceElement.ActionElementBase; import com.audium.server.session.ActionElementData; /** * This class is called when a standard action has been configured to use a * Java class. Since this is a standard action element, it applies to a * specific application and does not have a configuration. As a result, the * only method needed in this class is the doAction method. */ public class ActionDelay extends ActionElementBase { /** * All action classes must implement this method. The data is retrieved * through the ActionElementData class. Unlike decisions, there is no need * to return anything as all actions have a single exit state. */ public void doAction(String name, ActionElementData data) throws AudiumException { int timer = Integer.parseInt((String) data.getSessionData("delay-in-ms")); try { Thread.sleep(timer); } catch (InterruptedException e) { data.addToLog("ERROR InterruptedException", e.toString()); } } }
Upload file using AD credentials
/* * Author: Gerard O'Rourke * date: 28 September 2010 * * DecideADFileupload.java * this Java class uploads a file from a local file share to a nework file share. * * the following session variables are required to be set in the CVP Studio Application: * smbSource * smbDestination * domain * username * password * */ import com.audium.server.AudiumException; import com.audium.server.session.DecisionElementData; import com.audium.server.voiceElement.DecisionElementBase; import jcifs.smb.*; public class DecideADFileupload extends DecisionElementBase { public String doDecision(String name, DecisionElementData data) throws AudiumException { String varSMBSource = data.getSessionData("smbSource").toString(); String varSMBDestination = data.getSessionData("smbDestination").toString(); String varDomain = data.getSessionData("domain").toString(); String varUser = data.getSessionData("username").toString(); String varPassword = data.getSessionData("password").toString(); jcifs.Config.setProperty( "jcifs.smb.client.domain", varDomain); jcifs.Config.setProperty( "jcifs.smb.client.username", varUser); jcifs.Config.setProperty( "jcifs.smb.client.password", varPassword); try { SmbFile remote = new SmbFile( varSMBSource ); if( remote.exists() ) { SmbFile local = new SmbFile( varSMBDestination ); remote.copyTo( local ); return "Success"; } else{ data.logWarning("FileUploadError: " + varSMBSource + " -> " + varSMBDestination); return "Fail"; } } catch( Exception e ) { data.logWarning("FileUploadError: " + varSMBSource + " -> " + varSMBDestination); data.logWarning("FileUploadError: " + e.toString()); return "Fail"; } } }
XML Element Extract Value
/** * Author: Gerry O'Rourke * Date: 01 July 2021 * * This Java Class extract a value of a specific XML element. * In my use case I extract from the below SOAP XML response from LCM the value of the <strContactDetail> Element. * <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap:Header/> <soap:Body> <GetContactDetailResponse xmlns="http://tempuri.org/"> <GetContactDetailResult>1</GetContactDetailResult> <strContactDetail><DATA><BussFlds><FieldName>FirstName</FieldName><Value><![CDATA[Bob]]></Value><DataType>String</DataType><Format></Format></BussFlds><BussFlds><FieldName>LastName</FieldName><Value><![CDATA[SCA]]></Value><DataType>String</DataType><Format></Format></BussFlds><BussFlds><FieldName>RegNum</FieldName><Value><![CDATA[]]></Value><DataType>String</DataType><Format></Format></BussFlds><BussFlds><FieldName>SpeakerID</FieldName><Value><![CDATA[1872730M5BS]]></Value><DataType>String</DataType><Format></Format></BussFlds><BussFlds><FieldName>Merchant</FieldName><Value><![CDATA[Amazon]]></Value><DataType>String</DataType><Format></Format></BussFlds><BussFlds><FieldName>Amt</FieldName><Value><![CDATA[150.00]]></Value><DataType>Number</DataType><Format></Format></BussFlds><BussFlds><FieldName>CardNum</FieldName><Value><![CDATA[1234]]></Value><DataType>Number</DataType><Format></Format></BussFlds></DATA></strContactDetail> </GetContactDetailResponse> </soap:Body> </soap:Envelope> * Required Session Data Variables:, response_xml, elementName_xml * Set the Session Data Variable response_xml to the XML String you want to extract data from. * Set the Session Data Variable elementName_xml to the element name you want to extract data from, e.g. "strContactDetail" * * Session Data Variable Set by this Java Class: xmlElement * {Data.Session.xmlElement} will be set to the value of the element * * * References * https://howtodoinjava.com/java/xml/parse-string-to-xml-dom/ * https://stackoverflow.com/questions/4076910/how-to-retrieve-element-value-of-xml-using-java/24497342 * */ import com.audium.server.AudiumException; import com.audium.server.voiceElement.ActionElementBase; import com.audium.server.session.ActionElementData; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; /** * This class is called when a standard action has been configured to use a * Java class. Since this is a standard action element, it applies to a * specific application and does not have a configuration. As a result, the * only method needed in this class is the doAction method. */ public class GetXmlElement extends ActionElementBase { /** * All action classes must implement this method. The data is retrieved * through the ActionElementData class. Unlike decisions, there is no need * to return anything as all actions have a single exit state. */ public void doAction(String name, ActionElementData data) throws AudiumException { String xmlStr = data.getSessionData("response_xml").toString(); data.addToLog("xmlStr", xmlStr); String xmlElementName = data.getSessionData("elementName_xml").toString(); data.addToLog("xmlElementName", xmlElementName); Document doc = convertStringToXMLDocument(xmlStr); Element rootElement = doc.getDocumentElement(); String xmlElement = getString(xmlElementName, rootElement); data.setSessionData("xmlElement", xmlElement); data.addToLog("xmlElement", xmlElement); } private static Document convertStringToXMLDocument(String xmlString) { //Parser that produces DOM object trees from XML content DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //API to obtain DOM Document instance DocumentBuilder builder = null; try { //Create DocumentBuilder with default configuration builder = factory.newDocumentBuilder(); //Parse the content to Document object Document doc = builder.parse(new InputSource(new StringReader(xmlString))); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } protected String getString(String tagName, Element element) { NodeList list = element.getElementsByTagName(tagName); if (list != null && list.getLength() > 0) { NodeList subList = list.item(0).getChildNodes(); if (subList != null && subList.getLength() > 0) { return subList.item(0).getNodeValue(); } } return null; } }
Parse / Loop the BussFld tags of an XML String (retrieved from LCM)
/** * Author: Gerry O'Rourke * Date: 01 July 2021 * * This Java Class parses an extract of XML returned from Acquoen's LCM GetContactDetail Web Service Call. * Below is an example of what is expected to be set in the Session Variable 'strContactDetail' * * This session variable is retrieved and the 'BussFlds' tags are parsed. The Value of Each FieldName is created a Session Variable, who value is set to the 'Value' tags value. * e.g. {Data.Session.FirstName} == "Bob" etc. <DATA> <BussFlds><FieldName>FirstName</FieldName><Value><![CDATA[Bob]]></Value><DataType>String</DataType><Format></Format></BussFlds> <BussFlds><FieldName>LastName</FieldName><Value><![CDATA[SCA]]></Value><DataType>String</DataType><Format></Format></BussFlds> <BussFlds><FieldName>RegNum</FieldName><Value><![CDATA[]]></Value><DataType>String</DataType><Format></Format></BussFlds> <BussFlds><FieldName>SpeakerID</FieldName><Value><![CDATA[1872730M5BS]]></Value><DataType>String</DataType><Format></Format></BussFlds> <BussFlds><FieldName>Merchant</FieldName><Value><![CDATA[Amazon]]></Value><DataType>String</DataType><Format></Format></BussFlds> <BussFlds><FieldName>Amt</FieldName><Value><![CDATA[150.00]]></Value><DataType>Number</DataType><Format></Format></BussFlds> <BussFlds><FieldName>CardNum</FieldName><Value><![CDATA[1234]]></Value><DataType>Number</DataType><Format></Format></BussFlds> </DATA> * * References: * https://howtodoinjava.com/java/xml/read-xml-dom-parser-example/ * Reference: https://howtodoinjava.com/java/xml/parse-string-to-xml-dom/ */ /** * Required Session Variable: strContactDetail * * Session Variables Set: * Each FieldName will retrieved will have a Session Variable set to its FieldName and its value set to its Value. */ import com.audium.server.AudiumException; import com.audium.server.voiceElement.ActionElementBase; import com.audium.server.session.ActionElementData; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; /** * This class is called when a standard action has been configured to use a * Java class. Since this is a standard action element, it applies to a * specific application and does not have a configuration. As a result, the * only method needed in this class is the doAction method. */ public class LCMSetBussFldsVariables extends ActionElementBase { /** * All action classes must implement this method. The data is retrieved * through the ActionElementData class. Unlike decisions, there is no need * to return anything as all actions have a single exit state. */ public void doAction(String name, ActionElementData data) throws AudiumException { //Set the string variable 'strContactDetail ' to the CVP Session Variable 'strContactDetail' String strContactDetail = data.getSessionData("strContactDetail").toString(); //Convert XML string to Document Document document = convertStringToXMLDocument(strContactDetail); //Normalize the XML Structure; It's just too important !! document.getDocumentElement().normalize(); //Here comes the root node Element root = document.getDocumentElement(); data.addToLog("root.getNodeName()", root.getNodeName()); //Get all BussFlds NodeList nList = document.getElementsByTagName("BussFlds"); for (int temp = 0; temp < nList.getLength(); temp++) { Node node = nList.item(temp); if (node.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) node; //Retrieve and set the String variable 'FieldName' to the XML Tag 'FieldName' String FieldName = eElement.getElementsByTagName("FieldName").item(0).getTextContent().toString(); //Retrieve and set the String variable 'Value' to the XML Tag 'Value' String Value = eElement.getElementsByTagName("Value").item(0).getTextContent().toString(); // Set the Session Data Variable of the current 'FieldName' tag value to the current 'Value' tag value. // e.g. {Data.Session.FirstName} = "Bob" data.setSessionData(FieldName, Value); // Log the Session Data Variable Name and value data.addToLog("Setting Session.Data."+ FieldName, Value); } } } private static Document convertStringToXMLDocument(String xmlString) { //Parser that produces DOM object trees from XML content DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //API to obtain DOM Document instance DocumentBuilder builder = null; try { //Create DocumentBuilder with default configuration builder = factory.newDocumentBuilder(); //Parse the content to Document object Document doc = builder.parse(new InputSource(new StringReader(xmlString))); return doc; } catch (Exception e) { e.printStackTrace(); } return null; } }
Cisco Stock Elements
The out of the box elements for CVP in CVP Call Studio are located in the JAR file
- C:\Cisco\CallStudio\eclipse\plugins\com.audiumcorp.studio.elements.core\lib\elements.jar