<commit_before>
package org.exist.cocoon;

import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Session;
import org.apache.cocoon.acting.ServiceableAction;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;

import java.util.HashMap;
import java.util.Map;

import org.xmldb.api.*;
import org.xmldb.api.base.*;

/**
 *  Cocoon action to authenticate a user against the database.
 * 
 * If authentication succeeds, user and password will be stored into
 * the current session.
 *
 * @author     <NAME> <<EMAIL>>
 */
public class XMLDBSessionLoginAction extends ServiceableAction
     implements ThreadSafe {

    public Map act( Redirector redirector, SourceResolver resolver,
                    Map objectModel, String source, Parameters param ) throws Exception {
        Request request = ObjectModelHelper.getRequest( objectModel );
        if ( request == null ) {
            getLogger().error( "XMLDBSessionLoginAction: no request object!" );
            return null;
        }
        Map map = new HashMap();
        String user = null;
        String passwd = null;

        // check for valid session
        Session session = request.getSession( false );
        if ( session == null ||
            ( !request.isRequestedSessionIdValid() ) )
            // create a new session
            session = request.getSession( true );

        // check user account and store it to the session
        if ( session.getAttribute( "user" ) == null ) {
            // try to read parameters from sitemap
            user = param.getParameter( "user", null );
            passwd = param.getParameter( "password", null );
            // else try to read parameters from request
            if ( user == null ) {
                user = request.getParameter( "user" );
                passwd = request.getParameter( "password" );
            }
            if ( user == null ) {
                getLogger().error( "XMLDBSessionLoginAction: no parameters!" );
                return null;
            }
            if ( source == null ) {
                getLogger().error( "XMLDBSessionLoginAction: no source specified!" );
                return null;
            }
            getLogger().info( "trying to login user " + user );

            // try to access collection specified in source
            try {
                Collection collection =
                    DatabaseManager.getCollection( source, user, passwd );
            } catch ( XMLDBException e ) {
                getLogger().error( "login denied: " + e.getMessage() );
                return null;
            }

            // store user info to session
            session.setAttribute( "user", user );
            session.setAttribute( "password", <PASSWORD> );
        }
        else {
            // retrieve user info from session
            user = (String) session.getAttribute( "user" );
            passwd = (String) session.getAttribute( "password" );
        }
        // return data to the sitemap
        map.put( "user", user );
        map.put( "password", <PASSWORD> );
        return map;
    }
}
<commit_msg>session attribute might be an XQuery value, so cast to String fails.<commit_after>
package org.exist.cocoon;

import java.util.HashMap;
import java.util.Map;

import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.acting.ServiceableAction;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.Session;
import org.apache.cocoon.environment.SourceResolver;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.Item;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.XMLDBException;

/**
 *  Cocoon action to authenticate a user against the database.
 * 
 * If authentication succeeds, user and password will be stored into
 * the current session.
 *
 * @author     <NAME> <<EMAIL>.org>
 */
public class XMLDBSessionLoginAction extends ServiceableAction
     implements ThreadSafe {

    public Map act( Redirector redirector, SourceResolver resolver,
                    Map objectModel, String source, Parameters param ) throws Exception {
        Request request = ObjectModelHelper.getRequest( objectModel );
        if ( request == null ) {
            getLogger().error( "XMLDBSessionLoginAction: no request object!" );
            return null;
        }
        Map map = new HashMap();
        String user = null;
        String passwd = null;

        // check for valid session
        Session session = request.getSession( false );
        if ( session == null ||
            ( !request.isRequestedSessionIdValid() ) )
            // create a new session
            session = request.getSession( true );

        // check user account and store it to the session
        if ( session.getAttribute( "user" ) == null ) {
            // try to read parameters from sitemap
            user = param.getParameter( "user", null );
            passwd = param.getParameter( "password", null );
            // else try to read parameters from request
            if ( user == null ) {
                user = request.getParameter( "user" );
                passwd = request.getParameter( "password" );
            }
            if ( user == null ) {
                getLogger().error( "XMLDBSessionLoginAction: no parameters!" );
                return null;
            }
            if ( source == null ) {
                getLogger().error( "XMLDBSessionLoginAction: no source specified!" );
                return null;
            }
            getLogger().info( "trying to login user " + user );

            // try to access collection specified in source
            try {
                Collection collection =
                    DatabaseManager.getCollection( source, user, passwd );
            } catch ( XMLDBException e ) {
                getLogger().error( "login denied: " + e.getMessage() );
                return null;
            }

            // store user info to session
            session.setAttribute( "user", user );
            session.setAttribute( "password", <PASSWORD> );
        }
        else {
            // retrieve user info from session
        	user = getSessionAttribute(session, "user");
        	passwd = getSessionAttribute(session, "password");
        }
        // return data to the sitemap
        map.put( "user", user );
        map.put( "password", <PASSWORD> );
        return map;
    }
    
    private String getSessionAttribute(Session session, String attribute) {
		Object obj = session.getAttribute(attribute);
		if(obj == null)
			return null;
		if(obj instanceof Item)
			try {
				return ((Item)obj).getStringValue();
			} catch (XPathException e) {
				return null;
			}
		return obj.toString();
	}
}

{'fraction_non_alphanumeric': 0.09994439810953572, 'fraction_numerical': 0.0, 'mean_word_length': 2.1106787721573714, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4663204', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1812, 'n_words': 533}