<%@ page language="java" import="java.io.*, java.util.*, com.cisco.ipphone.sdk.*, javax.naming.*, javax.naming.directory.*" %>
<%
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Title:  		Extension Mobility Controller
// Author: 		kstearns
// Source File(s):  	extmobility.jsp
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
//  Description:
//
//  This sample illustrates the basics of accessing the Extension Mobility API via the EMProvider
//  class.
//  Call this page from a standard web browser (not from an IP Phone) and you can query and control
//  Extension Mobility login status for users and phones.
///////////////////////////////////////////////////////////////////////////////////////////////////////
//  Requirements and Caveats:
//
// -  Client: HTML Browser (PC-based web browser)
//
//  - In order to function properly, the user and phones accessed from this script must have
//    Extension Mobility configured and enabled on CallManager.
///////////////////////////////////////////////////////////////////////////////////////////////////////

//
//  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 extMobUserId = "sdkapps";
  String extMobPassword = "sdkapps";
  String callManager = "10.0.0.1";
  String callManagerUserId = "administrator";
  String callManagerPassword = "cisco";

  String defaultDevice = "SEP003094C3AE09";
  String defaultUserID = "kstearns";

  EMProvider emp = new EMProvider(callManager, callManagerUserId, callManagerPassword, extMobUserId, extMobPassword);

  ///////////////////////////////////////////////////////////
  // If the 'action' is null then list the directory
  ///////////////////////////////////////////////////////////

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

  if (action == null)
    action = "main";

  if (action.equals("main"))
  {

     %>
     <html>
     <head><h2>Extension Mobility Controller</h2></head>
     <body><br><br>

      <form method="POST" action=extmobility.jsp?action=queryuser>
      <textarea rows="1" name="user"><%=defaultUserID%></textarea>
      <p><input type="submit" value="Query User" name="B1">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Reset" name="B2"></p>
      </form>
      <br><br><br>

      <form method="POST" action=extmobility.jsp?action=querydevice>
      <textarea rows="1" name="deviceName"><%=defaultDevice%></textarea>
      <p><input type="submit" value="Query Device" name="B1">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Reset" name="B2"></p>
      </form>
      <br><br><br>

      <form method="POST" action=extmobility.jsp?action=login>
      <textarea rows="1" name="user"><%=defaultUserID%></textarea>
      <textarea rows="1" name="deviceName"><%=defaultDevice%></textarea>
      <p><input type="submit" value="Login" name="B1">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Reset" name="B2"></p>
      </form>
      <br><br><br>

      <form method="POST" action=extmobility.jsp?action=logout>
      <textarea rows="1" name="deviceName"><%=defaultDevice%></textarea>
      <p><input type="submit" value="Logout" name="B1">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Reset" name="B2"></p>
      </form>

      </body>
      </html>


     <%
  }

  if (action.equals("queryuser"))
  {
    User user = User.getUser(request.getParameter("user"));
    Phone phone = user.getCurrentExtMobPhone(emp);
    if (phone != null) {
      out.println(phone.getDeviceName());
    }
    else {
      out.println("User not currently logged in");
    }
  }

  if (action.equals("querydevice"))
  {
    Phone phone = Phone.getPhone(request.getParameter("deviceName"));
    User user = phone.getCurrentExtMobUser(emp);
    if (user != null) {
      out.println(user.getUserId());
    }
    else {
      out.println("No user currently logged in");
    }
  }

  if (action.equals("login"))
  {
    User user = User.getUser(request.getParameter("user"));
    Phone phone = Phone.getPhone(request.getParameter("deviceName"));
    if (user.login(emp, phone)) {
      out.println("Successful");
    }
    else {
      out.println("Failed");
    }
  }

  if (action.equals("logout"))
  {
    Phone phone = Phone.getPhone(request.getParameter("deviceName"));
    if (phone.logout(emp)) {
      out.println("Successful");
    }
    else {
      out.println("Failed");
    }
  }



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

%>