/***********************************************************************
 * 
 *  Copyright (C) 2006 Novell, Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 2.1
 *  of the License.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, Novell, Inc.
 * 
 *  To contact Novell about this file by physical or electronic mail, 
 *  you may find current contact information at www.novell.com.
 * 
 *  Author: Juan Carlos Luciani <jluciani@novell.com>
 *  
 ***********************************************************************/

package com.novell.casa.authtoksvc;

import org.apache.log4j.Logger;

import java.io.*;
import java.io.PrintWriter;

/**
 * GetAuthPolicy Class.
 * <p>
 * This class processes get authentication policy requests for a particular
 * service.
 * 
 */
public final class GetAuthPolicy implements RpcMethod
{
   private static final Logger m_log = Logger.getLogger(GetAuthPolicy.class);
   private Logger m_trace = null;

   private SvcConfig          m_svcConfig;
   private EnabledSvcsConfig  m_enabledSvcsConfig;

   /**
    * Constructor.
    */
   public GetAuthPolicy()
   {
      // Nothing to do at this time
   }

   /**
    * Initialize the Rpc method.
    *
    * @param svcConfig           Service configuration object.
    * @param enabledSvcsConfig   Enabled services configuration object.
    * @param traceLogger         Logger for tracing requests.
    * @throws Exception
    */
   public void init(SvcConfig svcConfig,
                    EnabledSvcsConfig enabledSvcsConfig,
                    Logger traceLogger) throws Exception
   {
      m_svcConfig = svcConfig;
      m_enabledSvcsConfig = enabledSvcsConfig;
      m_trace = traceLogger;
   }

   /**
    * Process Rpc.
    *
    * @param inStream   Input stream with request.
    * @param out        Ouput print writer for reply.
    * @throws IOException
    */
   public void invoke(InputStream inStream, PrintWriter out) throws IOException
   {
      try
      {
         m_log.debug("invoke()");

         // Read and parse the GetAuthPolicyReqMsg sent from the client
         GetAuthPolicyReqMsg getAuthPolicyReqMsg = new GetAuthPolicyReqMsg(inStream);

         // Verify that the service is enabled
         if (m_enabledSvcsConfig.svcEnabled(getAuthPolicyReqMsg.getHostName(), getAuthPolicyReqMsg.getServiceName()))
         {
            // Get the auth policy for the service
            byte[] authPolicy = m_enabledSvcsConfig.getAuthPolicyFileDataForSvc(getAuthPolicyReqMsg.getHostName(),
                                                                                getAuthPolicyReqMsg.getServiceName());
            if (authPolicy != null)
            {
               // Write out the response
               GetAuthPolicyRespMsg getAuthPolicyRespMsg = new GetAuthPolicyRespMsg(ProtoDefs.httpOkStatusMsg,
                                                                                    ProtoDefs.httpOkStatusCode,
                                                                                    new String(Base64Coder.encode(authPolicy)));
               out.println(getAuthPolicyRespMsg.toString());
               m_trace.info("GetAuthPolicy Rpc, Host=" + getAuthPolicyReqMsg.getHostName()
                            + ", Svc=" + getAuthPolicyReqMsg.getServiceName()
                            + ", Status=SUCCESS");
            }
            else
            {
               m_log.error("invoke()- authPolicy is null for enabled service: " + getAuthPolicyReqMsg.getServiceName());
               GetAuthPolicyRespMsg getAuthPolicyRespMsg = new GetAuthPolicyRespMsg(ProtoDefs.httpServerErrorStatusMsg,
                                                                                    ProtoDefs.httpServerErrorStatusCode);
               out.println(getAuthPolicyRespMsg.toString());

               m_trace.info("GetAuthPolicy Rpc, Host=" + getAuthPolicyReqMsg.getHostName()
                            + ", Svc=" + getAuthPolicyReqMsg.getServiceName()
                            + ", Status=INTERNAL_ERROR");
            }
         }
         else
         {
            // The service has not been enabled to utilize our authentication tokens
            GetAuthPolicyRespMsg getAuthPolicyRespMsg = new GetAuthPolicyRespMsg(ProtoDefs.httpNotFoundStatusMsg,
                                                                                 ProtoDefs.httpNotFoundStatusCode);
            out.println(getAuthPolicyRespMsg.toString());

            m_log.warn("invoke()- Service "
                       + getAuthPolicyReqMsg.getServiceName()
                       + " at " + getAuthPolicyReqMsg.getHostName()
                       + " not enabled");

            m_trace.info("GetAuthPolicy Rpc, Host=" + getAuthPolicyReqMsg.getHostName()
                         + ", Svc=" + getAuthPolicyReqMsg.getServiceName()
                         + ", Status=SERVICE_NOT_CONFIGURED");
         }
      }
      catch (Exception e)
      {
         m_log.error("invoke()- Exception: " + e.toString());
         m_trace.error("GetAuthPolicy Rpc, Status=INTERNAL_ERROR");

         // Write out the response
         try
         {
            GetAuthPolicyRespMsg getAuthPolicyRespMsg = new GetAuthPolicyRespMsg(ProtoDefs.httpServerErrorStatusMsg,
                                                                                 ProtoDefs.httpServerErrorStatusCode);
            out.println(getAuthPolicyRespMsg.toString());
         }
         catch (Exception e2)
         {
            m_log.error("invoke()- Exception trying to construct response msg: " + e2.toString());
         }
      }
   }

   /**
    * Return the method id.
    *
    * @return Method id.
    */
   public String getId()
   {
      return "GetAuthPolicy";
   }
}