/*
 * 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 com.silverwrist.dynamo.WebConstants;
import com.silverwrist.dynamo.iface.*;

class ServletChainParameterInput implements ChainParameterInput
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private ServletRequest m_req;
  private Collection m_names = null;

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

  ServletChainParameterInput(ServletRequest req)
  {
    m_req = req;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ChainParameterInput
   *--------------------------------------------------------------------------------
   */

  public synchronized Collection getChainParameterNames()
  {
    if (m_names==null)
    { // probe the request attributes
      Enumeration names = m_req.getAttributeNames();
      if (names==null)
	m_names = Collections.EMPTY_LIST;
      else
      { // go through the attributes and strip out the ones with a Dynamo internal prefix
	ArrayList tmp = new ArrayList();
	while (names.hasMoreElements())
	{ // move over the names in turn
	  String n = (String)(names.nextElement());
	  if (!(n.startsWith(WebConstants.REQUEST_PREFIX)))
	    tmp.add(n);

	} // end while

	if (tmp.isEmpty())
	  m_names = Collections.EMPTY_LIST;
	else
	{ // save off the list
	  tmp.trimToSize();
	  m_names = Collections.unmodifiableList(tmp);

	} // end else

      } // end else

    } // end if

    return m_names;

  } // end getChainParameterNames

  public Object getChainParameter(String name)
  {
    if (name.startsWith(WebConstants.REQUEST_PREFIX))
      return null;
    else
      return m_req.getAttribute(name);

  } // end getChainParameter

} // end class ServletChainParameterInput
