//______________________________________________________________________________
//   |       |
//   | knipp |           Knipp Medien und Kommunikation GmbH
//    -------
//
//
//     Knipp RRI Toolkit
//
//
//     Copyright (C) 2005 by Knipp Medien und Kommunikation GmbH
//
//     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; either version 2.1
//     of the License, or any later version.
//
//     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 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, write to the
//     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
//     Boston, MA 02111-1307 USA
//
//
//     initial author:    Klaus Malorny
//
//     version:           1.4 (15127), 2011/06/03 14:52:49
//                        $Revision: 14936 $
//                        $Date: 2010-11-05 15:57:40 +0100 (Fri, 05 Nov 2010) $
//                        $Author: Klaus.Malorny $
//

package de.knipp.rritoolkit.request;

import java.io.IOException;

import java.util.Date;

import org.w3c.dom.DOMException;

import de.knipp.rritoolkit.EncodeException;
import de.knipp.rritoolkit.KVList;
import de.knipp.rritoolkit.Port;
import de.knipp.rritoolkit.XmlBuilder;

import de.knipp.rritoolkit.response.AuthInfo1CreateResponse;

import static de.knipp.rritoolkit.Utils.toHex;
import static de.knipp.rritoolkit.Utils.formatDate;
import static de.knipp.rritoolkit.Utils.calcAuthInfoHash;

/**
 *  class that represents an AuthInfo 1 create request
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public class AuthInfo1CreateRequest extends DomainSimpleRequest
{
  /** the hash of the authinfo */

  private byte[] authInfoHash;


  /** the requested expiration date */

  private Date expirationDate;



  /**
   *  constructor
   */

  public AuthInfo1CreateRequest ()
  {
    // empty
  }


  /**
   *  constructor
   *
   *  @param theDomainName          the domain name
   *  @param theEncodedDomainName   the encoded domain name, may be {@code null}
   *                                for ASCII only domains
   *  @param hash                   the binary hash
   *  @param date                   the requested expiration date,
   *                                may be {@code null}
   */

  public AuthInfo1CreateRequest (String theDomainName,
    String theEncodedDomainName, byte[] hash, Date date)
  {
    super (theDomainName, theEncodedDomainName);
    setAuthInfoHash (hash);
    setExpirationDate (date);
  }


  /**
   *  constructor
   *
   *  @param theDomainName          the domain name
   *  @param theEncodedDomainName   the encoded domain name, may be {@code null}
   *                                for ASCII only domains
   *  @param authInfo               the AuthInfo password
   *  @param date                   the requested expiration date,
   *                                may be {@code null}
   */

  public AuthInfo1CreateRequest (String theDomainName,
    String theEncodedDomainName, String authInfo, Date date)
  {
    super (theDomainName, theEncodedDomainName);
    setAuthInfo (authInfo);
    setExpirationDate (date);
  }


  /**
   *  constructor
   *
   *  @param theDomainName          the domain name
   *  @param hash                   the binary hash
   *  @param date                   the requested expiration date,
   *                                may be {@code null}
   */

  public AuthInfo1CreateRequest (String theDomainName, byte[] hash, Date date)
  {
    super (theDomainName);
    setAuthInfoHash (hash);
    setExpirationDate (date);
  }


  /**
   *  constructor
   *
   *  @param theDomainName          the domain name
   *  @param authInfo               the AuthInfo password
   *  @param date                   the requested expiration date,
   *                                may be {@code null}
   */

  public AuthInfo1CreateRequest (String theDomainName, String authInfo,
    Date date)
  {
    super (theDomainName);
    setAuthInfo (authInfo);
    setExpirationDate (date);
  }


  /**
   *  constructor
   *
   *  @param theDomainName          the domain name
   *  @param theEncodedDomainName   the encoded domain name, may be {@code null}
   *                                for ASCII only domains
   *  @param hash                   the binary hash
   */

  public AuthInfo1CreateRequest (String theDomainName,
    String theEncodedDomainName, byte[] hash)
  {
    super (theDomainName, theEncodedDomainName);
    setAuthInfoHash (hash);
  }


  /**
   *  constructor
   *
   *  @param theDomainName          the domain name
   *  @param theEncodedDomainName   the encoded domain name, may be {@code null}
   *                                for ASCII only domains
   *  @param authInfo               the AuthInfo password
   */

  public AuthInfo1CreateRequest (String theDomainName,
    String theEncodedDomainName, String authInfo)
  {
    super (theDomainName, theEncodedDomainName);
    setAuthInfo (authInfo);
  }


  /**
   *  constructor
   *
   *  @param theDomainName          the domain name
   *  @param hash                   the binary hash
   */

  public AuthInfo1CreateRequest (String theDomainName, byte[] hash)
  {
    super (theDomainName);
    setAuthInfoHash (hash);
  }


  /**
   *  constructor
   *
   *  @param theDomainName          the domain name
   *  @param authInfo               the AuthInfo password
   */

  public AuthInfo1CreateRequest (String theDomainName, String authInfo)
  {
    super (theDomainName);
    setAuthInfo (authInfo);
  }



