Gateway Tutorial: VXML Application | Email Servlet

This is the start Servlet for the EmailToPhone application. When invoked the first time for a call, it dynamically generates a VXML file based on the email account. Voicent Gateway knows to call this Servlet because it is defined in the protectedappurl parameter of the EmailToPhone application conf file.

    EmailManager email_agent = EmailManager.getInstance(request);

    ...
    if (email_agent.startSession())
        ok = email_agent.download(false);
    ...

    PrintWriter pw = response.getWriter();

    pw.println("<?xml version=\"1.0\"?>");
    pw.println("<vxml version=\"1.0\">");
    pw.println("<form id=\"email\">");
    ...
    pw.println("</vxml>");
  }

By Voicent Gateway convention, this same method is invoked when the phone session ends. This invocation is identified by the parameter vxstate=end.

    String vxstate = request.getParameter("vxstate");
    if ("end".equals(vxstate)) {
        if (email_agent != null)  
            email_agent.endSession();
        return;
    }

Notice that in the path for certain audio file, there is a variable ${GW_RECORDED_VOICE}. This is a parameter corresponds to the selected male or female voice for pre-recorded audio files of Voicent Gateway. Voicent Gateway supports parameterized VXML files. These parameters are instantiated before using for the phone interaction. For more details, please refer to the Voicent Gateway reference manual online.

Source Code

package vx.apps.email;

import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.http.*;


public class EmailServlet extends HttpServlet
{
  public void service(HttpServletRequest request,
                      HttpServletResponse response)
    throws IOException
  {
    EmailManager email_agent = EmailManager.getInstance(request);

    // Voicent Gateway sends this request when session ends
    String vxstate = request.getParameter("vxstate");
    if ("end".equals(vxstate)) {
        if (email_agent != null)
            email_agent.endSession();
        return;
    }

    String failedReason = null;
    if (email_agent == null) {
        failedReason = "Invalid Session or email failed to start";
    }
    else {
        boolean ok = false;
        if (email_agent.startSession())
            ok = email_agent.download(false);
        if (! ok)
            failedReason = email_agent.getFailedReason();
    }

    PrintWriter pw = response.getWriter();

    pw.println("<?xml version=\"1.0\"?>");
    pw.println("<vxml version=\"1.0\">");

    pw.println("<form id=\"email\">");
    pw.println("<var name=\"fromapp\" expr=\"'email'\"/>");
    pw.println(" <block>");

    if (failedReason != null) {
        failedReason = "The cause of the problem is " + failedReason;
        pw.println("<audio src=\"audio/${GW_RECORDED_VOICE}/
                    no_serv_try_later.wav\"/>");
        pw.println(failedReason);
    }
    else {
        int total = email_agent.totalMessages();
        if (total == 0) {
            pw.println("<audio src=\"/audio/${GW_RECORDED_VOICE}/
                        u_have_no_msg.wav\"/>");
        }
        else {
            pw.println("<audio src=\"/audio/${GW_RECORDED_VOICE}/
                        u_have.wav\"/>");
            pw.println(Integer.toString(total));
            pw.println("<audio src=\"audio/${GW_RECORDED_VOICE}/
                         msgs.wav\"/>");
            pw.println("<audio src=\"audio/${GW_RECORDED_VOICE}/
                       first_msg.wav\"/>");
            pw.println("<var name=\"index\" expr=\"0\"/>");
            pw.println("<submit next=\"email_msg\" namelist=\"index\"/>");
        }
    }

    pw.println(" </block>");
    pw.println("</form>");

    pw.println("</vxml>");
  }
}

Previous Table of Contents Next