Sfoglia il codice sorgente

Merge branch 'v1.0.0' of http://39.106.8.246:3003/apm/apm-basics into v1.0.0

lixingyhj@gmail.com 2 anni fa
parent
commit
f49bd73bdc

+ 100 - 94
apm-common/src/main/java/com/persagy/apm/common/handler/PoemsRequestBodyWrapper.java

@@ -27,98 +27,104 @@ import java.util.Map;
  * @version V1.0 2021/9/9 8:18 下午
  */
 public class PoemsRequestBodyWrapper extends HttpServletRequestWrapper {
-    /**
-     * 存储body转换后的字符串
-     */
-    private String reqBodyStr;
-
-    public PoemsRequestBodyWrapper(HttpServletRequest request) throws IOException {
-        super(request);
-        // 初始化PoemsContext
-        initPoemsContext();
-    }
-
-    /**
-     * 初始化PoemsContext
-     *
-     * @author lixing
-     * @version V1.0 2021/9/9 8:17 下午
-     */
-    private void initPoemsContext() throws IOException{
-        //从输入流中取出body串, 如果为空,直接返回
-        reqBodyStr = IOUtils.toString(super.getInputStream(), "utf-8");
-        if (StringUtils.isEmpty(reqBodyStr)) {
-            return;
-        }
-
-        //reqBodyStr转为Map对象
-        Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr, new TypeReference<HashMap<String, Object>>() {
-        });
-        String userId = (String) paramMap.get("userId");
-        String loginDevice = (String) paramMap.get("loginDevice");
-        String pd = (String) paramMap.get("pd");
-        String groupCode = (String) paramMap.get("groupCode");
-        String projectId = (String) paramMap.get("projectId");
-
-        if (StringUtils.isBlank(groupCode)) {
-
-            throw new IllegalArgumentException(super.getRequestURI() + ": 请求体中缺少groupCode");
-        }
-        if (StringUtils.isBlank(userId)) {
-            throw new IllegalArgumentException(super.getRequestURI() + ": 请求体中缺少userId");
-        }
-        if (StringUtils.isBlank(pd)) {
-            throw new IllegalArgumentException(super.getRequestURI() + ": 请求体中缺少pd");
-        }
-        PoemsContext.setContext(userId, loginDevice, pd, groupCode, projectId);
-    }
-
-    @Override
-    public BufferedReader getReader() throws IOException {
-        return new BufferedReader(new InputStreamReader(this.getInputStream()));
-    }
-
-    @Override
-    public ServletInputStream getInputStream() throws IOException {
-        //非json类型,直接返回
-        if (super.getHeader(HttpHeaders.CONTENT_TYPE) == null ||
-                !super.getHeader(HttpHeaders.CONTENT_TYPE).contains(MediaType.APPLICATION_JSON_VALUE)) {
-            return super.getInputStream();
-        }
-
-        if (StringUtils.isEmpty(reqBodyStr)) {
-            return super.getInputStream();
-        }
-
-        //reqBodyStr转为Map对象
-        Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr, new TypeReference<HashMap<String, Object>>() {
-        });
-
-        //重新构造一个输入流对象
-        byte[] bytes = JSON.toJSONString(paramMap).getBytes("utf-8");
-        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
-
-        return new ServletInputStream() {
-
-            @Override
-            public boolean isFinished() {
-                return false;
-            }
-
-            @Override
-            public boolean isReady() {
-                return false;
-            }
-
-            @Override
-            public void setReadListener(ReadListener listener) {
-
-            }
-
-            @Override
-            public int read() throws IOException {
-                return bais.read();
-            }
-        };
-    }
+	/**
+	 * 存储body转换后的字符串
+	 */
+	private String reqBodyStr;
+
+	public PoemsRequestBodyWrapper(HttpServletRequest request) throws IOException {
+		super(request);
+		// 初始化PoemsContext
+		initPoemsContext(request);
+	}
+
+	/**
+	 * 初始化PoemsContext
+	 *
+	 * @author lixing
+	 * @version V1.0 2021/9/9 8:17 下午
+	 * @param request
+	 */
+	private void initPoemsContext(HttpServletRequest request) throws IOException {
+		// 从输入流中取出body串, 如果为空,直接返回
+		reqBodyStr = IOUtils.toString(super.getInputStream(), "utf-8");
+		if (StringUtils.isEmpty(reqBodyStr)) {
+			return;
+		}
+
+		String reqUserId = request.getParameter("userId");
+		String reqGroupCode = request.getParameter("groupCode");
+		//String reqGrojectId = request.getParameter("projectId");
+		String reqPd="1";
+		// reqBodyStr转为Map对象
+		Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr,
+				new TypeReference<HashMap<String, Object>>() {
+				});
+		String userId = (String) paramMap.get("userId");
+		String loginDevice = (String) paramMap.get("loginDevice");
+		String pd = (String) paramMap.get("pd");
+		String groupCode = (String) paramMap.get("groupCode");
+		String projectId = (String) paramMap.get("projectId");
+
+		if (StringUtils.isBlank(groupCode) && StringUtils.isBlank(reqGroupCode)) {
+			throw new IllegalArgumentException(super.getRequestURI() + ": 请求体或请求参数中缺少groupCode");
+		}
+		if (StringUtils.isBlank(userId)  && StringUtils.isBlank(reqUserId)) {
+			throw new IllegalArgumentException(super.getRequestURI() + ": 请求体或请求参数中缺少userId");
+		}
+		if (StringUtils.isBlank(pd) &&StringUtils.isBlank(reqPd) ) {
+			throw new IllegalArgumentException(super.getRequestURI() + ": 请求体或请求参数中缺少pd ");
+		}
+		PoemsContext.setContext(userId, loginDevice, pd, groupCode, projectId);
+	}
+
+	@Override
+	public BufferedReader getReader() throws IOException {
+		return new BufferedReader(new InputStreamReader(this.getInputStream()));
+	}
+
+	@Override
+	public ServletInputStream getInputStream() throws IOException {
+		// 非json类型,直接返回
+		if (super.getHeader(HttpHeaders.CONTENT_TYPE) == null
+				|| !super.getHeader(HttpHeaders.CONTENT_TYPE).contains(MediaType.APPLICATION_JSON_VALUE)) {
+			return super.getInputStream();
+		}
+
+		if (StringUtils.isEmpty(reqBodyStr)) {
+			return super.getInputStream();
+		}
+
+		// reqBodyStr转为Map对象
+		Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr,
+				new TypeReference<HashMap<String, Object>>() {
+				});
+
+		// 重新构造一个输入流对象
+		byte[] bytes = JSON.toJSONString(paramMap).getBytes("utf-8");
+		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+
+		return new ServletInputStream() {
+
+			@Override
+			public boolean isFinished() {
+				return false;
+			}
+
+			@Override
+			public boolean isReady() {
+				return false;
+			}
+
+			@Override
+			public void setReadListener(ReadListener listener) {
+
+			}
+
+			@Override
+			public int read() throws IOException {
+				return bais.read();
+			}
+		};
+	}
 }