Gateway Outbound Application Tutorial: The Sample List

The surveyHander.jsp is listed here. You can also download the whole sample file here.


<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.net.*" %>

<%@ page isThreadSafe="false" %>

<%!
  static class CallRecord
  {
    public CallRecord(String phoneno)
    {
      m_phoneno = phoneno;
      m_name = m_names[m_random.nextInt(3)];
      m_carmaker = m_makers[m_random.nextInt(3)];
      m_date = m_dates[m_random.nextInt(3)];
      m_callId = null;
      m_callStatus = null;
    }

    public String m_phoneno, m_name, m_carmaker, m_date, m_callId, m_callStatus;

    // randomly assign the value to a phone number
    static Random m_random = new Random(12345);
    static String m_names[] = { "John Smith", "Mary Jones", "Lisa Wells" };
    static String m_makers[] = { "Honda Accord", "Ford Escort", "BMW 545i" };
    static String m_dates[] = { "Monday Jan 20", "Tuesday Feb 12", "Friday June 30" };
  }

  public static ArrayList m_callRecords = new ArrayList();
%>

<%!
  // The routines below are simplifies implementations of the gateway
  // client library. Please note the format might change in future releases. It is
  // highly recommended to use the Gateway Client Library for commercial applications

  // send name value pairs to the gateway for the call
  void sendCallRequest(CallRecord rec)
  {
    // setting the required http post string
    String poststr = "info=";
    poststr += URLEncoder.encode("Survey Call " + rec.m_phoneno);
    poststr += "&phoneno=";
    poststr += rec.m_phoneno;
    poststr += "&firstocc=120";

    // append parameters to starturl string
    String starturl = "http://localhost:8155/ocall/myapp/start.jsp";
    starturl += "?name=" + rec.m_name;
    starturl += "&maker=" + rec.m_carmaker;
    starturl += "&date=" + rec.m_date;
    poststr += "&starturl=";
    poststr += URLEncoder.encode(starturl);

    // now the generic parameters for parameterized VXML
    // The name must match the one used in the VXML file

    poststr += "&customer_name=";
    poststr += URLEncoder.encode(rec.m_name);
    poststr += "&service_date=";
    poststr += URLEncoder.encode(rec.m_date);
    poststr += "&car_maker=";
    poststr += URLEncoder.encode(rec.m_carmaker);

    // Send Call Request
    String rcstr = postToGateway("/ocall/callreqHandler.jsp", poststr);
    rec.m_callId = getReqId(rcstr);
  }

  // get the call status based on callId
  void getCallStatus(CallRecord rec)
  {
    // setting the http post string
    String poststr = "reqid=";
    poststr += URLEncoder.encode(rec.m_callId);

    // Send Call Request
    String rcstr = postToGateway("/ocall/callstatusHandler.jsp", poststr);

    rec.m_callStatus = getCallStatus(rcstr);

    // now remove it on the gateway
    if (rec.m_callStatus != null)
      postToGateway("/ocall/callremoveHandler.jsp", poststr);
  }

  // send HTTP POST to Voicent Gateway
  private String postToGateway(String urlstr, String poststr)
  {
    try {
      URL url = new URL("http", "localhost", 8155, urlstr);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();

      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.setRequestMethod("POST");

      PrintWriter out = new PrintWriter(conn.getOutputStream());
      out.print(poststr);
      out.close();

      InputStream in = conn.getInputStream();

      StringBuffer rcstr = new StringBuffer();
      byte[] b = new byte[4096];
      int len;
      while ((len = in.read(b)) != -1)
        rcstr.append(new String(b, 0, len));
      return rcstr.toString();
    }
    catch (Exception e) {
      e.printStackTrace();
      return "";
    }
  }

  // process the call request return.
  private String getReqId(String rcstr)
  {
    int index1 = rcstr.indexOf("[ReqId=");
    if (index1 == -1)
      return "";
    index1 += 7;

    int index2 = rcstr.indexOf("]", index1);
    if (index2 == -1)
      return "";

    return rcstr.substring(index1, index2);
  }

  private String getCallStatus(String rcstr)
  {
    if (rcstr.indexOf("^made^") != -1)
      return "Call Made";

    if (rcstr.indexOf("^failed^") != -1)
      return "Call Failed";

    if (rcstr.indexOf("^retry^") != -1)
      return "Call Will Retry";

    return null;
  }

  private int getRateTotal(ServletContext app, String key)
  {
    Integer ii = (Integer) app.getAttribute(key);
    if (ii == null)
      return 0;
    return ii.intValue();
  }

