/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.servlet;

import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.bodington.server.BuildingServerException;
import org.bodington.server.NavigationSession;

/**
 * Class that handles request authentication using the systems internal method.
 * @see org.bodington.server.realm.PassPhraseAuthenticator
 * @author Alexis O'Connor
 */
class InternalSessionInitializer extends AbstractSessionInitializer
{
    /**
     * Find a session associated with the request. This implementation of the
     * method calls the version in its superclass and if that method returns
     * <code>null</code> this will just create a new one.
     * @param request {@inheritDoc}
     * @return Always returns a HttpSession, creating one if it can't find one
     * associated with the request.
     */
    protected HttpSession findHttpSession( Request request )
    {
        HttpSession session = super.findHttpSession( request );
        return (session == null) 
            ? (org.bodington.servlet.HttpSession) request.getSession( true ) 
            :  session;
    }
    
    protected boolean initialize( Request request,
        HttpSession session, NavigationSession navigation )
    {
        try
        {
            Properties props = new Properties();
            String alias = request.getParameter( 
                "org_bodington_servlet_authenticator_alias" );
            
            if (alias == null || !(alias.length() > 0))
                return false;
            
            // Put any form field with appropriate name into a properties
            // object.
            Enumeration enumeration = request.getParameterNames();
            while ( enumeration.hasMoreElements() )
            {
                String name = (String)enumeration.nextElement();
                if ( name.startsWith( "org_bodington_servlet_credential_" ) )
                {
                    props.setProperty( 
                        name.substring( "org_bodington_servlet_credential_".length() ), 
                        request.getParameter( name ) );
                }
            }

            navigation.setAuthenticationCredentials( props, alias );
        }
        catch ( BuildingServerException e )
        {
        }

        return true;
    }
    
    protected void postInitialize( HttpSession session, Request request )
    {
        super.postInitialize( session, request );
        session.setAuthType( HttpServletRequest.FORM_AUTH );
    }
}
