
package org.apache.myfaces.trinidadinternal.webapp;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

@SuppressWarnings("deprecation")
public class ReplaceParametersRequestWrapper extends HttpServletRequestWrapper
{
  public ReplaceParametersRequestWrapper(
    HttpServletRequest    request,
    Map<String, String[]> parameters)
  {
    super(request);
    _parameters = parameters;
  }

  @Override
  public void setCharacterEncoding(String encoding)
  {
    // Do nothing
  }

  @Override
  public String getParameter(String param)
  {
    String[] value = _getParameterValues(param);
    if (value == null)
      return null;

    return value[0];
  }

  @Override
  public Map<String, String[]> getParameterMap()
  {
    return Collections.unmodifiableMap(_parameters);
  }

  @Override
  public Enumeration<String> getParameterNames()
  {
    return Collections.enumeration(_parameters.keySet());
  }

  @Override
  public String[] getParameterValues(String param)
  {
    String[] value = _getParameterValues(param);
    if (value == null)
      return null;

    return value.clone();
  }

  private String[] _getParameterValues(String param)
  {
    return _parameters.get(param);
  }

  private Map<String, String[]> _parameters;
}