%>

<%
  String act = request.getParameter("action");
  if (! "report".equals(act)) { // call start
    String phonelist = request.getParameter("phonelist");
    StringTokenizer tkz = new StringTokenizer(phonelist, ",");
    String phoneno;
    while (tkz.hasMoreTokens()) {
      phoneno = tkz.nextToken();
      CallRecord rec = new CallRecord(phoneno);
      sendCallRequest(rec);
      m_callRecords.add(rec);
    }
  }
%>

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Customer Satisfactory Survey: Report</title>
</head>
<body>

<font face="Lucida Console"><b>Customer Satisfactory Survey</b></font>
<p>
<form method="POST" action="surveyHandler.jsp">
<input type="hidden" name="action" value="report">
<p><input type="submit" value="Show the current survey report" name="B1"></p>
</form>

<%
  // check call status from the list
  int callsToBeMade = 0;
  int callsFailed = 0;
  for (int i = 0; i < m_callRecords.size(); i++) {
    CallRecord rec = (CallRecord) m_callRecords.get(i);
    if (rec.m_callStatus == null) {
      getCallStatus(rec);
      if (rec.m_callStatus == null) {
        callsToBeMade++;
        continue;
      }
    }
    if (rec.m_callStatus.equals("Call Failed"))
      callsFailed++;
  }

  // get the rest from application vars
  int answeringTotal = getRateTotal(application, "anstotal");
  int rate1 = getRateTotal(application, "rate1");
  int rate2 = getRateTotal(application, "rate2");
  int rate3 = getRateTotal(application, "rate3");
  int rate4 = getRateTotal(application, "rate4");
  int rate5 = getRateTotal(application, "rate5");
  int norate = m_callRecords.size()
    - (callsToBeMade + callsFailed + answeringTotal)
    - (rate1 + rate2 + rate3 + rate4 + rate5);
%>

<hr>
<table border="0" cellpadding="10" cellspacing="10" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
<tr>
<td width="27%" align="right"><b>
<font color="#000080" face="Lucida Console" size="2">Total calls:</font></b></td>
<td width="73%"><font face="Bookman Old Style" color="#800000"><%=m_callRecords.size()%></font></td>
</tr>
<tr>
<td width="27%" align="right"><b>
<font color="#000080" face="Lucida Console" size="2">Calls to be made:</font></b></td>
<td width="73%"><font face="Bookman Old Style" color="#800000"><%=callsToBeMade%></font></td>
</tr>
<tr>
<td width="27%" align="right"><b>
<font color="#000080" face="Lucida Console" size="2">Calls reached
answering machine:</font></b></td>
<td width="73%"><font face="Bookman Old Style" color="#800000"><%=answeringTotal%></font></td>
</tr>
<tr>
<td width="27%" align="right"><b>
<font color="#000080" face="Lucida Console" size="2">Calls failed:</font></b></td>
<td width="73%"><font face="Bookman Old Style" color="#800000"><%=callsFailed%></font></td>
</tr>
<tr>
<td width="27%" align="right"><b>
<font color="#000080" face="Lucida Console" size="2">Customers rated us 1:</font></b></td>
<td width="73%"><font face="Bookman Old Style" color="#800000"><%=rate1%></font></td>
</tr>
<tr>
<td width="27%" align="right"><b>
<font color="#000080" face="Lucida Console" size="2">Customers rated us 2:</font></b></td>
<td width="73%"><font face="Bookman Old Style" color="#800000"><%=rate2%></font></td>
</tr>
<tr>
<td width="27%" align="right"><b>
<font color="#000080" face="Lucida Console" size="2">Customers rated us 3:</font></b></td>
<td width="73%"><font face="Bookman Old Style" color="#800000"><%=rate3%></font></td>
</tr>
<tr>
<td width="27%" align="right"><b>
<font color="#000080" face="Lucida Console" size="2">Customers rated us 4:</font></b></td>
<td width="73%"><font face="Bookman Old Style" color="#800000"><%=rate4%></font></td>
</tr>
<tr>
<td width="27%" align="right"><b>
<font color="#000080" face="Lucida Console" size="2">Customers rated us 5:</font></b></td>
<td width="73%"><font face="Bookman Old Style" color="#800000"><%=rate5%></font></td>
</tr>
<tr>
<td width="27%" align="right"><b>
<font color="#000080" face="Lucida Console" size="2">Customers did not rate
us:</font></b></td>
<td width="73%"><font face="Bookman Old Style" color="#800000"><%=norate%></font></td>
</tr>
</table>

</body>
</html>