package com.hys.app.framework.context.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class ParameterServletRequestWrapper extends HttpServletRequestWrapper {
    private final Map<String, String[]> parameterMap;

    public ParameterServletRequestWrapper(HttpServletRequest request, Map<String, String[]> newParams) {
        super(request);
        parameterMap = new HashMap<>();
        parameterMap.putAll(newParams);
    }

    @Override
    public String getParameter(String name) {
        String[] values = parameterMap.get(name);
        if (values == null || values.length == 0) {
            return null;
        }
        return values[0];
    }

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

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

    @Override
    public String[] getParameterValues(String name) {
        return parameterMap.get(name);
    }
}
