<%@ page import="java.io.*" %>
<%-- 
List of possible arguments to this JSP.
type - Indicates the launch method for launching the Messenger. Possible values are jnlp and plugin
server - IM server and port for example imserver.company.com:49909
codebase - The codebase from where the IM resources will be downloaded
uid - uid of the user
token - IS token string for the user
usessl - Indicates whether ssl shoudl be used or not. Possible values are true or false
autoinvite -  The auto invite list
domain - The virtual domain name
gateway_url - Indicates that the IM is being accessed using the gateway
locale - Locale to be used for launching the IM
help_codebase - Codebase for help pages
archive_control - Indicates whether the archive_control should be enabled. Possible values are true and false.
--%>

<%-- Change this block to assign a different default values --%>
<% 
String SERVER = null;
String LOCALE = null;
String CODEBASE = null;
//UID of the user required in case of auto logon
String UID = null;
//Domain Name required for virtual domains
String DOMAIN = null;
//UID of the user who will be invited to join the chat room just after the launch of messenger
String AUTO_INVITE = null;
//Indiactes the client should use SSL;
String USE_SSL = null;
//Indiactes the client should enable teh archive control;
String ARCHIVE_CONTROL = null;
//Indicates that the SRA gateway is being used for launching the Messenger
String GATEWAY_URL = null;
String HELP_CODEBASE = null;
String TYPE = "jnlp";
%>
<%-- End of Initialization block --%>

<%-- Read the arguments and assign them appropriately 
    Using the reflection class as the JSP will not be 
    compiled if Identity api's are directly used. --%>
