/*
 * ExternalProvider.java 1.00 27-Feb-2001
 *
 * Copyright 2000-2001 by Oracle Corporation
 * 500 Oracle Parkway, Redwood Shores, California, 94065, U.S.A.
 * All rights reserved.
 */
package oracle.portal.sample.v2.devguide.externalApp;

import oracle.portal.provider.v2.*;
import oracle.portal.provider.v2.http.*;
import oracle.portal.provider.v2.externalApp.*;
import oracle.portal.provider.v2.security.AuthenticationException;

import java.lang.reflect.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ExternalProvider extends ProviderInstance
{
    private static final String SERVLET_PATH = "/servlet/ExternalAppFlights";

    private FlightDispatch mDispatcher;

    /**
     *  Constructor
     *
     *  Call super to constructor the Default Provider.
     */
    public ExternalProvider()
    {
        super();
        mDispatcher = new FlightDispatch(SERVLET_PATH);
    }

    /**
     *  initSession performs all session and login work.
     *
     *  In this case the initSession simply needs to call the Application Login
     *  routines to get the Application Cookie, and return this to the caller.
     *  This routine is called during the doLogin calls of the provider framwork.
     *
     *  Note: This example uses 2 cookies - 1 is the original application's cookie
     *  and the second is the Jserv cookie used to maintain the servlet session.
     *  2 Cookies are required for this example because the original application
     *  did not use the servlet session.  Instead it used it's own cookie.  However,
     *  the JPDK requires a servlet session so a second cookie (for the servlet
     *  session) must be created.
     *
     *  @param  user  Is a ProviderUser object which can be seen in the
     *  JPDK API Documentation.
     *
     * @return An object array of, in this case, cookies.
     */
    public Object[] initSession(ProviderUser user, ExternalPrincipal extuser)
        throws ProviderException, AuthenticationException
    {
        ApplicationLogin login = new ApplicationLogin();
        Cookie appCookie = null;
        Cookie[] cookies = new Cookie[1];

        // Get hold of the path to the root of this web application
        String contextPath = ((ServletProviderContext)getProviderContext()).getContextPath();

        if ((appCookie = login.getApplicationCookie(extuser, contextPath)) == null)
        {
            // external login failed so raise an AuthenticationException to notify
            // the portal of the failure.
            throw new AuthenticationException("Failed to login to Fancy Flights Application");
        }
        // external login was successful so establish JPDK session
        super.initSession(user, extuser);
        // Cookie expires after 1.5 hours
        appCookie.setMaxAge(5400);
        cookies[0] = appCookie;
        return cookies;
    }

    /**
     * getDispatcher returns the FlightDispatch object that was initialized
     * in the constructor.
     */
    public FlightDispatch getDispatcher()
    {
        return mDispatcher;
    }

}
