package com.primeton.dgs.kernel.core.web; 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.HashSet; import java.util.Map; import java.util.Set; /** * * 元数据旧架构支持 RESTFull API 扩展 * * *
 *
 * Created by zhaopx.
 * User: zhaopx
 * Date: 2020/9/10
 * Time: 14:24
 *
 * 
* * @author zhaopx */ public class MetaCubeHttpServletRequestWrapper extends HttpServletRequestWrapper { /** * 元数据部分参数是从 payload 获得的,装饰 RestFull API */ protected final Map payloadParameters = new HashMap<>(); /** * Constructs a request object wrapping the given request. * * @param request the {@link HttpServletRequest} to be wrapped. * @throws IllegalArgumentException if the request is null */ public MetaCubeHttpServletRequestWrapper(HttpServletRequest request) { super(request); } public MetaCubeHttpServletRequestWrapper(HttpServletRequest request, Map payloadParameters) { super(request); this.payloadParameters.putAll(payloadParameters); } @Override public String getParameter(String name) { String parameter = super.getParameter(name); // 获取参数,如果 form 中为空,则从 payload 中获取 return parameter == null ? (String)payloadParameters.get(name) : parameter; } @Override public String[] getParameterValues(String name) { String[] parameterValues = super.getParameterValues(name); // 获取参数,如果 form 中为空,则从 payload 中获取 return parameterValues == null ? new String[]{ (String)payloadParameters.get(name)} : parameterValues; } @Override public Map getParameterMap() { Map parameterMap = new HashMap<>(super.getParameterMap()); // 合并参数 for (Map.Entry entry : payloadParameters.entrySet()) { parameterMap.put(entry.getKey(), new String[]{ (String)entry.getValue()}); } return parameterMap; } @Override public Enumeration getParameterNames() { Set vector = new HashSet(); // form 参数 Enumeration parameterNames = super.getParameterNames(); while (parameterNames.hasMoreElements()) { vector.add(parameterNames.nextElement()); } // payload 参数 for (String s : payloadParameters.keySet()) { vector.add(s); } // 构造一次性返回 return Collections.enumeration(vector); } }