<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="LoginError.jsp"
%><%@page import="java.io.File"
%><%@page import="java.io.FileReader"
%><%@page import="java.io.BufferedReader"
%><%@page import="java.util.List"
%><%@page import="java.util.Properties"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMBase"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMAdmin"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMSessionImpl"
%><%@page import="com.fujitsu.iflow.model.util.AEServers"
%><%@page import="com.fujitsu.iflow.model.util.LoginAttemptRecord"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="com.fujitsu.iflow.jsp.servlet.BPMHttpSessionBindingListener"
%><%request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
%><%

    String fileName = application.getRealPath("/").trim();
    if (fileName.endsWith(File.separator))
        fileName = fileName + "Taskmanager.conf";
    else
        fileName = fileName + File.separator + "Taskmanager.conf";
    Properties prop = new Properties();
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String line = null;
    for(line = in.readLine(); line != null; line = in.readLine())
    {
        int pos = line.indexOf("=");
        if(pos == -1 || line.charAt(1) == '#') {
            continue;
        }
        String name = line.substring(0,pos);
        String value = line.substring(pos+1);
        prop.setProperty(name.trim(),value.trim());
    }
    if (prop.isEmpty()) {
        throw new Exception("A properties file is expected for this application at the following location: "+fileName
         +"   A system administrator will need to place a configuration file with the right settings there.");
    }
    String hostName = prop.getProperty("HostName");
    if (hostName == null || hostName.trim().length()==0) {
        throw new Exception("A properties file at location: "+fileName
         +"  must have a setting for the name of the host ('HostName') that you are to log into.");
    }
    String serverName = prop.getProperty("ServerName");

    String plugin_ver = prop.getProperty("Plugin_version");
    String applet_codebase = prop.getProperty("Applet_codebase");
    String applet_classid = prop.getProperty("Applet_classid");
    String processdesigner_enabled = prop.getProperty("ProcessDesignerEnabled");

    String transportType = prop.getProperty("TWFTransportType");
    if (transportType == null) {
        throw new Exception("A properties file at location: "+fileName
         +"  must have a setting for the transport type ('transportType') that you will use to connect to server.");
    }

    String goPage = request.getParameter("go");
    if (goPage == null) {
        throw new Exception("LoginAction page needs a parameter 'go' to specify the next page to go to.");
    }


    String userName = request.getParameter("userName");
    String password = request.getParameter("password");

    String requestIPAddr = request.getRemoteAddr();
    LoginAttemptRecord.checkLoginThreshold(requestIPAddr);

    // update the failure count, just in case there is ANY failure
    LoginAttemptRecord.incrementCount(requestIPAddr);

    serverName = hostName + "Flow";

    if ( userName == null || userName.trim().length()==0 )
    {
         String message = "You must provide a User Name and Password !";
         request.setAttribute("message", message);
         request.setAttribute("nextPage", "Login.jsp");
         request.setAttribute("nextLink", "Login");
%>
        <jsp:forward page="Error.jsp"/>
<%
    }

    userName = userName.trim();
    if (password==null) {
        password = "";
    }
    else {
        password = password.trim();
    }

    BPMSession bps = BPMBase.getSession(userName, password, prop);

    // WFSession included for backward compatibility
    WFSession wfSession = ((BPMSessionImpl)bps)._wfs;

    // successful login means that we need to assure the count is cleared
    LoginAttemptRecord.clearCount(requestIPAddr);

    //Check if an admin user
    String isAdminUser = "false";
    try {
       isAdminUser = String.valueOf(bps.isAdmin());
    }
    catch(Exception e){
        isAdminUser = "false";
    }
    if(!transportType.equalsIgnoreCase("LE")) {
        BPMHttpSessionBindingListener bindingListener = new BPMHttpSessionBindingListener(bps);
        session.setAttribute("BPMSessionListener", bindingListener);
    }
    session.setAttribute("BPMSession", bps);
    session.setAttribute("ModelsessionID", wfSession);
    session.setAttribute("userName", userName);
    session.setAttribute("password", password);
    session.setAttribute("HostName", hostName);
    session.setAttribute("TransportType", transportType);
    session.setAttribute("isAdminUser",isAdminUser);
    session.setAttribute("PluginVersion",plugin_ver);
    session.setAttribute("AppletCodebase",applet_codebase);
    session.setAttribute("AppletClassid",applet_classid);
    session.setAttribute("ProcessDesignerEnabled",processdesigner_enabled);
    response.sendRedirect(goPage);
%>
