<%@ page language="java" import="com.cisco.ipphone.sdk.*, com.cisco.ipphone.sdk.CallerInfo.*, java.awt.*" %>

<%

///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Title:  		CallerInfo
// Author: 		kstearns
// Source File(s): 	callerinfo.jsp
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Description:
//
// CallerInfo is an example of an integrated CTI (JTAPI) and XML services application.
// CallerInfo works with PhotoDirectory to provide call party information for active calls
// on IP phones.
// CallerInfo works both in "pull" and "push" mode.
// The "pull" mode is when the CallerInfo service is invoked from the Services menu by the user
// to retrieve PhotoDirectory info about the user at the other end of the current active call.
// The "push" mode works by having the CallerInfoServer application push the PhotoDirectory URL
// to the phone as soon as an inbound call is answered.
// This sample app also illustrates the use of the new JTAPI CiscoTerminal.sendData() which
// allows phone PUSHes to be performed directly on JTAPI Terminal objects without the need
// to find the IP address of the phone or provide authentication information.
//
// PERFORMANCE NOTE:  As with any CTI-based application, scalability can be a limiting factor.
// Consult Cisco's CTI design guides to ensure that the use of this application does not cause
// performance or scalability problems.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Requirements and Caveats:
//
// - Cisco JTAPI Plugin must be installed on your web application server in order for this app to work.
//   The JTAPI plugin can be downloaded from any 3.3(3) CallManager from the Administrative pages under
//   Application -> Install Plugins. 
//   The c:\winnt\system32\java\lib\jtapi.jar installed by the Cisco JTAPI Client plugin *must* be in the
//   classpath of your JSP server. 
//   If you are running Tomcat, the easiest way to do this is to copy the jtapi.jar file
//   into the %tomcat%\shared\lib directory - where %tomcat% is the Tomcat installation directory.
//
// - In order for this app to function properly, the Phone Service URL *must* pass the devicename of
//   the requesting phone to this script in a querystring parameter called 'name'.
//   This is easily accomplished by adding the special #DEVICENAME# querystring parameter onto the
//   end of the URL when defining the Phone Service in CallManager administration.
//   The #DEVICENAME# tag is a special script tag which tells the getservicesmenu.asp script to
//   insert the devicename of the phone requesting the Services menu.
//   For example, the Phone Service URL should look like this:
//      http://yourserver:8080/callerinfo/callerinfo.jsp?name=#DEVICENAME#
//
// - All phones wanting to use the CallerInfo application (in either pull or push mode), *must* be
//   associated to the JTAPI application userId specified below.
//
// - CallerInfo relies on the PhotoDirectory application being operational in order to work properly.
//   Get PhotoDirectory working first, then edit the URLs below to point to that application.
//
// - In order for CallerInfo to work in "push" mode it must first be "pulled" at least once after
//   the JSP server has been restarted. The first time the CallerInfo page is accessed, it will
//   create the CallerInfoServer object which registers with the CallManager CTI and sets up the
//   Terminal (phone) listeners. Once the Terminal listeners are registered, the CallerInfoServer is
//   then able to monitor phones for incoming calls and push the information to the phones.
//   For troubleshooting purposes, watch the Java console window of your
//   JSP server the first time CallerInfo is called - you will see any success or error messages
//   when the CallerInfoServer is first created and started.
//
// - Nearly all of the real work for this app is done in the CallerInfoServer Java class.
//   Examine the source code and comments in that class to learn more about how it works.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//
//  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 callManager = "10.0.0.1";
String jtapiUserId = "sdkapps";
String jtapiPassword = "sdkapps";

String photoDirUrl = "http://10.0.0.1:8080/photodirectory/photodirectory.jsp?action=details&ext=";
String jtapiPushUrl = "http://10.0.0.1:8080/photodirectory/photodirectory.jsp?action=details&amp;ext=";

/////////////////////////////////////////////////////////////////////////////////////////////////////

// Create the CallerInfoServer, if it doesn't already exist
CallerInfoServer theInfoServer = CallerInfoServer.getCallerInfoServer(callManager, jtapiUserId, jtapiPassword, jtapiPushUrl);

String phoneName = request.getParameter("name");
String[] numbers = theInfoServer.getActiveCallPartyNumbers(phoneName);

String otherEnd = "";
if (numbers != null) {
  // Get the number involved in the call which does NOT belong to this phone
  otherEnd = (numbers[2].equals(numbers[0])) ? numbers[1] : numbers[0];
  // And redirect the phone to the PhotoDirectory application and pass number to look up
  response.sendRedirect(photoDirUrl+otherEnd);
}
else {
  response.setContentType("text/xml");
  out.println("<CiscoIPPhoneText><Title>CallerInfo</Title><Text>\r\nNo active calls</Text></CiscoIPPhoneText>");
}


//  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.

%>


