123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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 扩展
- *
- *
- * <pre>
- *
- * Created by zhaopx.
- * User: zhaopx
- * Date: 2020/9/10
- * Time: 14:24
- *
- * </pre>
- *
- * @author zhaopx
- */
- public class MetaCubeHttpServletRequestWrapper extends HttpServletRequestWrapper {
- /**
- * 元数据部分参数是从 payload 获得的,装饰 RestFull API
- */
- protected final Map<String, Object> 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<String, Object> 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<String, String[]> getParameterMap() {
- Map<String, String[]> parameterMap = new HashMap<>(super.getParameterMap());
- // 合并参数
- for (Map.Entry<String, Object> entry : payloadParameters.entrySet()) {
- parameterMap.put(entry.getKey(), new String[]{ (String)entry.getValue()});
- }
- return parameterMap;
- }
- @Override
- public Enumeration<String> getParameterNames() {
- Set<String> vector = new HashSet();
- // form 参数
- Enumeration<String> parameterNames = super.getParameterNames();
- while (parameterNames.hasMoreElements()) {
- vector.add(parameterNames.nextElement());
- }
- // payload 参数
- for (String s : payloadParameters.keySet()) {
- vector.add(s);
- }
- // 构造一次性返回
- return Collections.enumeration(vector);
- }
- }
|