<%
    String uid = (String)request.getAttribute("uid");
    if (uid == null) {
        uid = request.getParameter("uid");
        if (uid != null) {
            request.setAttribute("uid",uid);
        }
    }

    String t = (String)request.getAttribute("token");
    if (t == null) {
        t = request.getParameter("token");
        if (t != null) {
            request.setAttribute("token",t);
        }
    }

    java.util.Locale locale = request.getLocale();
    Object TOKEN = null;
    try {
	Object token;
	Object manager;
	Object connection;
	Object user;
	Class[] clArray = new Class[1];
	Object[] obArray = new Object[1];
        locale = (java.util.Locale) Class.forName("com.iplanet.am.util.Locale").getMethod("getDefaultLocale",null).invoke(null,null);
	manager = Class.forName("com.iplanet.sso.SSOTokenManager").getMethod("getInstance",null).invoke(null,null);
	clArray[0] = Class.forName("javax.servlet.http.HttpServletRequest");
	obArray[0] = (Object)request;
	token = manager.getClass().getMethod("createSSOToken",clArray).invoke(manager,obArray); 
	java.security.Principal principal;
	principal = (java.security.Principal)Class.forName("com.iplanet.sso.SSOToken").getMethod("getPrincipal",null).invoke(token,null); 
	clArray[0] = Class.forName("com.iplanet.sso.SSOToken");
	obArray[0] = token;
	connection = Class.forName("com.iplanet.am.sdk.AMStoreConnection").getConstructor(clArray).newInstance(obArray);
	clArray[0] = Class.forName("java.lang.String");
	obArray[0] = principal.getName();
	user =  Class.forName("com.iplanet.am.sdk.AMStoreConnection").getMethod("getUser",clArray).invoke(connection,obArray);
	clArray[0] = Class.forName("java.lang.String");
	obArray[0] = "iPlanetAMUserService";
	java.util.Map map =  (java.util.Map)Class.forName("com.iplanet.am.sdk.AMUser").getMethod("getServiceAttributes",clArray).invoke(user,obArray);
	java.util.Set u = (java.util.Set)map.get("uid");
	UID = principal.getName();
	if (u != null && !u.isEmpty()) {
	    UID = (String)u.iterator().next();
	}
        TOKEN = Class.forName("com.iplanet.sso.SSOToken").getMethod("getTokenID",null).invoke(token,null);
        request.setAttribute("uid",UID);
        request.setAttribute("token",TOKEN.toString());
    } catch(Exception e) {
        /*StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        out.print(sw.getBuffer().toString());*/
    }

    String codebase = (String)request.getAttribute("codebase");
    if (codebase == null) {
        codebase = request.getParameter("codebase");
        if (codebase == null) {
            if (CODEBASE == null) { 
                StringBuffer url = request.getRequestURL(); 
                CODEBASE = url.substring(0,url.lastIndexOf("/"));
            }
        } else CODEBASE = codebase;
        request.setAttribute("codebase",CODEBASE);
    } else CODEBASE = codebase;

    String l = (String)request.getAttribute("locale");
    if (l == null) {
        l = request.getParameter("locale");
        if (l == null) {
            if (LOCALE == null) {
                LOCALE = locale.toString();
            }
        } else LOCALE = l;
        request.setAttribute("locale",LOCALE);
    } else LOCALE = l;

    String server = (String)request.getAttribute("server");
    if (server == null) {
        server = request.getParameter("server");
        if (server == null) {
            if (SERVER == null) { 
                SERVER = request.getServerName();
            }
        } else SERVER = server;
        request.setAttribute("server",SERVER);
     } else SERVER = server;

    String domain = (String)request.getAttribute("domain");
    if (domain == null) {
        domain = request.getParameter("domain");
        if (domain != null) {
            request.setAttribute("domain",domain);
        } else if(DOMAIN != null) {
            request.setAttribute("domain",DOMAIN);
        }
    } else DOMAIN = domain;

    String invite = (String)request.getAttribute("autoinvite");
    if (invite == null) {
        invite = request.getParameter("autoinvite");
        if (invite != null) {
            request.setAttribute("auto_invite",invite);
        } else if (AUTO_INVITE != null) {
            request.setAttribute("auto_invite",AUTO_INVITE);
        }
    } else AUTO_INVITE = invite;

    String helpcodebase = (String)request.getAttribute("helpcodebase");
    if (helpcodebase == null) {
        helpcodebase = request.getParameter("help_codebase");
        if (helpcodebase == null) {
            if (HELP_CODEBASE == null) { 
                HELP_CODEBASE = CODEBASE + "/" + LOCALE;
            }
        } else HELP_CODEBASE = helpcodebase;
        request.setAttribute("help_codebase",HELP_CODEBASE);
    } else HELP_CODEBASE = helpcodebase;

    String ssl = (String)request.getAttribute("usessl");
    if (ssl == null) {
        ssl = request.getParameter("usessl");
        if (ssl != null) {
            request.setAttribute("use_ssl",ssl);
        } else if (USE_SSL != null) {
            request.setAttribute("use_ssl",USE_SSL);
        }
    } else USE_SSL = ssl;

    String archive = (String)request.getAttribute("archive_control");
    if (archive == null) {
        archive = request.getParameter("archive_control");
        if (archive != null) {
            request.setAttribute("archive_control",archive);
        } else if (ARCHIVE_CONTROL != null) {
            request.setAttribute("archive_control",ARCHIVE_CONTROL);
        }
    } else ARCHIVE_CONTROL = archive;

    String gateway = (String)request.getAttribute("gateway_url");
    if (gateway == null) {
        gateway = request.getParameter("gateway_url");
        if (gateway != null) {
            request.setAttribute("gateway_url",gateway);
        } else if (GATEWAY_URL != null) {
            request.setAttribute("gateway_url",GATEWAY_URL);
        }
    } GATEWAY_URL = gateway;

    String type = (String)request.getAttribute("type");
    if (type == null) type = request.getParameter("type");
    if (type != null) TYPE = type;
    if (TYPE.equalsIgnoreCase("jnlp")) { %>
        <jsp:forward page="jnlpLaunch.jsp" />
 <% } else if (TYPE.equalsIgnoreCase("plugin")) { %>
        <jsp:forward page="pluginLaunch.jsp" />
 <% } %>
