Cisco call studio : Java code for creating custom element which parses JSON

by sunil ravulapalli /5. November 2013 08:07 /cisco-voice-programming /Comments (0)
import com.audium.server.AudiumException;
import com.audium.server.voiceElement.ActionElementBase;
import com.audium.server.voiceElement.ElementInterface;
import com.audium.server.voiceElement.Setting;
import com.audium.server.voiceElement.ElementData;
import com.audium.server.voiceElement.ElementException;
import com.audium.server.xml.ActionElementConfig;
import com.audium.server.session.ActionElementData;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class GetFieldFromJSONElement extends ActionElementBase implements ElementInterface 
{
    public String getElementName()
    {
        return "GetFieldFromJSON";
    }

    public String getDisplayFolderName()
    {
        return "Company Elements";
    }

    public String getDescription() 
    {
        return "This element gets the value of the required field in a JSON string";
    }
    
	public Setting[] getSettings() throws ElementException 
	 {	
		 Setting[] settingArray = new Setting[4];
		 
		 settingArray[0] = new Setting("json", 
				 						"JSON", 
						   				"This is the input JSON",
						   				true,
						   				true,
						   				true,
						   				Setting.STRING);
		 
		 settingArray[1] = new Setting("field", 
					"Field", 
	   				"This is the input JSON",
	   				true,
	   				true,
	   				true,
	   				Setting.STRING);
		 
		 settingArray[2] = new Setting("resultType", 
				 						"Result Type", 
				   						"Choose where to store result \n" +
				   						"into Element or Session data",
				   						true,
				   						true,
				   						false,
				   						new String[]{"Element","Session"});
		settingArray[2].setDefaultValue("Session");		
		
		settingArray[3] = new Setting("resultName", 
										"Result Name", 
										"Name of variable to hold the result.",
										true,
										true,
										true,
										Setting.STRING);	
		settingArray[3].setDefaultValue("resultString");

		return settingArray;
	 }

    public ElementData[] getElementData() throws ElementException 
    {
        return null;
    }
    
	public void	doAction(String name, ActionElementData actionData) throws AudiumException
	{
		ActionElementConfig config = actionData.getActionElementConfig();			
		String json = config.getSettingValue("json",actionData);
		String field = config.getSettingValue("field",actionData);
		String resultType = config.getSettingValue("resultType",actionData);
		String resultName = config.getSettingValue("resultName",actionData);
		
		try {						
			String value = GetValueOfField(json, field);				
			
			if(resultType.equals("Element")){
				actionData.setElementData(resultName,value);
			} else {
				actionData.setSessionData(resultName,value);
			} 
			actionData.setElementData("status","success");
		} catch (Exception e) {
			
			if(resultType.equals("Element")){
				actionData.setElementData(resultName,e.toString());
			} else {
				actionData.setSessionData(resultName,e.toString());
			}
			
			actionData.setElementData("status","failure");
		}		
	}

	public String GetValueOfField(String json, String field) throws Exception
	{   
		JSONParser parser = new JSONParser();
		json = json.replaceAll("'", "\"");
		
		JSONObject jsonObject =  (JSONObject) parser.parse(json);
		
		Object val = jsonObject.get(field);
	    
		return val.toString();
	}
}

Cisco call studio : Java code for creating a custom element which can call a REST URL

by sunil ravulapalli /5. November 2013 08:03 /cisco-voice-programming /Comments (0)
import com.audium.server.AudiumException;
import com.audium.server.voiceElement.ActionElementBase;
import com.audium.server.voiceElement.ElementInterface;
import com.audium.server.voiceElement.Setting;
import com.audium.server.voiceElement.ElementData;
import com.audium.server.voiceElement.ElementException;
import com.audium.server.xml.ActionElementConfig;
import com.audium.server.session.ActionElementData;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.*;

public class GetUrlElement extends ActionElementBase implements ElementInterface 
{
    public String getElementName()
    {
        return "GetUrl";
    }

    public String getDisplayFolderName()
    {
        return "Company Elements";
    }

