/***********************************************************************
 * 
 *  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;

/**
 * AuthRespMsg Class.
 * 
 * This class deals with the message sent to the CASA Client as a
 * response to an authentication request. The format of the message is
 * as follows when the response includes a session token:
 * <p>
 *    <?xml version="1.0" encoding="UTF-8"?>
 *    <auth_resp>
 *    <status><description>OK</description>200</status>
 *    <session_token><lifetime>lifetime value</lifetime>session token data</session_token>
 *    </auth_resp>
 * <p>
 * The format of the message is as follows when the response does not
 * include a session token.
 * <p>
 *    <?xml version="1.0" encoding="UTF-8"?>
 *    <auth_resp>
 *    <status><description>status description</description>status code</status>
 *    </auth_resp>
 * <p>
 * Plase note that the protocol utilizes the status codes defined
 * in the HTTP 1.1 Specification.
 *
 */
public final class AuthRespMsg
{
   private static final Logger m_log = Logger.getLogger(AuthRespMsg.class);

   final String   m_msg;

   /**
    * Constructor for a msg that does not include the session token.
    *
    * @param statusDescription   Description of the status code.
    * @param statusCode          Status code.
    */
   public AuthRespMsg (String statusDescription, String statusCode)
   {
      // Get a StringBuffer to help us with the construction of the message
      StringBuffer sb = new StringBuffer();

      // Start building the message
      sb.append(ProtoDefs.xmlDeclaration); sb.append("\r\n");
      sb.append("<"); sb.append(ProtoDefs.authResponseElementName); sb.append(">\r\n");
      sb.append("<"); sb.append(ProtoDefs.statusElementName); sb.append(">");
      sb.append("<"); sb.append(ProtoDefs.descriptionElementName); sb.append(">"); sb.append(statusDescription); sb.append("</"); sb.append(ProtoDefs.descriptionElementName); sb.append(">");
      sb.append(statusCode); sb.append("</"); sb.append(ProtoDefs.statusElementName); sb.append(">\r\n");
      sb.append("</"); sb.append(ProtoDefs.authResponseElementName); sb.append(">\r\n");

      // The message has now been built, save it.
      m_msg = sb.toString();
   }

   /**
    * Constructor for a msg that includes the session token.
    *
    * @param statusDescription      Status code description.
    * @param statusCode             Status code.
    * @param sessionToken           Session token string.
    * @param sessionTokenLifetime   Lifetime of the session token.
    */
   public AuthRespMsg (String statusDescription,
                       String statusCode,
                       String sessionToken,
                       String sessionTokenLifetime)
   {
      // Get a StringBuffer to help us with the construction of the message
      StringBuffer sb = new StringBuffer();

      // Start building the message
      sb.append(ProtoDefs.xmlDeclaration); sb.append("\r\n");
      sb.append("<"); sb.append(ProtoDefs.authResponseElementName); sb.append(">\r\n");
      sb.append("<"); sb.append(ProtoDefs.statusElementName); sb.append(">");
      sb.append("<"); sb.append(ProtoDefs.descriptionElementName); sb.append(">"); sb.append(ProtoDefs.httpOkStatusMsg); sb.append("</"); sb.append(ProtoDefs.descriptionElementName); sb.append(">");
      sb.append(ProtoDefs.httpOkStatusCode); sb.append("</"); sb.append(ProtoDefs.statusElementName); sb.append(">\r\n");
      sb.append("<"); sb.append(ProtoDefs.sessionTokenElementName); sb.append(">");
      sb.append("<"); sb.append(ProtoDefs.lifetimeElementName); sb.append(">"); sb.append(sessionTokenLifetime); sb.append("</"); sb.append(ProtoDefs.lifetimeElementName); sb.append(">");
      sb.append(sessionToken); sb.append("</"); sb.append(ProtoDefs.sessionTokenElementName); sb.append(">\r\n");
      sb.append("</"); sb.append(ProtoDefs.authResponseElementName); sb.append(">\r\n");

      // The message has now been built, save it.
      m_msg = sb.toString();
   }

   /**
    * Returns a string containing the AuthRespMsg.
    *
    * @return  Authentication response message string.
    */
   public final String toString()
   {
      return m_msg;
   }
}
