<%@ page language="java" import="com.cisco.ipphone.sdk.*, com.cisco.ipphone.sdk.push2phone.PushMessage, java.net.InetAddress, java.util.*" %>

<%
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Title:  		AutoDialer
// Author: 		kstearns
// Source File(s):  	autodialer.jsp
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
//  Description:
//
//  AutoDialer is a simple example of how a custom phone service can be used to dial numbers which require
//  pauses in the dialing sequence - long distance services, calling cards, voicemail access, etc.
//  When the user subscribes to the AutoDialer Phone Service, they will be required to enter a dial string.
//  When the user invokes the AutoDialer service from their phone, their specified dial string will be dialed.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
//  Requirements and Caveats:
//
// -  Client: XML services-enabled IP Phone
//
// - When defining the IP Phone service on CallManager, a required Phone Service Parameter with the
//   following properties must be added:
//
//        Parameter Name: dialstring
//        Parameter Display Name: The string of digits and pauses to be dialed
//        Parameter Description: The string of digits to be dialed. The digits (0-9*#) are valid.
//				 Commas (,) can also be used to insert 1-second pauses.
//				 Example: 918005551212,,,,1,6185551212,,12345678#
//	  Parameter is Required: True (checked)
//
//   IMPORTANT NOTE !!!! : If phone firmware version 8.3 or later is running on the phones, this approach to inserting pauses is completely 
//        unnecessary and not recommended because the Dial URI (and SendDigits URI) now support the use of commas for inserting one-second pauses
//
// - In order for AutoDialer to work, the PUSH userID and PUSH password hard-coded below
//   MUST have authority to PUSH to all phones used by the application.
//   For example, a new application user ID of 'sdkapps' could be created in the Global Directory.
//   All phones using the application are then selected as Associated Devices for user 'sdkapps'.
//
//   NOTE: For large-scale PUSH applications, associating hundreds or even thousands of devices to a single user
//         (for purposes of PUSH authentication) is *NOT ACCEPTABLE*.
//         In this case, the PUSH authentication *MUST* be offloaded from the CallManager server to an external
//         web server - see the PushAuthenticate ASP sample script for more information.
//
//   NOTE: The key to getting rapid, sequential PUSHes to execute properly is to use persistent HTTP 1.1 connections
//        for the HTTP POST (PUSH). This keeps the TCP connection open rather than tearing it down and rebuilding
//        for each PUSH. The Phone.push() methods have been specifically designed to take advantage of persistent connections
//        and minimize overhead on the phone.
//        Also note that we are not retrieving the result of the push (getResult = false) in this application because it
//        would have caused additional unwanted delay between PUSHes. Only retrieve the result if you must verify the success
//        of the push - in this case, we assume it completes successfully and are not concerned enough to check it.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

//
//  Edit the following Strings for your environment.
//  This is for sample purposes only - placing passwords and other
//  sensitive information in an open text file is obviously NOT recommended !!!
//

String phoneIP = request.getRemoteAddr();

String pushUserId = "sdkapps";
String pushPassword = "sdkapps";

String dialstring = request.getParameter("dialstring");

Phone.push("Key:Speaker", phoneIP, pushUserId, pushPassword, false);

// Wait a second for phone to go off-hook
try {
  Thread.sleep(1000);
} catch (Exception e) {};

// Now step thru each digit of the dial string and process it

for (int i=0; i < dialstring.length(); i++) {
  String digit = dialstring.substring(i, i+1);

  if (digit.equals(",")) {
    // If it's a comma, then pause for one second and continue
    try {
      Thread.sleep(1000);
    } catch (Exception e) {};
  }
  else if (digit.equals("#")) {
    Phone.push("Key:KeyPadPound", phoneIP, pushUserId, pushPassword, false);

  }
  else if (digit.equals("*")) {
    Phone.push("Key:KeyPadStar", phoneIP, pushUserId, pushPassword, false);
  }
  else {
    // We assume here that the digit must be a number, so we can append it to the KeyPad URI
    Phone.push("Key:KeyPad"+digit, phoneIP, pushUserId, pushPassword, false);
  }
  // Pause for 100ms before dialing next digit
  // This helps get the timing of the keypresses closer to the timing of digit playout on a PSTN gateway.
  try {
    Thread.sleep(100);
  } catch (Exception e) {};

}



//  THIS SAMPLE APPLICATION AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND BY CISCO, 
//  EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY
//  FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, SATISFACTORY QUALITY OR ARISING FROM A COURSE
//  OF DEALING, LAW, USAGE, OR TRADE PRACTICE.  CISCO TAKES NO RESPONSIBILITY REGARDING ITS USAGE IN AN
//  APPLICATION., THE APPLICATION IS PROVIDED AS AN EXAMPLE ONLY, THEREFORE CISCO DOES NOT MAKE ANY
//  REPRESENTATIONS REGARDING ITS RELIABILITY, SERVICEABILITY, OR FUNCTION.  IN NO EVENT DOES CISCO
//  WARRANT THAT THE SOFTWARE IS ERROR FREE OR THAT CUSTOMER WILL BE ABLE TO OPERATE THE SOFTWARE WITHOUT
//  PROBLEMS OR INTERRUPTIONS.  NOR DOES CISCO WARRANT THAT THE SOFTWARE OR ANY EQUIPMENT ON WHICH THE
//  SOFTWARE IS USED WILL BE FREE OF VULNERABILITY TO INTRUSION OR ATTACK.  THIS SAMPLE APPLICATION IS
//  NOT SUPPORTED BY CISCO IN ANY MANNER. CISCO DOES NOT ASSUME ANY LIABILITY ARISING FROM THE USE OF THE
//  APPLICATION. FURTHERMORE, IN NO EVENT SHALL CISCO OR ITS SUPPLIERS BE LIABLE FOR ANY INCIDENTAL OR
//  CONSEQUENTIAL DAMAGES, LOST PROFITS, OR LOST DATA, OR ANY OTHER INDIRECT DAMAGES EVEN IF CISCO OR ITS
//  SUPPLIERS HAVE BEEN INFORMED OF THE POSSIBILITY THEREOF.


%>