//--- implementation of Request ----------------------------------------------



  /**
   *  execute the this request on the given port.
   *
   *  @param port                 the port on which this request shall
   *                              be executed
   *  @return                     the response
   *  @throws IOException         if the execution failed
   */

  @Override
  public AuthInfo1CreateResponse execute (Port port)
    throws IOException
  {
    return (AuthInfo1CreateResponse) super.execute (port);
  }



  /**
   *  return the XML element name
   *
   *  @return                      the element name
   */

  @Override
  protected String getRequestElementName ()
  {
    return "createAuthInfo1";
  }



  /**
   *  allow the addition of attributes to the request. The default
   *  implementation is empty and should not be called by implementing
   *  subclasses
   *
   *  @param xb                   the XML builder
   *  @throws DOMException        on DOM exceptions
   *  @throws EncodeException     if the encoding fails for some reason
   */

  @Override
  protected void addAttributes (XmlBuilder xb)
    throws DOMException, EncodeException
  {
    xb.attr ("hash", toHex (authInfoHash));
    
    if (expirationDate != null)
      xb.attr ("expire", formatDate (expirationDate));
  }




  /**
   *  return the action string for this request
   *
   *  @return                     the action string
   */

  @Override
  protected String getActionCode ()
  {
    return "create-authinfo1";
  }



  /**
   *  method that needs to be implemented by subclasses for the
   *  key-value list conversion
   *
   *  @param list                   the list that has to be filled
   *  @throws EncodeException       if the encoding fails for some reason
   */

  @Override
  protected void requestToKVList (KVList list)
    throws EncodeException
  {
    super.requestToKVList (list);
    
    list.set ("authinfohash", toHex (authInfoHash));

    if (expirationDate != null)
      list.set ("authinfoexpire", formatDate (expirationDate));
  }




  /**
   *  return a string representation for debugging purposes
   *
   *  @return                 the representation
   */

  @Override
  public String toString ()
  {
    return super.toString () + "  hash: " + toHex (authInfoHash) +
      "\n  expire: " + expirationDate + "\n";
  }



  //--- access methods ---------------------------------------------------------


  /**
   *  return the binary hash of the AuthInfo
   *
   *  @return               the hash
   */

  public byte[] getAuthInfoHash ()
  {
    return authInfoHash;
  }



  /**
   *  set the binary hash of the AuthInfo
   *
   *  @param hash          the hash (no copy is made)
   */

  public void setAuthInfoHash (byte[] hash)
  {
    authInfoHash = hash;
  }



  /**
   *  set the AuthInfo password. The method calculates the hash and stores this.
   *
   *  @param authInfo                 the AuthInfo password
   */

  public void setAuthInfo (String authInfo)
  {
    authInfoHash = calcAuthInfoHash (authInfo);
  }



  /**
   *  return the requested expiration date of the AuthInfo
   *
   *  @return                           the date, may be {@code null}
   */

  public Date getExpirationDate ()
  {
    return expirationDate;
  }


  /**
   *  set the requested expiration date of the AuthInfo
   *
   *  @param date                       the date, may be {@code null}
   */

  public void setExpirationDate (Date date)
  {
    expirationDate = date;
  }


}






