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();
	}
}
blog comments powered by Disqus