/*
 * Copyright (C) EFP Consulting GmbH. All rights reserved.
 *
 * This software is published under the terms of the Web3 Software License
 * version 1.0, a copy of which has been included with this distribution in
 * the LICENSE file.
 */

package cc.efp.web3.portal.cocoon;

import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.configuration.ConfigurationException;

import org.apache.avalon.excalibur.pool.Poolable;
import org.apache.avalon.excalibur.pool.Recyclable;

import org.apache.cocoon.Constants;
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.apache.cocoon.acting.ConfigurableComposerAction;

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

import cc.efp.web3.portal.components.UserFactory;
import cc.efp.web3.portal.components.User;
import cc.efp.web3.portal.components.WrongCredentialsException;
import cc.efp.web3.portal.components.Digest;
import cc.efp.web3.language.components.Language;

/**
 *
 * @author  <a href="michael.gerzabek@at.efp.cc">Michael Gerzabek</a>
 * @version 2001-12-17
 */
public class AuthenticationAction extends ConfigurableComposerAction implements Poolable, Recyclable {
    
    /**
     * @param m_user enthält in der sitemap configuration den Wert "user" und bestimmt den POST Parameter
     * der den Benutzernamen definiert.
     */
    String m_user;
    /**
     * @param m_passwd enthält in der sitemap configuration den Wert "passwd" und bestimmt den POST
     * Parameter für das Bentuerzpasswort.
     */
    String m_passwd;
    
    public void configure(Configuration configuration) throws ConfigurationException {
        m_user   = configuration.getChild("request-user-name").getValue("user");
        m_passwd = configuration.getChild("request-passwd-name").getValue("passwd");
    }
        
    public Map act (Redirector redirector, SourceResolver resolver, 
                Map objectModel, String src, Parameters par) throws Exception {

        HashMap theMap = new HashMap();
        Request request = (Request) objectModel.get(Constants.REQUEST_OBJECT);
        Session session = request.getSession(true);
        if ( session.isNew() ) {
            // whatever
        }
        else {
            // do some cleanup and logging enhancement
        }
        theMap.put("SID", session.getId());
        UserFactory ufy = null;
        Digest digest = null;
        try {
            ufy = (UserFactory) manager.lookup( UserFactory.ROLE );
            digest = (Digest) manager.lookup( Digest.ROLE );
            String username = request.getParameter( m_user );
            String passwd   = digest.getDigest( request.getParameter( m_passwd ) );
            User user = ufy.logon( username, passwd );
            session.setAttribute( "User", user );
            session.setAttribute( "Language", user.getLanguageHint() );
        }
        catch (WrongCredentialsException x) {
            theMap.put("SID", "Rejected");
            /* Strange behaviour on logger in conjunction with selector!
              getLogger().error(x.getMessage(), x);
             */
        }
        catch (Exception x) {
            theMap.put("SID", "Error");
            /* Strange behaviour on logger in conjunction with selector!
              getLogger().error(x.getMessage(), x);
             */
        }
        finally {
            manager.release( ufy );
            manager.release( digest );
        }
        return Collections.unmodifiableMap(theMap);
    }
    
    public void recycle() {
    }
    
}

