public App findApplication(HttpServletRequest req, HttpServletResponse res)
    {
        ServletApplication application = null;
        String strDatatype = BaseHttpTask.getParam(req, DBParams.DATATYPE, null); // Raw data (such as in image from the DB)
        if (strDatatype != null)    // NOTE: I hope there isn't an issue with getting a param before setting the data type, but I have no choice.
        { // Special case - if asking for a datatype, don't need a session, use a default application!
            return this.getNonUserApplication();
        }

    // First, see if you are in a current session, if not create one
        HttpSession session = req.getSession();
        String strUserNameOrID = BaseHttpTask.cleanParam(req.getParameterValues(DBParams.USER_NAME), false, null);
        if (strUserNameOrID == null)
            strUserNameOrID = BaseHttpTask.cleanParam(req.getParameterValues(DBParams.USER_ID), false, null);
        if (!session.isNew())
        {   // Session in progress
            application = (ServletApplication)session.getAttribute(DBParams.APPLICATION);    // Get the currently instantiate app getValue(APPLICATION);  //
            if (application != null)
            { // Always
                String systemName = BaseHttpTask.cleanParam(req.getParameterValues(DBConstants.SYSTEM_NAME), false, null);
                if (systemName != null)
                    if (!Utility.getSystemSuffix(systemName, application.getProperty(DBConstants.DEFAULT_SYSTEM_NAME)).equalsIgnoreCase(Utility.getSystemSuffix(application.getProperty(DBConstants.SYSTEM_NAME), application.getProperty(DBConstants.DEFAULT_SYSTEM_NAME))))
                        if (strUserNameOrID == null)
                            strUserNameOrID = DBConstants.BLANK;    // Change in system name means new app.
                if (strUserNameOrID != null)    // user= means new user
                {
                    boolean bNewUser = false;
                    if ((strUserNameOrID.length() == 0) && (BaseHttpTask.cleanParam(req.getParameterValues(DBParams.PASSWORD), false, null) == null)) // This means logout
                        bNewUser = true;  // Different user or up the security level
                    if (strUserNameOrID.length() > 0)
                    {
                        if ((BaseHttpTask.cleanParam(req.getParameterValues(DBParams.PASSWORD), false, null) != null)
                                && (BaseHttpTask.cleanParam(req.getParameterValues(DBParams.PASSWORD), false, null).length() > 0))
                                    bNewUser = true;    // Supplying a password = sign on to new
                        if ((BaseHttpTask.cleanParam(req.getParameterValues(DBParams.AUTH_TOKEN), false, null) != null)
                                && (BaseHttpTask.cleanParam(req.getParameterValues(DBParams.AUTH_TOKEN), false, null).length() > 0))
                                    bNewUser = true;    // Supplying authentication = sign on to new
                        if ((!strUserNameOrID.equalsIgnoreCase(application.getProperty(DBParams.USER_NAME)))
                            && (!strUserNameOrID.equalsIgnoreCase(application.getProperty(DBParams.USER_ID))))
                                bNewUser = true;
                        if (systemName != null)
                            if (!Utility.getSystemSuffix(systemName, application.getProperty(DBConstants.DEFAULT_SYSTEM_NAME)).equalsIgnoreCase(Utility.getSystemSuffix(application.getProperty(DBConstants.SYSTEM_NAME), application.getProperty(DBConstants.DEFAULT_SYSTEM_NAME))))
                                bNewUser = true;
                    }
                    if (bNewUser)
                    { // They want to change users
                        session.removeAttribute(DBParams.APPLICATION); // This also frees the application (through the xxx interface)    //removeValue(APPLICATION);   //
                        application = null;
                    }
                }
                if (application != null)
                {
                    application = this.changeCookie(application, req, res);
                    application.addTask(this, null);      // Will be removed on exit
                    this.setApplication(application);

                    return application;     // Return the current application
                }
            }
        }
        Utility.getLogger().info("New session");
    // Next, see if they are changing their userID/username
        if (strUserNameOrID != null)    // Note user= means "new user"
        { // Special logic - change User name/ID
            Map<String,Object> properties = new Hashtable<String,Object>();
            Map<String,Object> appProperties = this.getApplicationProperties(false);
            if (appProperties != null)
                properties.putAll(appProperties);
            this.addDatabaseProperties(properties);
            properties.remove(Params.USER_ID);	// Being careful
            properties.put(Params.USER_NAME, strUserNameOrID);
            application = new ServletApplication(null, properties, null);    // Read/Create the new user ID/Name
            if (this.getApplication() == null)
            {
            	application.addTask(this, null);
            	this.setApplication(application);
            }
            int iLoginError = DBConstants.NORMAL_RETURN;
            if ((strUserNameOrID.length() > 0)
                && (!strUserNameOrID.equalsIgnoreCase(application.getProperty(DBParams.USER_ID)))
                    && (!strUserNameOrID.equalsIgnoreCase(application.getProperty(DBParams.USER_NAME))))
                iLoginError = DBConstants.ERROR_RETURN;   // Invalid user - Show the sign-up screen
            else
            {
                String strPassword = BaseHttpTask.cleanParam(req.getParameterValues(DBParams.PASSWORD), false, null);
                if ((strPassword != null) && (strPassword.length() > 0))
                {
                    try {
                        byte[] bytes = strPassword.getBytes(Base64.DEFAULT_ENCODING);
                        bytes = Base64.encodeSHA(bytes); // TODO (don) Fix this
                        char[] chars = Base64.encode(bytes);
                        strPassword = new String(chars);
                    } catch (NoSuchAlgorithmException ex) {
                        ex.printStackTrace();
                        strPassword = null;
                    } catch (UnsupportedEncodingException ex) {
                        ex.printStackTrace();
                        strPassword = null;
                    }
                }
                if ((strPassword == null) || (strPassword.length() == 0))
                {
                    strPassword = BaseHttpTask.cleanParam(req.getParameterValues(DBParams.AUTH_TOKEN), false, null);
                    strPassword = application.authenticateToken(strPassword);	// TODO(don) HACK - For now, authentication is the SHA password! See MainApplication/139
                }
                if ((strPassword != null) && (strPassword.length() > 0))
                {
                    String strDomain = Utility.getDomainFromURL(BaseHttpTask.getParam(req, DBParams.URL), null);
                    iLoginError = application.login(this, strUserNameOrID, strPassword, strDomain);
                }
            }
            if (iLoginError != DBConstants.NORMAL_RETURN)
            {
                application.login(this, null, null, null);
                application.setProperty(Params.SECURITY_LEVEL, Integer.toString(Constants.LOGIN_USER));    // This will cause a security error, which will display the login screen
                if (iLoginError == DBConstants.ERROR_RETURN)
                    application.setProperty(Params.SECURITY_MAP, Application.CREATE_USER_REQUIRED);
                else
                    application.setProperty(Params.SECURITY_MAP, Application.LOGIN_REQUIRED);
            }
        }
        
        application = this.changeCookie(application, req, res);
        
        session.setAttribute(DBParams.APPLICATION, application);   // Cache the preferences, so I can save my session putValue(APPLICATION, application);
        // Note: Application will be notified of session termination in valueUnbound method
        application.addTask(this, null);      // Will be removed on session destroy
        this.setApplication(application);
        return application;
    }
