package at.gv.egovernment.moa.id.data;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

import at.gv.egovernment.moa.logging.Logger;

/**
 * The Cookie-class provides methods to save and return cookies for
 * each single session
 * 
 * @author Stefan Knirsch
 * @version $Id$
 * 
 */
public class Cookie {
  /** A HahsMap containing all our cookies */
  HashMap cookies = new HashMap();
  /** A HashMap to temporarely store 'Set-Cookie' values from the OnlineApplication 
   * to send them back to the client/browser as soon as possible */
  HashMap cookies401 = new HashMap();

  /**
   * Adds a Cookie from a response with response-code 401 to the cookie-pool
   * for sending it back to the browser / client
   * @param String: the complete 'Set-Cookie' - String
   */
  public void add401(String cookieString)
  {
    cookies401.put(getKey(cookieString),cookieString);
  }
  
  /**
   * Get the HashMap containing all cookies to be sent to the browser / client
   * @return HashMap with all cookies
   */
  public HashMap get401()
  {
    return cookies401;
  }
  
  /**
   * Clear the 401 cookie-pool
   */
  public void clear401()
  {
    cookies401.clear();
  }
  
  /**
   * Set a cookie that comes from the Online-Application
   * and save it in our "normal" cookie-pool
   * @param String the complete "Set-Cookie" - String from the Online-Application
   */
  public void setCookie(String value) {
    cookies.put(getKey(value), getValue(value));
  }
  
  /**
   * Method saveOldCookies.
   * @param String the complete "Set-Cookie" - String from the Online-Application
   */
  public void saveOldCookies(String value) {
    StringTokenizer st = new StringTokenizer(value,";");
    while (st.hasMoreTokens())
    {
      // We have to trim because the Tokenizer returns cookies including spaces at the beginning
     StringTokenizer st2 = new StringTokenizer(st.nextToken().trim(),"=");
     String cookieKey = st2.nextToken().trim();
     if (st2.hasMoreTokens())
     {
       String cookieValue = st2.nextToken().trim();     
       if (!cookies.containsKey(cookieKey))
        cookies.put(cookieKey , cookieValue);
     }      
    }      
    Logger.debug("Found these cookies: " + getCookies());
  }

  /**
   * Get a String containing all cookies saved in that session seperated by '; '
   * to be sent back to the Online-Application
   * @return String containing all cookies saved in that session seperated by '; '
   */
  public String getCookies() {
    String result = "";
    if (cookies.size()==0)
      return null;
    Iterator i = cookies.keySet().iterator();
    while (i.hasNext()) {
      String key = (String) i.next();
      result += key + "=" + (String)cookies.get(key) + "; ";
    }
    return result.substring(0, result.length() - 2);
  }

  /**
   * Returns the key of a key-value-pair of a cookie
   * getKey("CookieA=1234") returns CookieA
   * @param String the complete "Set-cookie" String containing a key-value-pair of a cookie
   * @return String the key of a key-value-pair of a cookie
   */
  private String getKey(String input) {
      return input.substring(0, input.indexOf("="));
  }

  /**
   * Returns the value of a key-value-pair of a cookie
   * getKey("CookieA=1234") returns 1234
   * @param String the complete "Set-cookie" String containing a key-value-pair of a cookie
   * @return String the value of a key-value-pair of a cookie
   */
  private String getValue(String input) {
    if (input.indexOf(";") == -1)
        return input.substring(input.indexOf("=") + 1, input.getBytes().length);
    return input.substring(input.indexOf("=") + 1, input.indexOf(";"));
  }

}