    public String getDescription() 
    {
        return "This element does a GET on a given URL and returns a result JSON string";
    }
    
	public Setting[] getSettings() throws ElementException 
	 {	
		 Setting[] settingArray = new Setting[3];
		 
		 settingArray[0] = new Setting("url", 
				 						"URL", 
						   				"This is the URL to call",
						   				true,
						   				true,
						   				true,
						   				Setting.STRING);
		 
		 settingArray[1] = new Setting("resultType", 
				 						"Result Type", 
				   						"Choose where to store result \n" +
				   						"into Element or Session data",
				   						true,
				   						true,
				   						false,
				   						new String[]{"Element","Session"});
		settingArray[1].setDefaultValue("Session");		
		
		settingArray[2] = new Setting("resultName", 
										"Result Name", 
										"Name of variable to hold the result.",
										true,
										true,
										true,
										Setting.STRING);	
		settingArray[2].setDefaultValue("resultString");

		return settingArray;
	 }

    public ElementData[] getElementData() throws ElementException 
    {
        return null;
    }
    
	public void	doAction(String name, ActionElementData actionData) throws AudiumException
	{
		ActionElementConfig config = actionData.getActionElementConfig();			
		String url = config.getSettingValue("url",actionData);
		String resultType = config.getSettingValue("resultType",actionData);
		String resultName = config.getSettingValue("resultName",actionData);
		try {						
			String responseBody = GetUrl(url);	
			
			if(resultType.equals("Element")){
				actionData.setElementData(resultName,responseBody);
			} else {
				actionData.setSessionData(resultName,responseBody);
			} 
			actionData.setElementData("status","success");
		} catch (Exception e) {
			
			if(resultType.equals("Element")){
				actionData.setElementData(resultName,e.toString());
			} else {
				actionData.setSessionData(resultName,e.toString());
			}
			
			actionData.setElementData("status","failure");
		}		
	}
	
	public String GetUrl(String inputUrl) throws Exception
	{
		URL url = new URL(inputUrl);
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		connection.setRequestMethod("GET");
		connection.connect();

		InputStream stream = connection.getInputStream();
		Reader reader = new InputStreamReader(stream);		

		StringBuilder sb = new StringBuilder();

		int data = reader.read();		

		while(data != -1){
		    char theChar = (char) data;
			if(theChar == '"')
			    sb.append("'");			
			else
				sb.append(theChar);
		    
			data = reader.read();		    		   
		}
		
		reader.close();

		return sb.toString();
	}
}

Cisco unified call studio 9.0 debugger always showing error

by sunil ravulapalli /19. October 2013 01:40 /cisco-voice-programming /Comments (0)

If your old call studio application used to work fine and even the simplest of applications don't seem to run in the Cisco unified call studio 9.0 debugger then you may need a mail.jar file.

SYMPTOMS

This the error you must be getting every time

Connecting...
Prompt: I'm sorry, there was a problem with this application. Please call back later. Thank you. (/CVP/audio/error.wav)
Disconnected.

The actual error which could give you a hit what may be happening is in this file startup_error_logYYYY-MM-DD.txt in this folder C:\Cisco\CallStudio\eclipse\plugins\com.audiumcorp.studio.debug.runtime_9.0.1-SNAPSHOT\AUDIUM_HOME\logs

SERVER ERROR: There was a problem loading classes from CVP_HOME\VXMLServer/common. The error was: There was a problem loading classes from CVP_HOME\VXMLServer/common. The root cause was: java.lang.NoClassDefFoundError: javax/mail/Authenticator

To fix this go to http://www.oracle.com/technetwork/java/index-138643.html download the latest Javamail.

Extract the contents and in the directory(javamail-1.4.x) you find a file called mail.jar.

Copy that mail.jar file to C:\Cisco\CallStudio\eclipse\plugins\com.audiumcorp.studio.debug.runtime_9.0.1-SNAPSHOT\CATALINA_HOME\webapps\CVP\WEB-INF\lib

Re-run your application. That should fix it!

Credit goes to http://developer.cisco.com/web/cvp/forums/-/message_boards/message/17876067