public App findApplication ( HttpServletRequest req , HttpServletResponse res ) { ServletApplication application = null ; String strDatatype = BaseHttpTask . getParam ( req , DBParams . DATATYPE , null ) ; if ( strDatatype != null ) { return this . getNonUserApplication ( ) ; } HttpSession session = req . getSession ( ) ; String strUserNameOrID = BaseHttpTask . cleanParam ( req . getParameterValues ( DBParams . USER_NAME ) , false , null ) ; if ( strUserNameOrID == null ) strUserNameOrID = BaseHttpTask . cleanParam ( req . getParameterValues ( DBParams . USER_ID ) , false , null ) ; if ( ! session . isNew ( ) ) { application = ( ServletApplication ) session . getAttribute ( DBParams . APPLICATION ) ; if ( application != null ) { String systemName = BaseHttpTask . cleanParam ( req . getParameterValues ( DBConstants . SYSTEM_NAME ) , false , null ) ; if ( systemName != null ) if ( ! Utility . getSystemSuffix ( systemName , application . getProperty ( DBConstants . DEFAULT_SYSTEM_NAME ) ) . equalsIgnoreCase ( Utility . getSystemSuffix ( application . getProperty ( DBConstants . SYSTEM_NAME ) , application . getProperty ( DBConstants . DEFAULT_SYSTEM_NAME ) ) ) ) if ( strUserNameOrID == null ) strUserNameOrID = DBConstants . BLANK ; if ( strUserNameOrID != null ) { boolean bNewUser = false ; if ( ( strUserNameOrID . length ( ) == 0 ) && ( BaseHttpTask . cleanParam ( req . getParameterValues ( DBParams . PASSWORD ) , false , null ) == null ) ) bNewUser = true ; if ( strUserNameOrID . length ( ) > 0 ) { if ( ( BaseHttpTask . cleanParam ( req . getParameterValues ( DBParams . PASSWORD ) , false , null ) != null ) && ( BaseHttpTask . cleanParam ( req . getParameterValues ( DBParams . PASSWORD ) , false , null ) . length ( ) > 0 ) ) bNewUser = true ; if ( ( BaseHttpTask . cleanParam ( req . getParameterValues ( DBParams . AUTH_TOKEN ) , false , null ) != null ) && ( BaseHttpTask . cleanParam ( req . getParameterValues ( DBParams . AUTH_TOKEN ) , false , null ) . length ( ) > 0 ) ) bNewUser = true ; if ( ( ! strUserNameOrID . equalsIgnoreCase ( application . getProperty ( DBParams . USER_NAME ) ) ) && ( ! strUserNameOrID . equalsIgnoreCase ( application . getProperty ( DBParams . USER_ID ) ) ) ) bNewUser = true ; if ( systemName != null ) if ( ! Utility . getSystemSuffix ( systemName , application . getProperty ( DBConstants . DEFAULT_SYSTEM_NAME ) ) . equalsIgnoreCase ( Utility . getSystemSuffix ( application . getProperty ( DBConstants . SYSTEM_NAME ) , application . getProperty ( DBConstants . DEFAULT_SYSTEM_NAME ) ) ) ) bNewUser = true ; } if ( bNewUser ) { session . removeAttribute ( DBParams . APPLICATION ) ; application = null ; } } if ( application != null ) { application = this . changeCookie ( application , req , res ) ; application . addTask ( this , null ) ; this . setApplication ( application ) ; return application ; } } } Utility . getLogger ( ) . info ( "New session" ) ; if ( strUserNameOrID != null ) { Map < String , Object > properties = new Hashtable < String , Object > ( ) ; Map < String , Object > appProperties = this . getApplicationProperties ( false ) ; if ( appProperties != null ) properties . putAll ( appProperties ) ; this . addDatabaseProperties ( properties ) ; properties . remove ( Params . USER_ID ) ; properties . put ( Params . USER_NAME , strUserNameOrID ) ; application = new ServletApplication ( null , properties , null ) ; if ( this . getApplication ( ) == null ) { application . addTask ( this , null ) ; this . setApplication ( application ) ; } int iLoginError = DBConstants . NORMAL_RETURN ; if ( ( strUserNameOrID . length ( ) > 0 ) && ( ! strUserNameOrID . equalsIgnoreCase ( application . getProperty ( DBParams . USER_ID ) ) ) && ( ! strUserNameOrID . equalsIgnoreCase ( application . getProperty ( DBParams . USER_NAME ) ) ) ) iLoginError = DBConstants . ERROR_RETURN ; else { String strPassword = BaseHttpTask . cleanParam ( req . getParameterValues ( DBParams . PASSWORD ) , false , null ) ; if ( ( strPassword != null ) && ( strPassword . length ( ) > 0 ) ) { try { byte [ ] bytes = strPassword . getBytes ( Base64 . DEFAULT_ENCODING ) ; bytes = Base64 . encodeSHA ( bytes ) ; char [ ] chars = Base64 . encode ( bytes ) ; strPassword = new String ( chars ) ; } catch ( NoSuchAlgorithmException ex ) { ex . printStackTrace ( ) ; strPassword = null ; } catch ( UnsupportedEncodingException ex ) { ex . printStackTrace ( ) ; strPassword = null ; } } if ( ( strPassword == null ) || ( strPassword . length ( ) == 0 ) ) { strPassword = BaseHttpTask . cleanParam ( req . getParameterValues ( DBParams . AUTH_TOKEN ) , false , null ) ; strPassword = application . authenticateToken ( strPassword ) ; } if ( ( strPassword != null ) && ( strPassword . length ( ) > 0 ) ) { String strDomain = Utility . getDomainFromURL ( BaseHttpTask . getParam ( req , DBParams . URL ) , null ) ; iLoginError = application . login ( this , strUserNameOrID , strPassword , strDomain ) ; } } if ( iLoginError != DBConstants . NORMAL_RETURN ) { application . login ( this , null , null , null ) ; application . setProperty ( Params . SECURITY_LEVEL , Integer . toString ( Constants . LOGIN_USER ) ) ; if ( iLoginError == DBConstants . ERROR_RETURN ) application . setProperty ( Params . SECURITY_MAP , Application . CREATE_USER_REQUIRED ) ; else application . setProperty ( Params . SECURITY_MAP , Application . LOGIN_REQUIRED ) ; } } application = this . changeCookie ( application , req , res ) ; session . setAttribute ( DBParams . APPLICATION , application ) ; application . addTask ( this , null ) ; this . setApplication ( application ) ; return application ; }
See if this user has a persisent userID cookie and set up preferences.
Note: This method is only called if the user does not have a current session.
The username and password are used for authentication here:
user=null, password=null - No change
user=user, password=null - Sign-on (login level)
user=user, password=pass - Sign-on (authenticated level)
user=blank,password=null - Sign-off (anonymous user)
username can be user(name) or userid.
password can be password or auth(entication) token.