/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.servlet;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.silverwrist.dynamo.iface.*;

class ServletHeaderInput implements HeaderInput
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private HttpServletRequest m_req;
  private Collection m_names = null;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  ServletHeaderInput(HttpServletRequest req)
  {
    m_req = req;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface HeaderInput
   *--------------------------------------------------------------------------------
   */

  public synchronized Collection getHeaderNames()
  {
    if (m_names==null)
    { // get the list of header names
      Enumeration enum = m_req.getHeaderNames();
      if (enum==null)
	m_names = Collections.EMPTY_LIST;
      else
      { // copy enumeration to array list
	ArrayList tmp = new ArrayList();
	while (enum.hasMoreElements())
	  tmp.add(enum.nextElement());
	if (tmp.isEmpty())
	  m_names = Collections.EMPTY_LIST;
	else
	{ // trim the array list and save it off
	  tmp.trimToSize();
	  m_names = Collections.unmodifiableList(tmp);

	} // end else

      } // end else

    } // end if

    return m_names;

  } // end getHeaderNames

  public List getHeaderValues(String name)
  {
    Enumeration enum = m_req.getHeaders(name);
    if (enum==null)
      return Collections.EMPTY_LIST;
    ArrayList tmp = new ArrayList();
    while (enum.hasMoreElements())
      tmp.add(enum.nextElement());
    if (tmp.isEmpty())
      return Collections.EMPTY_LIST;
    tmp.trimToSize();
    return Collections.unmodifiableList(tmp);

  } // end getHeaderValues

  public String getHeader(String name)
  {
    return m_req.getHeader(name);

  } // end getHeader

  public int getIntHeader(String name)
  {
    return m_req.getIntHeader(name);

  } // end getIntHeader

  public java.util.Date getDateHeader(String name)
  {
    long tmp = m_req.getDateHeader(name);
    return ((tmp==-1) ? null : new java.util.Date(tmp));

  } // end getDateHeader

  public boolean containsHeader(String name)
  {
    return (m_req.getHeader(name)!=null);

  } // end containsHeader

} // end class ServletHeaderInput
